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 if(strlcpy(ascii, aname + 6, sizeof(ascii)) >= sizeof(ascii))
61 return -1; /* bad partition name: trailing characters */
62 } else if (!strncmp(aname, "vicep", 5)) {
63 if(strlcpy(ascii, aname + 5, sizeof(ascii)) >= sizeof(ascii))
64 return -1; /* bad partition name: trailing characters */
66 return -1; /* bad partition name */
67 /* now partitions are named /vicepa ... /vicepz, /vicepaa, /vicepab, .../vicepzz,
68 * and are numbered from 0. Do the appropriate conversion */
70 /* one char name, 0..25 */
71 if (ascii[0] < 'a' || ascii[0] > 'z')
72 return -1; /* wrongo */
73 return ascii[0] - 'a';
75 /* two char name, 26 .. <whatever> */
76 if (ascii[0] < 'a' || ascii[0] > 'z')
77 return -1; /* wrongo */
78 if (ascii[1] < 'a' || ascii[1] > 'z')
79 return -1; /* just as bad */
80 temp = (ascii[0] - 'a') * 26 + (ascii[1] - 'a') + 26;
81 return (temp >= VOLMAXPARTS ? -1 : temp);
86 * convert a partition index number into a partition name string (/vicepXX).
88 * @param[in] part partition index number
89 * @param[out] tbuffer buffer in which to store name
90 * @param[in] buflen length of tbuffer
92 * @return operation status
94 * @retval -1 buffer too short
95 * @retval -2 invalid partition id
97 * @see volutil_PartitionName_r
98 * @see volutil_PartitionName
99 * @see volutil_GetPartitionID
102 volutil_PartitionName2_r(afs_int32 part, char *tbuffer, size_t buflen)
107 if (part < 0 || part >= VOLMAXPARTS) {
111 tempString[1] = tempString[2] = 0;
112 strncpy(tbuffer, "/vicep", buflen);
114 tempString[0] = 'a' + part;
118 tempString[0] = i + 'a';
119 tempString[1] = (part % 26) + 'a';
121 if (strlcat(tbuffer, tempString, buflen) >= buflen) {
127 #define BAD_VID "BAD VOLUME ID"
128 #define BAD_VID_LEN (sizeof(BAD_VID))
130 * convert a partition index number into a partition name string (/vicepXX).
132 * @param[in] part partition index number
133 * @param[out] tbuffer buffer in which to store name
134 * @param[in] buflen length of tbuffer
136 * @return partition name string
137 * @retval "" buffer too short
138 * @retval "SPC" buffer too short
139 * @retval "BAD VOLUME ID" avalue contains an invalid partition index
141 * @note you may wish to consider using volutil_PartitionName2_r, as its
142 * error handling is more standard
144 * @see volutil_PartitionName2_r
145 * @see volutil_PartitionName
146 * @see volutil_GetPartitionID
149 volutil_PartitionName_r(int part, char *tbuffer, int buflen)
153 if (buflen < BAD_VID_LEN) {
154 strlcpy(tbuffer, "SPC", buflen);
158 code = volutil_PartitionName2_r(part, tbuffer, buflen);
161 strlcpy(tbuffer, BAD_VID, buflen);
168 * convert a partition index number into a partition name string (/vicepXX).
170 * @param[in] avalue partition index number
172 * @return partition name string
173 * @retval "BAD VOLUME ID" avalue contains an invalid partition index
175 * @warning this interface is not re-entrant
177 * @see volutil_PartitionName2_r
178 * @see volutil_PartitionName_r
179 * @see volutil_GetPartitionID
182 volutil_PartitionName(int avalue)
184 #define VPN_TBUFLEN 64
185 static char tbuffer[VPN_TBUFLEN];
186 return volutil_PartitionName_r(avalue, tbuffer, VPN_TBUFLEN - 1);
189 /* is this a digit or a digit-like thing? */
191 ismeta(int ac, int abase)
193 /* if (ac == '-' || ac == 'x' || ac == 'X') return 1; */
194 if (ac >= '0' && ac <= '7')
198 if (ac >= '8' && ac <= '9')
202 if (ac >= 'a' && ac <= 'f')
204 if (ac >= 'A' && ac <= 'F')
209 /* given that this is a digit or a digit-like thing, compute its value */
213 if (ac >= '0' && ac <= '9')
215 if (ac >= 'a' && ac <= 'f')
216 return ac - 'a' + 10;
217 if (ac >= 'A' && ac <= 'F')
218 return ac - 'A' + 10;
223 util_GetInt32(char *as, afs_int32 * aval)
230 total = 0; /* initialize things */
233 /* skip over leading spaces */
234 for (tc = *as; tc !='\0'; as++, tc = *as) {
235 if (tc != ' ' && tc != '\t')
242 as++; /* skip over character */
245 /* compute the base */
248 if (*as == 'x' || *as == 'X') {
256 /* compute the # itself */
257 for (tc = *as; tc !='\0'; as++, tc = *as) {
258 if (!ismeta(tc, base))
261 total += getmeta(tc);
272 util_GetUInt32(char *as, afs_uint32 * aval)
278 total = 0; /* initialize things */
280 /* skip over leading spaces */
281 for (tc = *as; tc !='\0'; as++, tc = *as) {
282 if (tc != ' ' && tc != '\t')
286 /* compute the base */
289 if (*as == 'x' || *as == 'X') {
297 /* compute the # itself */
298 for (tc = *as; tc !='\0'; as++, tc = *as) {
299 if (!ismeta(tc, base))
302 total += getmeta(tc);
309 static const char power_letter[] = {
317 util_GetHumanInt32(char *as, afs_int32 * aval)
325 value = strtol(as, &unit, 0);
329 for (exponent = 0; exponent < sizeof(power_letter) && power_letter[exponent] != unit[0]; exponent++) {
332 if (exponent == sizeof(power_letter))
335 if (value > MAX_AFS_INT32 / mult || value < MIN_AFS_INT32 / mult)
338 *aval = value * mult;
344 util_GetInt64(char *as, afs_int64 * aval)
351 total = 0; /* initialize things */
354 /* skip over leading spaces */
356 if (tc != ' ' && tc != '\t')
363 as++; /* skip over character */
366 /* compute the base */
369 if (*as == 'x' || *as == 'X') {
377 /* compute the # itself */
379 if (!ismeta(tc, base))
382 total += getmeta(tc);
394 util_GetUInt64(char *as, afs_uint64 * aval)
400 total = 0; /* initialize things */
402 /* skip over leading spaces */
404 if (tc != ' ' && tc != '\t')
408 /* compute the base */
411 if (*as == 'x' || *as == 'X') {
419 /* compute the # itself */
421 if (!ismeta(tc, base))
424 total += getmeta(tc);