dafs-updates-20080612
[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
14     ("$Header$");
15
16 #include <string.h>
17 #ifdef HAVE_STDLIB_H
18 #include <stdlib.h>
19 #endif
20
21 /**
22  * map a partition id from any partition-style name.
23  *
24  * @param[in] aname  partition name string
25  *
26  * @return partition index number
27  *   @retval -1  invalid partition name
28  *
29  * @see volutil_PartitionName2_r
30  * @see volutil_PartitionName_r
31  * @see volutil_PartitionName
32  */
33 afs_int32
34 volutil_GetPartitionID(char *aname)
35 {
36     register char tc;
37     afs_int32 temp;
38     char ascii[3];
39
40     tc = *aname;
41     if (tc == 0)
42         return -1;              /* unknown */
43     /* numbers go straight through */
44     if (tc >= '0' && tc <= '9') {
45         temp = atoi(aname);
46         /* this next check is to make the syntax less ambiguous when discriminating
47          * between volume numbers and partition IDs.  This less things like
48          * bos salvage do some reasonability checks its input w/o checking
49          * to see if the partition is really on the server.
50          */
51         if (temp < 0 || temp > 25)
52             return -1;
53         else
54             return temp;
55     }
56     /* otherwise check for vicepa or /vicepa, or just plain "a" */
57     ascii[2] = 0;
58     if (strlen(aname) <= 2) {
59         strcpy(ascii, aname);
60     } else if (!strncmp(aname, "/vicep", 6)) {
61         strncpy(ascii, aname + 6, 2);
62     } else if (!strncmp(aname, "vicep", 5)) {
63         strncpy(ascii, aname + 5, 2);
64     } else
65         return -1;              /* bad partition name */
66     /* now partitions are named /vicepa ... /vicepz, /vicepaa, /vicepab, .../vicepzz,
67      * and are numbered from 0.  Do the appropriate conversion */
68     if (ascii[1] == 0) {
69         /* one char name, 0..25 */
70         if (ascii[0] < 'a' || ascii[0] > 'z')
71             return -1;          /* wrongo */
72         return ascii[0] - 'a';
73     } else {
74         /* two char name, 26 .. <whatever> */
75         if (ascii[0] < 'a' || ascii[0] > 'z')
76             return -1;          /* wrongo */
77         if (ascii[1] < 'a' || ascii[1] > 'z')
78             return -1;          /* just as bad */
79         return (ascii[0] - 'a') * 26 + (ascii[1] - 'a') + 26;
80     }
81 }
82
83 /**
84  * convert a partition index number into a partition name string (/vicepXX).
85  *
86  * @param[in]  part     partition index number
87  * @param[out] tbuffer  buffer in which to store name
88  * @param[in]  buflen   length of tbuffer
89  *
90  * @return operation status
91  *   @retval 0   success
92  *   @retval -1  buffer too short
93  *   @retval -2  invalid partition id
94  *
95  * @see volutil_PartitionName_r
96  * @see volutil_PartitionName
97  * @see volutil_GetPartitionID
98  */
99 afs_int32
100 volutil_PartitionName2_r(afs_int32 part, char *tbuffer, size_t buflen)
101 {
102     char tempString[3];
103     register int i;
104
105     if (part < 0 || part >= (26 * 26 + 26)) {
106         return -2;
107     }
108
109     tempString[1] = tempString[2] = 0;
110     strncpy(tbuffer, "/vicep", buflen);
111     if (part <= 25) {
112         tempString[0] = 'a' + part;
113     } else {
114         part -= 26;
115         i = (part / 26);
116         tempString[0] = i + 'a';
117         tempString[1] = (part % 26) + 'a';
118     }
119     if (strlcat(tbuffer, tempString, buflen) >= buflen) {
120         return -1;
121     }
122     return tbuffer;
123 }
124
125 #define BAD_VID "BAD VOLUME ID"
126 #define BAD_VID_LEN (sizeof(BAD_VID))
127 /**
128  * convert a partition index number into a partition name string (/vicepXX).
129  *
130  * @param[in]  part     partition index number
131  * @param[out] tbuffer  buffer in which to store name
132  * @param[in]  buflen   length of tbuffer
133  *
134  * @return partition name string
135  *   @retval ""               buffer too short
136  *   @retval "SPC"            buffer too short
137  *   @retval "BAD VOLUME ID"  avalue contains an invalid partition index
138  *
139  * @note you may wish to consider using volutil_PartitionName2_r, as its
140  *       error handling is more standard
141  *
142  * @see volutil_PartitionName2_r
143  * @see volutil_PartitionName
144  * @see volutil_GetPartitionID
145  */
146 char *
147 volutil_PartitionName_r(int part, char *tbuffer, int buflen)
148 {
149     afs_int32 code;
150
151     if (buflen < BAD_VID_LEN) {
152         strlcpy(tbuffer, "SPC", buflen);
153         return tbuffer;
154     }
155
156     code = volutil_PartitionName2_r(part, tbuffer, buflen);
157
158     if (code == -2) {
159         strlcpy(tbuffer, BAD_VID, buflen);
160     }
161
162     return tbuffer;
163 }
164
165 /**
166  * convert a partition index number into a partition name string (/vicepXX).
167  *
168  * @param[in] avalue  partition index number
169  *
170  * @return partition name string
171  *   @retval "BAD VOLUME ID"  avalue contains an invalid partition index
172  *
173  * @warning this interface is not re-entrant
174  *
175  * @see volutil_PartitionName2_r
176  * @see volutil_PartitionName_r
177  * @see volutil_GetPartitionID
178  */
179 char *
180 volutil_PartitionName(int avalue)
181 {
182 #define VPN_TBUFLEN 64
183     static char tbuffer[VPN_TBUFLEN];
184     return volutil_PartitionName_r(avalue, tbuffer, VPN_TBUFLEN - 1);
185 }
186
187 /* is this a digit or a digit-like thing? */
188 static int
189 ismeta(register int ac, register int abase)
190 {
191 /*    if (ac == '-' || ac == 'x' || ac == 'X') return 1; */
192     if (ac >= '0' && ac <= '7')
193         return 1;
194     if (abase <= 8)
195         return 0;
196     if (ac >= '8' && ac <= '9')
197         return 1;
198     if (abase <= 10)
199         return 0;
200     if (ac >= 'a' && ac <= 'f')
201         return 1;
202     if (ac >= 'A' && ac <= 'F')
203         return 1;
204     return 0;
205 }
206
207 /* given that this is a digit or a digit-like thing, compute its value */
208 static int
209 getmeta(register int ac)
210 {
211     if (ac >= '0' && ac <= '9')
212         return ac - '0';
213     if (ac >= 'a' && ac <= 'f')
214         return ac - 'a' + 10;
215     if (ac >= 'A' && ac <= 'F')
216         return ac - 'A' + 10;
217     return 0;
218 }
219
220 afs_int32
221 util_GetInt32(register char *as, afs_int32 * aval)
222 {
223     register afs_int32 total;
224     register int tc;
225     int base;
226     int negative;
227
228     total = 0;                  /* initialize things */
229     negative = 0;
230
231     /* skip over leading spaces */
232     while ((tc = *as)) {
233         if (tc != ' ' && tc != '\t')
234             break;
235     }
236
237     /* compute sign */
238     if (*as == '-') {
239         negative = 1;
240         as++;                   /* skip over character */
241     }
242
243     /* compute the base */
244     if (*as == '0') {
245         as++;
246         if (*as == 'x' || *as == 'X') {
247             base = 16;
248             as++;
249         } else
250             base = 8;
251     } else
252         base = 10;
253
254     /* compute the # itself */
255     while ((tc = *as)) {
256         if (!ismeta(tc, base))
257             return -1;
258         total *= base;
259         total += getmeta(tc);
260         as++;
261     }
262
263     if (negative)
264         *aval = -total;
265     else
266         *aval = total;
267     return 0;
268 }
269
270 afs_uint32
271 util_GetUInt32(register char *as, afs_uint32 * aval)
272 {
273     register afs_uint32 total;
274     register int tc;
275     int base;
276
277     total = 0;                  /* initialize things */
278
279     /* skip over leading spaces */
280     while ((tc = *as)) {
281         if (tc != ' ' && tc != '\t')
282             break;
283     }
284
285     /* compute the base */
286     if (*as == '0') {
287         as++;
288         if (*as == 'x' || *as == 'X') {
289             base = 16;
290             as++;
291         } else
292             base = 8;
293     } else
294         base = 10;
295
296     /* compute the # itself */
297     while ((tc = *as)) {
298         if (!ismeta(tc, base))
299             return -1;
300         total *= base;
301         total += getmeta(tc);
302         as++;
303     }
304
305     *aval = total;
306     return 0;
307 }