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