2 * Copyright 2000, International Business Machines Corporation and others.
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
10 #include <afsconfig.h>
11 #include <afs/param.h>
17 /* maximum number of partitions - must match vol/voldefs.h */
18 #define VOLMAXPARTS 255
21 * map a partition id from any partition-style name.
23 * @param[in] aname partition name string
25 * @return partition index number
26 * @retval -1 invalid partition name
28 * @see volutil_PartitionName2_r
29 * @see volutil_PartitionName_r
30 * @see volutil_PartitionName
33 volutil_GetPartitionID(char *aname)
41 return -1; /* unknown */
42 /* numbers go straight through */
43 if (tc >= '0' && tc <= '9') {
45 /* this next check is to make the syntax less ambiguous when discriminating
46 * between volume numbers and partition IDs. This lets things like
47 * bos salvage do some reasonability checks on its input w/o checking
48 * to see if the partition is really on the server.
50 if (temp < 0 || temp >= VOLMAXPARTS)
55 /* otherwise check for vicepa or /vicepa, or just plain "a" */
57 if (strlen(aname) <= 2) {
59 } else if (!strncmp(aname, "/vicep", 6)) {
60 strncpy(ascii, aname + 6, 2);
61 } else if (!strncmp(aname, "vicep", 5)) {
62 strncpy(ascii, aname + 5, 2);
64 return -1; /* bad partition name */
65 /* now partitions are named /vicepa ... /vicepz, /vicepaa, /vicepab, .../vicepzz,
66 * and are numbered from 0. Do the appropriate conversion */
68 /* one char name, 0..25 */
69 if (ascii[0] < 'a' || ascii[0] > 'z')
70 return -1; /* wrongo */
71 return ascii[0] - 'a';
73 /* two char name, 26 .. <whatever> */
74 if (ascii[0] < 'a' || ascii[0] > 'z')
75 return -1; /* wrongo */
76 if (ascii[1] < 'a' || ascii[1] > 'z')
77 return -1; /* just as bad */
78 temp = (ascii[0] - 'a') * 26 + (ascii[1] - 'a') + 26;
79 return (temp >= VOLMAXPARTS ? -1 : temp);
84 * convert a partition index number into a partition name string (/vicepXX).
86 * @param[in] part partition index number
87 * @param[out] tbuffer buffer in which to store name
88 * @param[in] buflen length of tbuffer
90 * @return operation status
92 * @retval -1 buffer too short
93 * @retval -2 invalid partition id
95 * @see volutil_PartitionName_r
96 * @see volutil_PartitionName
97 * @see volutil_GetPartitionID
100 volutil_PartitionName2_r(afs_int32 part, char *tbuffer, size_t buflen)
105 if (part < 0 || part >= VOLMAXPARTS) {
109 tempString[1] = tempString[2] = 0;
110 strncpy(tbuffer, "/vicep", buflen);
112 tempString[0] = 'a' + part;
116 tempString[0] = i + 'a';
117 tempString[1] = (part % 26) + 'a';
119 if (strlcat(tbuffer, tempString, buflen) >= buflen) {
125 #define BAD_VID "BAD VOLUME ID"
126 #define BAD_VID_LEN (sizeof(BAD_VID))
128 * convert a partition index number into a partition name string (/vicepXX).
130 * @param[in] part partition index number
131 * @param[out] tbuffer buffer in which to store name
132 * @param[in] buflen length of tbuffer
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
139 * @note you may wish to consider using volutil_PartitionName2_r, as its
140 * error handling is more standard
142 * @see volutil_PartitionName2_r
143 * @see volutil_PartitionName
144 * @see volutil_GetPartitionID
147 volutil_PartitionName_r(int part, char *tbuffer, int buflen)
151 if (buflen < BAD_VID_LEN) {
152 strlcpy(tbuffer, "SPC", buflen);
156 code = volutil_PartitionName2_r(part, tbuffer, buflen);
159 strlcpy(tbuffer, BAD_VID, buflen);
166 * convert a partition index number into a partition name string (/vicepXX).
168 * @param[in] avalue partition index number
170 * @return partition name string
171 * @retval "BAD VOLUME ID" avalue contains an invalid partition index
173 * @warning this interface is not re-entrant
175 * @see volutil_PartitionName2_r
176 * @see volutil_PartitionName_r
177 * @see volutil_GetPartitionID
180 volutil_PartitionName(int avalue)
182 #define VPN_TBUFLEN 64
183 static char tbuffer[VPN_TBUFLEN];
184 return volutil_PartitionName_r(avalue, tbuffer, VPN_TBUFLEN - 1);
187 /* is this a digit or a digit-like thing? */
189 ismeta(int ac, int abase)
191 /* if (ac == '-' || ac == 'x' || ac == 'X') return 1; */
192 if (ac >= '0' && ac <= '7')
196 if (ac >= '8' && ac <= '9')
200 if (ac >= 'a' && ac <= 'f')
202 if (ac >= 'A' && ac <= 'F')
207 /* given that this is a digit or a digit-like thing, compute its value */
211 if (ac >= '0' && ac <= '9')
213 if (ac >= 'a' && ac <= 'f')
214 return ac - 'a' + 10;
215 if (ac >= 'A' && ac <= 'F')
216 return ac - 'A' + 10;
221 util_GetInt32(char *as, afs_int32 * aval)
228 total = 0; /* initialize things */
231 /* skip over leading spaces */
232 for (tc = *as; tc !='\0'; as++, tc = *as) {
233 if (tc != ' ' && tc != '\t')
240 as++; /* skip over character */
243 /* compute the base */
246 if (*as == 'x' || *as == 'X') {
254 /* compute the # itself */
255 for (tc = *as; tc !='\0'; as++, tc = *as) {
256 if (!ismeta(tc, base))
259 total += getmeta(tc);
270 util_GetUInt32(char *as, afs_uint32 * aval)
276 total = 0; /* initialize things */
278 /* skip over leading spaces */
279 for (tc = *as; tc !='\0'; as++, tc = *as) {
280 if (tc != ' ' && tc != '\t')
284 /* compute the base */
287 if (*as == 'x' || *as == 'X') {
295 /* compute the # itself */
296 for (tc = *as; tc !='\0'; as++, tc = *as) {
297 if (!ismeta(tc, base))
300 total += getmeta(tc);
307 static const char power_letter[] = {
315 util_GetHumanInt32(char *as, afs_int32 * aval)
323 value = strtol(as, &unit, 0);
327 for (exponent = 0; exponent < sizeof(power_letter) && power_letter[exponent] != unit[0]; exponent++) {
330 if (exponent == sizeof(power_letter))
333 if (value > MAX_AFS_INT32 / mult || value < MIN_AFS_INT32 / mult)
336 *aval = value * mult;
342 util_GetInt64(char *as, afs_int64 * aval)
349 total = 0; /* initialize things */
352 /* skip over leading spaces */
354 if (tc != ' ' && tc != '\t')
361 as++; /* skip over character */
364 /* compute the base */
367 if (*as == 'x' || *as == 'X') {
375 /* compute the # itself */
377 if (!ismeta(tc, base))
380 total += getmeta(tc);
392 util_GetUInt64(char *as, afs_uint64 * aval)
398 total = 0; /* initialize things */
400 /* skip over leading spaces */
402 if (tc != ' ' && tc != '\t')
406 /* compute the base */
409 if (*as == 'x' || *as == 'X') {
417 /* compute the # itself */
419 if (!ismeta(tc, base))
422 total += getmeta(tc);