aix-strings-h-preferred-over-string-h-20010628
[openafs.git] / src / util / volparse.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * ALL RIGHTS RESERVED
12  */
13
14 #include <afs/param.h>
15 #include <afsconfig.h>
16 #ifdef HAVE_STRINGS_H
17 #include <strings.h>
18 #else
19 #ifdef HAVE_STRING_H
20 #include <string.h>
21 #endif
22 #endif
23 #ifdef HAVE_STDLIB_H
24 #include <stdlib.h>
25 #endif
26
27 /* map a partition id from any partition-style name */
28 afs_int32 volutil_GetPartitionID(aname)
29 char *aname; {
30     register char tc;
31     afs_int32 temp;
32     char ascii[3];
33
34     tc = *aname;
35     if (tc == 0) return -1;     /* unknown */
36     /* numbers go straight through */
37     if (tc >= '0' && tc <= '9') {
38         temp = atoi(aname);
39         /* this next check is to make the syntax less ambiguous when discriminating
40          * between volume numbers and partition IDs.  This less things like
41          * bos salvage do some reasonability checks its input w/o checking
42          * to see if the partition is really on the server.
43          */
44         if (temp < 0 || temp > 25) return -1;
45         else return temp;
46     }
47     /* otherwise check for vicepa or /vicepa, or just plain "a" */
48     ascii[2] = 0;
49     if (strlen(aname) <= 2) {
50         strcpy(ascii, aname);
51     }
52     else if (!strncmp(aname, "/vicep", 6)) {
53         strncpy(ascii, aname+6, 2);
54     }
55     else if (!strncmp(aname, "vicep", 5)) {
56         strncpy(ascii, aname+5, 2);
57     }
58     else return -1;     /* bad partition name */
59     /* now partitions are named /vicepa ... /vicepz, /vicepaa, /vicepab, .../vicepzz,
60        and are numbered from 0.  Do the appropriate conversion */
61     if (ascii[1] == 0) {
62         /* one char name, 0..25 */
63         if (ascii[0] <  'a' || ascii[0] > 'z')  return -1;  /* wrongo */
64         return ascii[0] - 'a';
65     }
66     else {
67         /* two char name, 26 .. <whatever> */
68         if (ascii[0] <  'a' || ascii[0] > 'z')  return -1;  /* wrongo */
69         if (ascii[1] <  'a' || ascii[1] > 'z')  return -1;  /* just as bad */
70         return (ascii[0] - 'a') * 26 + (ascii[1] - 'a') + 26;
71     }
72 }
73
74 /* map a partition number back into a partition string */
75 #define BAD_VID "BAD VOLUME ID"
76 #define BAD_VID_LEN (sizeof(BAD_VID))
77 char *volutil_PartitionName_r(int avalue, char *tbuffer, int buflen)
78 {
79     char tempString[3];
80     register int i;
81
82     if (buflen < BAD_VID_LEN) {
83         if (buflen > 3)
84             (void) strcpy(tbuffer, "SPC");
85         else
86             tbuffer[0] = '\0';
87         return tbuffer;
88     }
89     bzero(tbuffer, buflen);
90     tempString[1] = tempString[2] = 0;
91     strcpy(tbuffer, "/vicep");
92     if (avalue < 0 || avalue >= (26*26+26)) {
93         strcpy(tbuffer, "BAD VOLUME ID");
94     }
95     else if (avalue <= 25) {
96         tempString[0] = 'a'+avalue;
97         strcat(tbuffer, tempString);
98     }
99     else {
100         avalue -= 26;
101         i = (avalue / 26 );
102         tempString[0] = i + 'a';
103         tempString[1] = (avalue % 26) + 'a';
104         strcat(tbuffer, tempString);
105     }
106     return tbuffer;
107 }
108
109 char *volutil_PartitionName(int avalue)
110 {
111 #define VPN_TBUFLEN 64
112      static char tbuffer[VPN_TBUFLEN];
113      return volutil_PartitionName_r(avalue, tbuffer, VPN_TBUFLEN-1);
114 }
115
116 /* is this a digit or a digit-like thing? */
117 static int ismeta(ac, abase)
118 register int abase;
119 register int ac; {
120 /*    if (ac == '-' || ac == 'x' || ac == 'X') return 1; */
121     if (ac >= '0' && ac <= '7') return 1;
122     if (abase <= 8) return 0;
123     if (ac >= '8' && ac <= '9') return 1;
124     if (abase <= 10) return 0;
125     if (ac >= 'a' && ac <= 'f') return 1;
126     if (ac >= 'A' && ac <= 'F') return 1;
127     return 0;
128 }
129
130 /* given that this is a digit or a digit-like thing, compute its value */
131 static int getmeta(ac)
132 register int ac; {
133     if (ac >= '0' && ac <= '9') return ac - '0';
134     if (ac >= 'a' && ac <= 'f') return ac - 'a' + 10;
135     if (ac >= 'A' && ac <= 'F') return ac - 'A' + 10;
136     return 0;
137 }
138
139 afs_int32 util_GetInt32 (as, aval)
140 register char *as;
141 afs_int32 *aval;
142 {
143     register afs_int32 total;
144     register int tc;
145     int base;
146     int negative;
147
148     total = 0;  /* initialize things */
149     negative = 0;
150
151     /* skip over leading spaces */
152     while ((tc = *as)) {
153         if (tc != ' ' && tc != '\t') break;
154     }
155
156     /* compute sign */
157     if (*as == '-') {
158         negative = 1;
159         as++;   /* skip over character */
160     }
161
162     /* compute the base */
163     if (*as == '0') {
164         as++;
165         if (*as == 'x' || *as == 'X') {
166             base = 16;
167             as++;
168         }
169         else base = 8;
170     }
171     else base = 10;
172
173     /* compute the # itself */
174     while((tc = *as)) {
175         if (!ismeta(tc, base)) return -1;
176         total *= base;
177         total += getmeta(tc);
178         as++;
179     }
180
181     if (negative) *aval = -total;
182     else *aval = total;
183     return 0;
184 }
185
186 afs_uint32
187 GetUInt32 (as, aval)
188 register char *as;
189 afs_uint32 *aval;
190 {
191     register afs_uint32 total;
192     register int tc;
193     int base;
194
195     total = 0;  /* initialize things */
196
197     /* skip over leading spaces */
198     while ((tc = *as)) {
199         if (tc != ' ' && tc != '\t') break;
200     }
201
202     /* compute the base */
203     if (*as == '0') {
204         as++;
205         if (*as == 'x' || *as == 'X') {
206             base = 16;
207             as++;
208         }
209         else base = 8;
210     }
211     else base = 10;
212
213     /* compute the # itself */
214     while((tc = *as)) {
215         if (!ismeta(tc, base)) return -1;
216         total *= base;
217         total += getmeta(tc);
218         as++;
219     }
220     
221     *aval = total;
222     return 0;
223 }
224