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>
23 /* maximum number of partitions - must match vol/voldefs.h */
24 #define VOLMAXPARTS 255
27 * map a partition id from any partition-style name.
29 * @param[in] aname partition name string
31 * @return partition index number
32 * @retval -1 invalid partition name
34 * @see volutil_PartitionName2_r
35 * @see volutil_PartitionName_r
36 * @see volutil_PartitionName
39 volutil_GetPartitionID(char *aname)
47 return -1; /* unknown */
48 /* numbers go straight through */
49 if (tc >= '0' && tc <= '9') {
51 /* this next check is to make the syntax less ambiguous when discriminating
52 * between volume numbers and partition IDs. This lets things like
53 * bos salvage do some reasonability checks on its input w/o checking
54 * to see if the partition is really on the server.
56 if (temp < 0 || temp >= VOLMAXPARTS)
61 /* otherwise check for vicepa or /vicepa, or just plain "a" */
63 if (strlen(aname) <= 2) {
65 } else if (!strncmp(aname, "/vicep", 6)) {
66 strncpy(ascii, aname + 6, 2);
67 } else if (!strncmp(aname, "vicep", 5)) {
68 strncpy(ascii, aname + 5, 2);
70 return -1; /* bad partition name */
71 /* now partitions are named /vicepa ... /vicepz, /vicepaa, /vicepab, .../vicepzz,
72 * and are numbered from 0. Do the appropriate conversion */
74 /* one char name, 0..25 */
75 if (ascii[0] < 'a' || ascii[0] > 'z')
76 return -1; /* wrongo */
77 return ascii[0] - 'a';
79 /* two char name, 26 .. <whatever> */
80 if (ascii[0] < 'a' || ascii[0] > 'z')
81 return -1; /* wrongo */
82 if (ascii[1] < 'a' || ascii[1] > 'z')
83 return -1; /* just as bad */
84 temp = (ascii[0] - 'a') * 26 + (ascii[1] - 'a') + 26;
85 return (temp >= VOLMAXPARTS ? -1 : temp);
90 * convert a partition index number into a partition name string (/vicepXX).
92 * @param[in] part partition index number
93 * @param[out] tbuffer buffer in which to store name
94 * @param[in] buflen length of tbuffer
96 * @return operation status
98 * @retval -1 buffer too short
99 * @retval -2 invalid partition id
101 * @see volutil_PartitionName_r
102 * @see volutil_PartitionName
103 * @see volutil_GetPartitionID
106 volutil_PartitionName2_r(afs_int32 part, char *tbuffer, size_t buflen)
111 if (part < 0 || part >= VOLMAXPARTS) {
115 tempString[1] = tempString[2] = 0;
116 strncpy(tbuffer, "/vicep", buflen);
118 tempString[0] = 'a' + part;
122 tempString[0] = i + 'a';
123 tempString[1] = (part % 26) + 'a';
125 if (strlcat(tbuffer, tempString, buflen) >= buflen) {
131 #define BAD_VID "BAD VOLUME ID"
132 #define BAD_VID_LEN (sizeof(BAD_VID))
134 * convert a partition index number into a partition name string (/vicepXX).
136 * @param[in] part partition index number
137 * @param[out] tbuffer buffer in which to store name
138 * @param[in] buflen length of tbuffer
140 * @return partition name string
141 * @retval "" buffer too short
142 * @retval "SPC" buffer too short
143 * @retval "BAD VOLUME ID" avalue contains an invalid partition index
145 * @note you may wish to consider using volutil_PartitionName2_r, as its
146 * error handling is more standard
148 * @see volutil_PartitionName2_r
149 * @see volutil_PartitionName
150 * @see volutil_GetPartitionID
153 volutil_PartitionName_r(int part, char *tbuffer, int buflen)
157 if (buflen < BAD_VID_LEN) {
158 strlcpy(tbuffer, "SPC", buflen);
162 code = volutil_PartitionName2_r(part, tbuffer, buflen);
165 strlcpy(tbuffer, BAD_VID, buflen);
172 * convert a partition index number into a partition name string (/vicepXX).
174 * @param[in] avalue partition index number
176 * @return partition name string
177 * @retval "BAD VOLUME ID" avalue contains an invalid partition index
179 * @warning this interface is not re-entrant
181 * @see volutil_PartitionName2_r
182 * @see volutil_PartitionName_r
183 * @see volutil_GetPartitionID
186 volutil_PartitionName(int avalue)
188 #define VPN_TBUFLEN 64
189 static char tbuffer[VPN_TBUFLEN];
190 return volutil_PartitionName_r(avalue, tbuffer, VPN_TBUFLEN - 1);
193 /* is this a digit or a digit-like thing? */
195 ismeta(int ac, int abase)
197 /* if (ac == '-' || ac == 'x' || ac == 'X') return 1; */
198 if (ac >= '0' && ac <= '7')
202 if (ac >= '8' && ac <= '9')
206 if (ac >= 'a' && ac <= 'f')
208 if (ac >= 'A' && ac <= 'F')
213 /* given that this is a digit or a digit-like thing, compute its value */
217 if (ac >= '0' && ac <= '9')
219 if (ac >= 'a' && ac <= 'f')
220 return ac - 'a' + 10;
221 if (ac >= 'A' && ac <= 'F')
222 return ac - 'A' + 10;
227 util_GetInt32(char *as, afs_int32 * aval)
234 total = 0; /* initialize things */
237 /* skip over leading spaces */
238 for (tc = *as; tc !='\0'; as++, tc = *as) {
239 if (tc != ' ' && tc != '\t')
246 as++; /* skip over character */
249 /* compute the base */
252 if (*as == 'x' || *as == 'X') {
260 /* compute the # itself */
261 for (tc = *as; tc !='\0'; as++, tc = *as) {
262 if (!ismeta(tc, base))
265 total += getmeta(tc);
276 util_GetUInt32(char *as, afs_uint32 * aval)
282 total = 0; /* initialize things */
284 /* skip over leading spaces */
285 for (tc = *as; tc !='\0'; as++, tc = *as) {
286 if (tc != ' ' && tc != '\t')
290 /* compute the base */
293 if (*as == 'x' || *as == 'X') {
301 /* compute the # itself */
302 for (tc = *as; tc !='\0'; as++, tc = *as) {
303 if (!ismeta(tc, base))
306 total += getmeta(tc);
313 static const char power_letter[] = {
321 util_GetHumanInt32(char *as, afs_int32 * aval)
329 value = strtol(as, &unit, 0);
333 for (exponent = 0; exponent < sizeof(power_letter) && power_letter[exponent] != unit[0]; exponent++) {
336 if (exponent == sizeof(power_letter))
339 if (value > MAX_AFS_INT32 / mult || value < MIN_AFS_INT32 / mult)
342 *aval = value * mult;
348 util_GetInt64(char *as, afs_int64 * aval)
355 total = 0; /* initialize things */
358 /* skip over leading spaces */
360 if (tc != ' ' && tc != '\t')
367 as++; /* skip over character */
370 /* compute the base */
373 if (*as == 'x' || *as == 'X') {
381 /* compute the # itself */
383 if (!ismeta(tc, base))
386 total += getmeta(tc);
398 util_GetUInt64(char *as, afs_uint64 * aval)
404 total = 0; /* initialize things */
406 /* skip over leading spaces */
408 if (tc != ' ' && tc != '\t')
412 /* compute the base */
415 if (*as == 'x' || *as == 'X') {
423 /* compute the # itself */
425 if (!ismeta(tc, base))
428 total += getmeta(tc);