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>
20 /* maximum number of partitions - must match vol/voldefs.h */
21 #define VOLMAXPARTS 255
24 * map a partition id from any partition-style name.
26 * @param[in] aname partition name string
28 * @return partition index number
29 * @retval -1 invalid partition name
31 * @see volutil_PartitionName2_r
32 * @see volutil_PartitionName_r
33 * @see volutil_PartitionName
36 volutil_GetPartitionID(char *aname)
44 return -1; /* unknown */
45 /* numbers go straight through */
46 if (tc >= '0' && tc <= '9') {
48 /* this next check is to make the syntax less ambiguous when discriminating
49 * between volume numbers and partition IDs. This lets things like
50 * bos salvage do some reasonability checks on its input w/o checking
51 * to see if the partition is really on the server.
53 if (temp < 0 || temp >= VOLMAXPARTS)
58 /* otherwise check for vicepa or /vicepa, or just plain "a" */
60 if (strlen(aname) <= 2) {
62 } else if (!strncmp(aname, "/vicep", 6)) {
63 strncpy(ascii, aname + 6, 2);
64 } else if (!strncmp(aname, "vicep", 5)) {
65 strncpy(ascii, aname + 5, 2);
67 return -1; /* bad partition name */
68 /* now partitions are named /vicepa ... /vicepz, /vicepaa, /vicepab, .../vicepzz,
69 * and are numbered from 0. Do the appropriate conversion */
71 /* one char name, 0..25 */
72 if (ascii[0] < 'a' || ascii[0] > 'z')
73 return -1; /* wrongo */
74 return ascii[0] - 'a';
76 /* two char name, 26 .. <whatever> */
77 if (ascii[0] < 'a' || ascii[0] > 'z')
78 return -1; /* wrongo */
79 if (ascii[1] < 'a' || ascii[1] > 'z')
80 return -1; /* just as bad */
81 temp = (ascii[0] - 'a') * 26 + (ascii[1] - 'a') + 26;
82 return (temp >= VOLMAXPARTS ? -1 : temp);
87 * convert a partition index number into a partition name string (/vicepXX).
89 * @param[in] part partition index number
90 * @param[out] tbuffer buffer in which to store name
91 * @param[in] buflen length of tbuffer
93 * @return operation status
95 * @retval -1 buffer too short
96 * @retval -2 invalid partition id
98 * @see volutil_PartitionName_r
99 * @see volutil_PartitionName
100 * @see volutil_GetPartitionID
103 volutil_PartitionName2_r(afs_int32 part, char *tbuffer, size_t buflen)
108 if (part < 0 || part >= VOLMAXPARTS) {
112 tempString[1] = tempString[2] = 0;
113 strncpy(tbuffer, "/vicep", buflen);
115 tempString[0] = 'a' + part;
119 tempString[0] = i + 'a';
120 tempString[1] = (part % 26) + 'a';
122 if (strlcat(tbuffer, tempString, buflen) >= buflen) {
128 #define BAD_VID "BAD VOLUME ID"
129 #define BAD_VID_LEN (sizeof(BAD_VID))
131 * convert a partition index number into a partition name string (/vicepXX).
133 * @param[in] part partition index number
134 * @param[out] tbuffer buffer in which to store name
135 * @param[in] buflen length of tbuffer
137 * @return partition name string
138 * @retval "" buffer too short
139 * @retval "SPC" buffer too short
140 * @retval "BAD VOLUME ID" avalue contains an invalid partition index
142 * @note you may wish to consider using volutil_PartitionName2_r, as its
143 * error handling is more standard
145 * @see volutil_PartitionName2_r
146 * @see volutil_PartitionName
147 * @see volutil_GetPartitionID
150 volutil_PartitionName_r(int part, char *tbuffer, int buflen)
154 if (buflen < BAD_VID_LEN) {
155 strlcpy(tbuffer, "SPC", buflen);
159 code = volutil_PartitionName2_r(part, tbuffer, buflen);
162 strlcpy(tbuffer, BAD_VID, buflen);
169 * convert a partition index number into a partition name string (/vicepXX).
171 * @param[in] avalue partition index number
173 * @return partition name string
174 * @retval "BAD VOLUME ID" avalue contains an invalid partition index
176 * @warning this interface is not re-entrant
178 * @see volutil_PartitionName2_r
179 * @see volutil_PartitionName_r
180 * @see volutil_GetPartitionID
183 volutil_PartitionName(int avalue)
185 #define VPN_TBUFLEN 64
186 static char tbuffer[VPN_TBUFLEN];
187 return volutil_PartitionName_r(avalue, tbuffer, VPN_TBUFLEN - 1);
190 /* is this a digit or a digit-like thing? */
192 ismeta(register int ac, register int abase)
194 /* if (ac == '-' || ac == 'x' || ac == 'X') return 1; */
195 if (ac >= '0' && ac <= '7')
199 if (ac >= '8' && ac <= '9')
203 if (ac >= 'a' && ac <= 'f')
205 if (ac >= 'A' && ac <= 'F')
210 /* given that this is a digit or a digit-like thing, compute its value */
212 getmeta(register int ac)
214 if (ac >= '0' && ac <= '9')
216 if (ac >= 'a' && ac <= 'f')
217 return ac - 'a' + 10;
218 if (ac >= 'A' && ac <= 'F')
219 return ac - 'A' + 10;
224 util_GetInt32(register char *as, afs_int32 * aval)
226 register afs_int32 total;
231 total = 0; /* initialize things */
234 /* skip over leading spaces */
235 for (tc = *as; tc !='\0'; as++, tc = *as) {
236 if (tc != ' ' && tc != '\t')
243 as++; /* skip over character */
246 /* compute the base */
249 if (*as == 'x' || *as == 'X') {
257 /* compute the # itself */
258 for (tc = *as; tc !='\0'; as++, tc = *as) {
259 if (!ismeta(tc, base))
262 total += getmeta(tc);
273 util_GetUInt32(register char *as, afs_uint32 * aval)
275 register afs_uint32 total;
279 total = 0; /* initialize things */
281 /* skip over leading spaces */
282 for (tc = *as; tc !='\0'; as++, tc = *as) {
283 if (tc != ' ' && tc != '\t')
287 /* compute the base */
290 if (*as == 'x' || *as == 'X') {
298 /* compute the # itself */
299 for (tc = *as; tc !='\0'; as++, tc = *as) {
300 if (!ismeta(tc, base))
303 total += getmeta(tc);