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>
21 /* maximum number of partitions - must match vol/voldefs.h */
22 #define VOLMAXPARTS 255
25 * map a partition id from any partition-style name.
27 * @param[in] aname partition name string
29 * @return partition index number
30 * @retval -1 invalid partition name
32 * @see volutil_PartitionName2_r
33 * @see volutil_PartitionName_r
34 * @see volutil_PartitionName
37 volutil_GetPartitionID(char *aname)
45 return -1; /* unknown */
46 /* numbers go straight through */
47 if (tc >= '0' && tc <= '9') {
49 /* this next check is to make the syntax less ambiguous when discriminating
50 * between volume numbers and partition IDs. This lets things like
51 * bos salvage do some reasonability checks on its input w/o checking
52 * to see if the partition is really on the server.
54 if (temp < 0 || temp >= VOLMAXPARTS)
59 /* otherwise check for vicepa or /vicepa, or just plain "a" */
61 if (strlen(aname) <= 2) {
63 } else if (!strncmp(aname, "/vicep", 6)) {
64 strncpy(ascii, aname + 6, 2);
65 } else if (!strncmp(aname, "vicep", 5)) {
66 strncpy(ascii, aname + 5, 2);
68 return -1; /* bad partition name */
69 /* now partitions are named /vicepa ... /vicepz, /vicepaa, /vicepab, .../vicepzz,
70 * and are numbered from 0. Do the appropriate conversion */
72 /* one char name, 0..25 */
73 if (ascii[0] < 'a' || ascii[0] > 'z')
74 return -1; /* wrongo */
75 return ascii[0] - 'a';
77 /* two char name, 26 .. <whatever> */
78 if (ascii[0] < 'a' || ascii[0] > 'z')
79 return -1; /* wrongo */
80 if (ascii[1] < 'a' || ascii[1] > 'z')
81 return -1; /* just as bad */
82 temp = (ascii[0] - 'a') * 26 + (ascii[1] - 'a') + 26;
83 return (temp >= VOLMAXPARTS ? -1 : temp);
88 * convert a partition index number into a partition name string (/vicepXX).
90 * @param[in] part partition index number
91 * @param[out] tbuffer buffer in which to store name
92 * @param[in] buflen length of tbuffer
94 * @return operation status
96 * @retval -1 buffer too short
97 * @retval -2 invalid partition id
99 * @see volutil_PartitionName_r
100 * @see volutil_PartitionName
101 * @see volutil_GetPartitionID
104 volutil_PartitionName2_r(afs_int32 part, char *tbuffer, size_t buflen)
109 if (part < 0 || part >= VOLMAXPARTS) {
113 tempString[1] = tempString[2] = 0;
114 strncpy(tbuffer, "/vicep", buflen);
116 tempString[0] = 'a' + part;
120 tempString[0] = i + 'a';
121 tempString[1] = (part % 26) + 'a';
123 if (strlcat(tbuffer, tempString, buflen) >= buflen) {
129 #define BAD_VID "BAD VOLUME ID"
130 #define BAD_VID_LEN (sizeof(BAD_VID))
132 * convert a partition index number into a partition name string (/vicepXX).
134 * @param[in] part partition index number
135 * @param[out] tbuffer buffer in which to store name
136 * @param[in] buflen length of tbuffer
138 * @return partition name string
139 * @retval "" buffer too short
140 * @retval "SPC" buffer too short
141 * @retval "BAD VOLUME ID" avalue contains an invalid partition index
143 * @note you may wish to consider using volutil_PartitionName2_r, as its
144 * error handling is more standard
146 * @see volutil_PartitionName2_r
147 * @see volutil_PartitionName
148 * @see volutil_GetPartitionID
151 volutil_PartitionName_r(int part, char *tbuffer, int buflen)
155 if (buflen < BAD_VID_LEN) {
156 strlcpy(tbuffer, "SPC", buflen);
160 code = volutil_PartitionName2_r(part, tbuffer, buflen);
163 strlcpy(tbuffer, BAD_VID, buflen);
170 * convert a partition index number into a partition name string (/vicepXX).
172 * @param[in] avalue partition index number
174 * @return partition name string
175 * @retval "BAD VOLUME ID" avalue contains an invalid partition index
177 * @warning this interface is not re-entrant
179 * @see volutil_PartitionName2_r
180 * @see volutil_PartitionName_r
181 * @see volutil_GetPartitionID
184 volutil_PartitionName(int avalue)
186 #define VPN_TBUFLEN 64
187 static char tbuffer[VPN_TBUFLEN];
188 return volutil_PartitionName_r(avalue, tbuffer, VPN_TBUFLEN - 1);
191 /* is this a digit or a digit-like thing? */
193 ismeta(register int ac, register int abase)
195 /* if (ac == '-' || ac == 'x' || ac == 'X') return 1; */
196 if (ac >= '0' && ac <= '7')
200 if (ac >= '8' && ac <= '9')
204 if (ac >= 'a' && ac <= 'f')
206 if (ac >= 'A' && ac <= 'F')
211 /* given that this is a digit or a digit-like thing, compute its value */
213 getmeta(register int ac)
215 if (ac >= '0' && ac <= '9')
217 if (ac >= 'a' && ac <= 'f')
218 return ac - 'a' + 10;
219 if (ac >= 'A' && ac <= 'F')
220 return ac - 'A' + 10;
225 util_GetInt32(register char *as, afs_int32 * aval)
227 register afs_int32 total;
232 total = 0; /* initialize things */
235 /* skip over leading spaces */
236 for (tc = *as; tc !='\0'; as++, tc = *as) {
237 if (tc != ' ' && tc != '\t')
244 as++; /* skip over character */
247 /* compute the base */
250 if (*as == 'x' || *as == 'X') {
258 /* compute the # itself */
259 for (tc = *as; tc !='\0'; as++, tc = *as) {
260 if (!ismeta(tc, base))
263 total += getmeta(tc);
274 util_GetUInt32(register char *as, afs_uint32 * aval)
276 register afs_uint32 total;
280 total = 0; /* initialize things */
282 /* skip over leading spaces */
283 for (tc = *as; tc !='\0'; as++, tc = *as) {
284 if (tc != ' ' && tc != '\t')
288 /* compute the base */
291 if (*as == 'x' || *as == 'X') {
299 /* compute the # itself */
300 for (tc = *as; tc !='\0'; as++, tc = *as) {
301 if (!ismeta(tc, base))
304 total += getmeta(tc);
311 static const char power_letter[] = {
319 util_GetHumanInt32(register char *as, afs_int32 * aval)
327 value = strtol(as, &unit, 0);
331 for (exponent = 0; exponent < sizeof(power_letter) && power_letter[exponent] != unit[0]; exponent++) {
334 if (exponent == sizeof(power_letter))
337 if (value > MAX_AFS_INT32 / mult || value < MIN_AFS_INT32 / mult)
340 *aval = value * mult;