getcap.c revision 1.35 1 1.35 groo /* $NetBSD: getcap.c,v 1.35 2002/04/16 19:07:57 groo Exp $ */
2 1.9 cgd
3 1.1 cgd /*-
4 1.9 cgd * Copyright (c) 1992, 1993
5 1.9 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Casey Leedom of Lawrence Livermore National Laboratory.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.1 cgd * 3. All advertising materials mentioning features or use of this software
19 1.1 cgd * must display the following acknowledgement:
20 1.1 cgd * This product includes software developed by the University of
21 1.1 cgd * California, Berkeley and its contributors.
22 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
23 1.1 cgd * may be used to endorse or promote products derived from this software
24 1.1 cgd * without specific prior written permission.
25 1.1 cgd *
26 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 cgd * SUCH DAMAGE.
37 1.1 cgd */
38 1.1 cgd
39 1.13 christos #include <sys/cdefs.h>
40 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
41 1.9 cgd #if 0
42 1.9 cgd static char sccsid[] = "@(#)getcap.c 8.3 (Berkeley) 3/25/94";
43 1.9 cgd #else
44 1.35 groo __RCSID("$NetBSD: getcap.c,v 1.35 2002/04/16 19:07:57 groo Exp $");
45 1.9 cgd #endif
46 1.1 cgd #endif /* LIBC_SCCS and not lint */
47 1.1 cgd
48 1.14 jtc #include "namespace.h"
49 1.1 cgd #include <sys/types.h>
50 1.30 lukem
51 1.30 lukem #include <assert.h>
52 1.1 cgd #include <ctype.h>
53 1.1 cgd #include <db.h>
54 1.1 cgd #include <errno.h>
55 1.1 cgd #include <fcntl.h>
56 1.1 cgd #include <limits.h>
57 1.1 cgd #include <stdio.h>
58 1.1 cgd #include <stdlib.h>
59 1.1 cgd #include <string.h>
60 1.1 cgd #include <unistd.h>
61 1.14 jtc
62 1.14 jtc #ifdef __weak_alias
63 1.32 mycroft __weak_alias(cgetcap,_cgetcap)
64 1.32 mycroft __weak_alias(cgetclose,_cgetclose)
65 1.32 mycroft __weak_alias(cgetent,_cgetent)
66 1.32 mycroft __weak_alias(cgetfirst,_cgetfirst)
67 1.32 mycroft __weak_alias(cgetmatch,_cgetmatch)
68 1.32 mycroft __weak_alias(cgetnext,_cgetnext)
69 1.32 mycroft __weak_alias(cgetnum,_cgetnum)
70 1.32 mycroft __weak_alias(cgetset,_cgetset)
71 1.32 mycroft __weak_alias(cgetstr,_cgetstr)
72 1.32 mycroft __weak_alias(cgetustr,_cgetustr)
73 1.14 jtc #endif
74 1.1 cgd
75 1.1 cgd #define BFRAG 1024
76 1.1 cgd #define BSIZE 1024
77 1.1 cgd #define ESC ('[' & 037) /* ASCII ESC */
78 1.1 cgd #define MAX_RECURSION 32 /* maximum getent recursion */
79 1.1 cgd #define SFRAG 100 /* cgetstr mallocs in SFRAG chunks */
80 1.1 cgd
81 1.1 cgd #define RECOK (char)0
82 1.1 cgd #define TCERR (char)1
83 1.1 cgd #define SHADOW (char)2
84 1.1 cgd
85 1.1 cgd static size_t topreclen; /* toprec length */
86 1.1 cgd static char *toprec; /* Additional record specified by cgetset() */
87 1.1 cgd static int gottoprec; /* Flag indicating retrieval of toprecord */
88 1.1 cgd
89 1.21 mycroft static int cdbget __P((DB *, char **, const char *));
90 1.21 mycroft static int getent __P((char **, size_t *, char **, int, const char *, int, char *));
91 1.1 cgd static int nfcmp __P((char *, char *));
92 1.1 cgd
93 1.1 cgd /*
94 1.1 cgd * Cgetset() allows the addition of a user specified buffer to be added
95 1.1 cgd * to the database array, in effect "pushing" the buffer on top of the
96 1.1 cgd * virtual database. 0 is returned on success, -1 on failure.
97 1.1 cgd */
98 1.1 cgd int
99 1.1 cgd cgetset(ent)
100 1.21 mycroft const char *ent;
101 1.1 cgd {
102 1.27 abs const char *source, *check;
103 1.27 abs char *dest;
104 1.27 abs
105 1.1 cgd if (ent == NULL) {
106 1.1 cgd if (toprec)
107 1.1 cgd free(toprec);
108 1.1 cgd toprec = NULL;
109 1.1 cgd topreclen = 0;
110 1.1 cgd return (0);
111 1.1 cgd }
112 1.1 cgd topreclen = strlen(ent);
113 1.1 cgd if ((toprec = malloc (topreclen + 1)) == NULL) {
114 1.1 cgd errno = ENOMEM;
115 1.1 cgd return (-1);
116 1.1 cgd }
117 1.1 cgd gottoprec = 0;
118 1.27 abs
119 1.27 abs source=ent;
120 1.27 abs dest=toprec;
121 1.27 abs while (*source) { /* Strip whitespace */
122 1.27 abs *dest++ = *source++; /* Do not check first field */
123 1.27 abs while (*source == ':') {
124 1.27 abs check=source+1;
125 1.29 abs while (*check && (isspace((unsigned char)*check) ||
126 1.29 abs (*check=='\\' && isspace((unsigned char)check[1]))))
127 1.27 abs ++check;
128 1.27 abs if( *check == ':' )
129 1.27 abs source=check;
130 1.27 abs else
131 1.27 abs break;
132 1.27 abs
133 1.27 abs }
134 1.27 abs }
135 1.27 abs *dest=0;
136 1.27 abs
137 1.1 cgd return (0);
138 1.1 cgd }
139 1.1 cgd
140 1.1 cgd /*
141 1.1 cgd * Cgetcap searches the capability record buf for the capability cap with
142 1.1 cgd * type `type'. A pointer to the value of cap is returned on success, NULL
143 1.1 cgd * if the requested capability couldn't be found.
144 1.1 cgd *
145 1.1 cgd * Specifying a type of ':' means that nothing should follow cap (:cap:).
146 1.1 cgd * In this case a pointer to the terminating ':' or NUL will be returned if
147 1.1 cgd * cap is found.
148 1.1 cgd *
149 1.1 cgd * If (cap, '@') or (cap, terminator, '@') is found before (cap, terminator)
150 1.1 cgd * return NULL.
151 1.1 cgd */
152 1.1 cgd char *
153 1.1 cgd cgetcap(buf, cap, type)
154 1.21 mycroft char *buf;
155 1.21 mycroft const char *cap;
156 1.1 cgd int type;
157 1.1 cgd {
158 1.21 mycroft char *bp;
159 1.21 mycroft const char *cp;
160 1.1 cgd
161 1.30 lukem _DIAGASSERT(buf != NULL);
162 1.30 lukem _DIAGASSERT(cap != NULL);
163 1.30 lukem
164 1.1 cgd bp = buf;
165 1.1 cgd for (;;) {
166 1.1 cgd /*
167 1.1 cgd * Skip past the current capability field - it's either the
168 1.1 cgd * name field if this is the first time through the loop, or
169 1.1 cgd * the remainder of a field whose name failed to match cap.
170 1.1 cgd */
171 1.1 cgd for (;;)
172 1.1 cgd if (*bp == '\0')
173 1.1 cgd return (NULL);
174 1.1 cgd else
175 1.1 cgd if (*bp++ == ':')
176 1.1 cgd break;
177 1.1 cgd
178 1.1 cgd /*
179 1.1 cgd * Try to match (cap, type) in buf.
180 1.1 cgd */
181 1.1 cgd for (cp = cap; *cp == *bp && *bp != '\0'; cp++, bp++)
182 1.1 cgd continue;
183 1.1 cgd if (*cp != '\0')
184 1.1 cgd continue;
185 1.1 cgd if (*bp == '@')
186 1.1 cgd return (NULL);
187 1.1 cgd if (type == ':') {
188 1.1 cgd if (*bp != '\0' && *bp != ':')
189 1.1 cgd continue;
190 1.1 cgd return(bp);
191 1.1 cgd }
192 1.1 cgd if (*bp != type)
193 1.1 cgd continue;
194 1.1 cgd bp++;
195 1.1 cgd return (*bp == '@' ? NULL : bp);
196 1.1 cgd }
197 1.1 cgd /* NOTREACHED */
198 1.1 cgd }
199 1.1 cgd
200 1.1 cgd /*
201 1.1 cgd * Cgetent extracts the capability record name from the NULL terminated file
202 1.1 cgd * array db_array and returns a pointer to a malloc'd copy of it in buf.
203 1.1 cgd * Buf must be retained through all subsequent calls to cgetcap, cgetnum,
204 1.1 cgd * cgetflag, and cgetstr, but may then be free'd. 0 is returned on success,
205 1.1 cgd * -1 if the requested record couldn't be found, -2 if a system error was
206 1.1 cgd * encountered (couldn't open/read a file, etc.), and -3 if a potential
207 1.1 cgd * reference loop is detected.
208 1.1 cgd */
209 1.1 cgd int
210 1.1 cgd cgetent(buf, db_array, name)
211 1.21 mycroft char **buf, **db_array;
212 1.21 mycroft const char *name;
213 1.1 cgd {
214 1.18 thorpej size_t dummy;
215 1.1 cgd
216 1.30 lukem _DIAGASSERT(buf != NULL);
217 1.30 lukem _DIAGASSERT(db_array != NULL);
218 1.30 lukem _DIAGASSERT(name != NULL);
219 1.30 lukem
220 1.1 cgd return (getent(buf, &dummy, db_array, -1, name, 0, NULL));
221 1.1 cgd }
222 1.1 cgd
223 1.1 cgd /*
224 1.1 cgd * Getent implements the functions of cgetent. If fd is non-negative,
225 1.1 cgd * *db_array has already been opened and fd is the open file descriptor. We
226 1.1 cgd * do this to save time and avoid using up file descriptors for tc=
227 1.1 cgd * recursions.
228 1.1 cgd *
229 1.1 cgd * Getent returns the same success/failure codes as cgetent. On success, a
230 1.1 cgd * pointer to a malloc'ed capability record with all tc= capabilities fully
231 1.1 cgd * expanded and its length (not including trailing ASCII NUL) are left in
232 1.1 cgd * *cap and *len.
233 1.1 cgd *
234 1.1 cgd * Basic algorithm:
235 1.1 cgd * + Allocate memory incrementally as needed in chunks of size BFRAG
236 1.1 cgd * for capability buffer.
237 1.1 cgd * + Recurse for each tc=name and interpolate result. Stop when all
238 1.1 cgd * names interpolated, a name can't be found, or depth exceeds
239 1.1 cgd * MAX_RECURSION.
240 1.1 cgd */
241 1.1 cgd static int
242 1.1 cgd getent(cap, len, db_array, fd, name, depth, nfield)
243 1.21 mycroft char **cap, **db_array, *nfield;
244 1.21 mycroft const char *name;
245 1.17 perry size_t *len;
246 1.1 cgd int fd, depth;
247 1.1 cgd {
248 1.1 cgd DB *capdbp;
249 1.16 perry char *r_end, *rp = NULL, **db_p; /* pacify gcc */
250 1.17 perry int myfd = 0, eof, foundit, retval;
251 1.17 perry size_t clen;
252 1.33 itojun char *record, *cbuf, *newrecord;
253 1.1 cgd int tc_not_resolved;
254 1.1 cgd char pbuf[_POSIX_PATH_MAX];
255 1.1 cgd
256 1.30 lukem _DIAGASSERT(cap != NULL);
257 1.30 lukem _DIAGASSERT(len != NULL);
258 1.30 lukem _DIAGASSERT(db_array != NULL);
259 1.30 lukem /* fd may be -1 */
260 1.30 lukem _DIAGASSERT(name != NULL);
261 1.30 lukem /* nfield may be NULL */
262 1.30 lukem
263 1.1 cgd /*
264 1.1 cgd * Return with ``loop detected'' error if we've recursed more than
265 1.1 cgd * MAX_RECURSION times.
266 1.1 cgd */
267 1.1 cgd if (depth > MAX_RECURSION)
268 1.1 cgd return (-3);
269 1.1 cgd
270 1.1 cgd /*
271 1.1 cgd * Check if we have a top record from cgetset().
272 1.1 cgd */
273 1.1 cgd if (depth == 0 && toprec != NULL && cgetmatch(toprec, name) == 0) {
274 1.1 cgd if ((record = malloc (topreclen + BFRAG)) == NULL) {
275 1.1 cgd errno = ENOMEM;
276 1.1 cgd return (-2);
277 1.1 cgd }
278 1.11 mrg (void)strcpy(record, toprec); /* XXX: strcpy is safe */
279 1.1 cgd db_p = db_array;
280 1.1 cgd rp = record + topreclen + 1;
281 1.1 cgd r_end = rp + BFRAG;
282 1.1 cgd goto tc_exp;
283 1.1 cgd }
284 1.1 cgd /*
285 1.1 cgd * Allocate first chunk of memory.
286 1.1 cgd */
287 1.1 cgd if ((record = malloc(BFRAG)) == NULL) {
288 1.1 cgd errno = ENOMEM;
289 1.1 cgd return (-2);
290 1.1 cgd }
291 1.1 cgd r_end = record + BFRAG;
292 1.1 cgd foundit = 0;
293 1.1 cgd /*
294 1.1 cgd * Loop through database array until finding the record.
295 1.1 cgd */
296 1.1 cgd
297 1.1 cgd for (db_p = db_array; *db_p != NULL; db_p++) {
298 1.1 cgd eof = 0;
299 1.1 cgd
300 1.1 cgd /*
301 1.1 cgd * Open database if not already open.
302 1.1 cgd */
303 1.1 cgd
304 1.1 cgd if (fd >= 0) {
305 1.15 kleink (void)lseek(fd, (off_t)0, SEEK_SET);
306 1.1 cgd } else {
307 1.1 cgd (void)snprintf(pbuf, sizeof(pbuf), "%s.db", *db_p);
308 1.1 cgd if ((capdbp = dbopen(pbuf, O_RDONLY, 0, DB_HASH, 0))
309 1.1 cgd != NULL) {
310 1.1 cgd free(record);
311 1.1 cgd retval = cdbget(capdbp, &record, name);
312 1.8 cgd if (retval < 0) {
313 1.8 cgd /* no record available */
314 1.8 cgd (void)capdbp->close(capdbp);
315 1.9 cgd return (retval);
316 1.8 cgd }
317 1.8 cgd /* save the data; close frees it */
318 1.7 cgd clen = strlen(record);
319 1.8 cgd cbuf = malloc(clen + 1);
320 1.23 perry memmove(cbuf, record, clen + 1);
321 1.7 cgd if (capdbp->close(capdbp) < 0) {
322 1.30 lukem int serrno = errno;
323 1.30 lukem
324 1.7 cgd free(cbuf);
325 1.30 lukem errno = serrno;
326 1.7 cgd return (-2);
327 1.7 cgd }
328 1.7 cgd *len = clen;
329 1.7 cgd *cap = cbuf;
330 1.1 cgd return (retval);
331 1.1 cgd } else {
332 1.1 cgd fd = open(*db_p, O_RDONLY, 0);
333 1.1 cgd if (fd < 0) {
334 1.1 cgd /* No error on unfound file. */
335 1.10 mycroft continue;
336 1.1 cgd }
337 1.1 cgd myfd = 1;
338 1.1 cgd }
339 1.1 cgd }
340 1.1 cgd /*
341 1.1 cgd * Find the requested capability record ...
342 1.1 cgd */
343 1.1 cgd {
344 1.1 cgd char buf[BUFSIZ];
345 1.20 mycroft char *b_end, *bp, *cp;
346 1.20 mycroft int c, slash;
347 1.1 cgd
348 1.1 cgd /*
349 1.1 cgd * Loop invariants:
350 1.1 cgd * There is always room for one more character in record.
351 1.1 cgd * R_end always points just past end of record.
352 1.1 cgd * Rp always points just past last character in record.
353 1.1 cgd * B_end always points just past last character in buf.
354 1.1 cgd * Bp always points at next character in buf.
355 1.20 mycroft * Cp remembers where the last colon was.
356 1.1 cgd */
357 1.1 cgd b_end = buf;
358 1.1 cgd bp = buf;
359 1.20 mycroft cp = 0;
360 1.20 mycroft slash = 0;
361 1.1 cgd for (;;) {
362 1.1 cgd
363 1.1 cgd /*
364 1.1 cgd * Read in a line implementing (\, newline)
365 1.1 cgd * line continuation.
366 1.1 cgd */
367 1.1 cgd rp = record;
368 1.1 cgd for (;;) {
369 1.1 cgd if (bp >= b_end) {
370 1.1 cgd int n;
371 1.1 cgd
372 1.1 cgd n = read(fd, buf, sizeof(buf));
373 1.1 cgd if (n <= 0) {
374 1.1 cgd if (myfd)
375 1.1 cgd (void)close(fd);
376 1.1 cgd if (n < 0) {
377 1.30 lukem int serrno = errno;
378 1.30 lukem
379 1.1 cgd free(record);
380 1.30 lukem errno = serrno;
381 1.1 cgd return (-2);
382 1.1 cgd } else {
383 1.1 cgd fd = -1;
384 1.1 cgd eof = 1;
385 1.1 cgd break;
386 1.1 cgd }
387 1.1 cgd }
388 1.1 cgd b_end = buf+n;
389 1.1 cgd bp = buf;
390 1.1 cgd }
391 1.1 cgd
392 1.1 cgd c = *bp++;
393 1.1 cgd if (c == '\n') {
394 1.20 mycroft if (slash) {
395 1.20 mycroft slash = 0;
396 1.1 cgd rp--;
397 1.1 cgd continue;
398 1.1 cgd } else
399 1.1 cgd break;
400 1.1 cgd }
401 1.20 mycroft if (slash) {
402 1.20 mycroft slash = 0;
403 1.20 mycroft cp = 0;
404 1.20 mycroft }
405 1.20 mycroft if (c == ':') {
406 1.20 mycroft /*
407 1.20 mycroft * If the field was `empty' (i.e.
408 1.20 mycroft * contained only white space), back up
409 1.20 mycroft * to the colon (eliminating the
410 1.20 mycroft * field).
411 1.20 mycroft */
412 1.20 mycroft if (cp)
413 1.20 mycroft rp = cp;
414 1.20 mycroft else
415 1.20 mycroft cp = rp;
416 1.20 mycroft } else if (c == '\\') {
417 1.20 mycroft slash = 1;
418 1.20 mycroft } else if (c != ' ' && c != '\t') {
419 1.20 mycroft /*
420 1.20 mycroft * Forget where the colon was, as this
421 1.20 mycroft * is not an empty field.
422 1.20 mycroft */
423 1.20 mycroft cp = 0;
424 1.20 mycroft }
425 1.1 cgd *rp++ = c;
426 1.1 cgd
427 1.1 cgd /*
428 1.1 cgd * Enforce loop invariant: if no room
429 1.1 cgd * left in record buffer, try to get
430 1.1 cgd * some more.
431 1.1 cgd */
432 1.1 cgd if (rp >= r_end) {
433 1.1 cgd u_int pos;
434 1.1 cgd size_t newsize;
435 1.1 cgd
436 1.1 cgd pos = rp - record;
437 1.1 cgd newsize = r_end - record + BFRAG;
438 1.33 itojun newrecord = realloc(record, newsize);
439 1.33 itojun if (newrecord == NULL) {
440 1.33 itojun free(record);
441 1.1 cgd if (myfd)
442 1.1 cgd (void)close(fd);
443 1.30 lukem errno = ENOMEM;
444 1.1 cgd return (-2);
445 1.1 cgd }
446 1.33 itojun record = newrecord;
447 1.1 cgd r_end = record + newsize;
448 1.1 cgd rp = record + pos;
449 1.1 cgd }
450 1.1 cgd }
451 1.20 mycroft /* Eliminate any white space after the last colon. */
452 1.20 mycroft if (cp)
453 1.20 mycroft rp = cp + 1;
454 1.20 mycroft /* Loop invariant lets us do this. */
455 1.1 cgd *rp++ = '\0';
456 1.1 cgd
457 1.1 cgd /*
458 1.1 cgd * If encountered eof check next file.
459 1.1 cgd */
460 1.1 cgd if (eof)
461 1.1 cgd break;
462 1.1 cgd
463 1.1 cgd /*
464 1.1 cgd * Toss blank lines and comments.
465 1.1 cgd */
466 1.1 cgd if (*record == '\0' || *record == '#')
467 1.1 cgd continue;
468 1.1 cgd
469 1.1 cgd /*
470 1.1 cgd * See if this is the record we want ...
471 1.1 cgd */
472 1.1 cgd if (cgetmatch(record, name) == 0) {
473 1.1 cgd if (nfield == NULL || !nfcmp(nfield, record)) {
474 1.1 cgd foundit = 1;
475 1.1 cgd break; /* found it! */
476 1.1 cgd }
477 1.1 cgd }
478 1.1 cgd }
479 1.1 cgd }
480 1.1 cgd if (foundit)
481 1.1 cgd break;
482 1.1 cgd }
483 1.1 cgd
484 1.1 cgd if (!foundit)
485 1.1 cgd return (-1);
486 1.1 cgd
487 1.1 cgd /*
488 1.1 cgd * Got the capability record, but now we have to expand all tc=name
489 1.1 cgd * references in it ...
490 1.1 cgd */
491 1.1 cgd tc_exp: {
492 1.16 perry char *newicap, *s;
493 1.17 perry size_t ilen, newilen;
494 1.1 cgd int diff, iret, tclen;
495 1.1 cgd char *icap, *scan, *tc, *tcstart, *tcend;
496 1.1 cgd
497 1.1 cgd /*
498 1.1 cgd * Loop invariants:
499 1.1 cgd * There is room for one more character in record.
500 1.1 cgd * R_end points just past end of record.
501 1.1 cgd * Rp points just past last character in record.
502 1.1 cgd * Scan points at remainder of record that needs to be
503 1.1 cgd * scanned for tc=name constructs.
504 1.1 cgd */
505 1.1 cgd scan = record;
506 1.1 cgd tc_not_resolved = 0;
507 1.1 cgd for (;;) {
508 1.1 cgd if ((tc = cgetcap(scan, "tc", '=')) == NULL)
509 1.1 cgd break;
510 1.1 cgd
511 1.1 cgd /*
512 1.1 cgd * Find end of tc=name and stomp on the trailing `:'
513 1.1 cgd * (if present) so we can use it to call ourselves.
514 1.1 cgd */
515 1.1 cgd s = tc;
516 1.1 cgd for (;;)
517 1.1 cgd if (*s == '\0')
518 1.1 cgd break;
519 1.1 cgd else
520 1.1 cgd if (*s++ == ':') {
521 1.1 cgd *(s - 1) = '\0';
522 1.1 cgd break;
523 1.1 cgd }
524 1.1 cgd tcstart = tc - 3;
525 1.1 cgd tclen = s - tcstart;
526 1.1 cgd tcend = s;
527 1.1 cgd
528 1.1 cgd iret = getent(&icap, &ilen, db_p, fd, tc, depth+1,
529 1.1 cgd NULL);
530 1.1 cgd newicap = icap; /* Put into a register. */
531 1.1 cgd newilen = ilen;
532 1.1 cgd if (iret != 0) {
533 1.1 cgd /* an error */
534 1.1 cgd if (iret < -1) {
535 1.1 cgd if (myfd)
536 1.1 cgd (void)close(fd);
537 1.1 cgd free(record);
538 1.1 cgd return (iret);
539 1.1 cgd }
540 1.1 cgd if (iret == 1)
541 1.1 cgd tc_not_resolved = 1;
542 1.1 cgd /* couldn't resolve tc */
543 1.1 cgd if (iret == -1) {
544 1.1 cgd *(s - 1) = ':';
545 1.1 cgd scan = s - 1;
546 1.1 cgd tc_not_resolved = 1;
547 1.1 cgd continue;
548 1.1 cgd
549 1.1 cgd }
550 1.1 cgd }
551 1.1 cgd /* not interested in name field of tc'ed record */
552 1.1 cgd s = newicap;
553 1.1 cgd for (;;)
554 1.1 cgd if (*s == '\0')
555 1.1 cgd break;
556 1.1 cgd else
557 1.1 cgd if (*s++ == ':')
558 1.1 cgd break;
559 1.1 cgd newilen -= s - newicap;
560 1.1 cgd newicap = s;
561 1.1 cgd
562 1.1 cgd /* make sure interpolated record is `:'-terminated */
563 1.1 cgd s += newilen;
564 1.1 cgd if (*(s-1) != ':') {
565 1.1 cgd *s = ':'; /* overwrite NUL with : */
566 1.1 cgd newilen++;
567 1.1 cgd }
568 1.1 cgd
569 1.1 cgd /*
570 1.1 cgd * Make sure there's enough room to insert the
571 1.1 cgd * new record.
572 1.1 cgd */
573 1.1 cgd diff = newilen - tclen;
574 1.1 cgd if (diff >= r_end - rp) {
575 1.1 cgd u_int pos, tcpos, tcposend;
576 1.1 cgd size_t newsize;
577 1.1 cgd
578 1.1 cgd pos = rp - record;
579 1.1 cgd newsize = r_end - record + diff + BFRAG;
580 1.1 cgd tcpos = tcstart - record;
581 1.1 cgd tcposend = tcend - record;
582 1.33 itojun newrecord = realloc(record, newsize);
583 1.33 itojun if (newrecord == NULL) {
584 1.33 itojun free(record);
585 1.1 cgd if (myfd)
586 1.1 cgd (void)close(fd);
587 1.1 cgd free(icap);
588 1.30 lukem errno = ENOMEM;
589 1.1 cgd return (-2);
590 1.1 cgd }
591 1.33 itojun record = newrecord;
592 1.1 cgd r_end = record + newsize;
593 1.1 cgd rp = record + pos;
594 1.1 cgd tcstart = record + tcpos;
595 1.1 cgd tcend = record + tcposend;
596 1.1 cgd }
597 1.1 cgd
598 1.1 cgd /*
599 1.1 cgd * Insert tc'ed record into our record.
600 1.1 cgd */
601 1.1 cgd s = tcstart + newilen;
602 1.23 perry memmove(s, tcend, (size_t)(rp - tcend));
603 1.23 perry memmove(tcstart, newicap, newilen);
604 1.1 cgd rp += diff;
605 1.1 cgd free(icap);
606 1.1 cgd
607 1.1 cgd /*
608 1.1 cgd * Start scan on `:' so next cgetcap works properly
609 1.1 cgd * (cgetcap always skips first field).
610 1.1 cgd */
611 1.1 cgd scan = s-1;
612 1.1 cgd }
613 1.1 cgd
614 1.1 cgd }
615 1.1 cgd /*
616 1.1 cgd * Close file (if we opened it), give back any extra memory, and
617 1.1 cgd * return capability, length and success.
618 1.1 cgd */
619 1.1 cgd if (myfd)
620 1.1 cgd (void)close(fd);
621 1.1 cgd *len = rp - record - 1; /* don't count NUL */
622 1.33 itojun if (r_end > rp) {
623 1.33 itojun if ((newrecord =
624 1.1 cgd realloc(record, (size_t)(rp - record))) == NULL) {
625 1.33 itojun free(record);
626 1.1 cgd errno = ENOMEM;
627 1.1 cgd return (-2);
628 1.1 cgd }
629 1.33 itojun record = newrecord;
630 1.33 itojun }
631 1.1 cgd
632 1.1 cgd *cap = record;
633 1.1 cgd if (tc_not_resolved)
634 1.1 cgd return (1);
635 1.1 cgd return (0);
636 1.1 cgd }
637 1.1 cgd
638 1.1 cgd static int
639 1.1 cgd cdbget(capdbp, bp, name)
640 1.1 cgd DB *capdbp;
641 1.21 mycroft char **bp;
642 1.21 mycroft const char *name;
643 1.1 cgd {
644 1.25 christos DBT key;
645 1.24 christos DBT data;
646 1.1 cgd
647 1.30 lukem _DIAGASSERT(capdbp != NULL);
648 1.30 lukem _DIAGASSERT(bp != NULL);
649 1.30 lukem _DIAGASSERT(name != NULL);
650 1.30 lukem
651 1.24 christos /* LINTED key is not modified */
652 1.21 mycroft key.data = (char *)name;
653 1.1 cgd key.size = strlen(name);
654 1.1 cgd
655 1.1 cgd for (;;) {
656 1.1 cgd /* Get the reference. */
657 1.1 cgd switch(capdbp->get(capdbp, &key, &data, 0)) {
658 1.1 cgd case -1:
659 1.1 cgd return (-2);
660 1.1 cgd case 1:
661 1.1 cgd return (-1);
662 1.1 cgd }
663 1.1 cgd
664 1.1 cgd /* If not an index to another record, leave. */
665 1.1 cgd if (((char *)data.data)[0] != SHADOW)
666 1.1 cgd break;
667 1.1 cgd
668 1.1 cgd key.data = (char *)data.data + 1;
669 1.1 cgd key.size = data.size - 1;
670 1.1 cgd }
671 1.1 cgd
672 1.1 cgd *bp = (char *)data.data + 1;
673 1.1 cgd return (((char *)(data.data))[0] == TCERR ? 1 : 0);
674 1.1 cgd }
675 1.1 cgd
676 1.1 cgd /*
677 1.1 cgd * Cgetmatch will return 0 if name is one of the names of the capability
678 1.1 cgd * record buf, -1 if not.
679 1.1 cgd */
680 1.1 cgd int
681 1.1 cgd cgetmatch(buf, name)
682 1.21 mycroft const char *buf, *name;
683 1.1 cgd {
684 1.21 mycroft const char *np, *bp;
685 1.1 cgd
686 1.30 lukem _DIAGASSERT(buf != NULL);
687 1.30 lukem _DIAGASSERT(name != NULL);
688 1.30 lukem
689 1.1 cgd /*
690 1.1 cgd * Start search at beginning of record.
691 1.1 cgd */
692 1.1 cgd bp = buf;
693 1.1 cgd for (;;) {
694 1.1 cgd /*
695 1.1 cgd * Try to match a record name.
696 1.1 cgd */
697 1.1 cgd np = name;
698 1.1 cgd for (;;)
699 1.26 christos if (*np == '\0') {
700 1.1 cgd if (*bp == '|' || *bp == ':' || *bp == '\0')
701 1.1 cgd return (0);
702 1.1 cgd else
703 1.1 cgd break;
704 1.26 christos } else
705 1.1 cgd if (*bp++ != *np++)
706 1.1 cgd break;
707 1.1 cgd
708 1.1 cgd /*
709 1.1 cgd * Match failed, skip to next name in record.
710 1.1 cgd */
711 1.34 mrg if (bp > buf)
712 1.34 mrg bp--; /* a '|' or ':' may have stopped the match */
713 1.34 mrg else
714 1.34 mrg return (-1);
715 1.1 cgd for (;;)
716 1.1 cgd if (*bp == '\0' || *bp == ':')
717 1.1 cgd return (-1); /* match failed totally */
718 1.1 cgd else
719 1.1 cgd if (*bp++ == '|')
720 1.1 cgd break; /* found next name */
721 1.1 cgd }
722 1.1 cgd }
723 1.1 cgd
724 1.1 cgd int
725 1.1 cgd cgetfirst(buf, db_array)
726 1.1 cgd char **buf, **db_array;
727 1.1 cgd {
728 1.30 lukem
729 1.30 lukem _DIAGASSERT(buf != NULL);
730 1.30 lukem _DIAGASSERT(db_array != NULL);
731 1.30 lukem
732 1.1 cgd (void)cgetclose();
733 1.1 cgd return (cgetnext(buf, db_array));
734 1.1 cgd }
735 1.1 cgd
736 1.1 cgd static FILE *pfp;
737 1.1 cgd static int slash;
738 1.1 cgd static char **dbp;
739 1.1 cgd
740 1.1 cgd int
741 1.1 cgd cgetclose()
742 1.1 cgd {
743 1.1 cgd if (pfp != NULL) {
744 1.1 cgd (void)fclose(pfp);
745 1.1 cgd pfp = NULL;
746 1.1 cgd }
747 1.1 cgd dbp = NULL;
748 1.1 cgd gottoprec = 0;
749 1.1 cgd slash = 0;
750 1.1 cgd return(0);
751 1.1 cgd }
752 1.1 cgd
753 1.1 cgd /*
754 1.1 cgd * Cgetnext() gets either the first or next entry in the logical database
755 1.1 cgd * specified by db_array. It returns 0 upon completion of the database, 1
756 1.1 cgd * upon returning an entry with more remaining, and -1 if an error occurs.
757 1.1 cgd */
758 1.1 cgd int
759 1.1 cgd cgetnext(bp, db_array)
760 1.16 perry char **bp;
761 1.1 cgd char **db_array;
762 1.1 cgd {
763 1.1 cgd size_t len;
764 1.17 perry int status, done;
765 1.1 cgd char *cp, *line, *rp, *np, buf[BSIZE], nbuf[BSIZE];
766 1.18 thorpej size_t dummy;
767 1.1 cgd
768 1.30 lukem _DIAGASSERT(bp != NULL);
769 1.30 lukem _DIAGASSERT(db_array != NULL);
770 1.30 lukem
771 1.1 cgd if (dbp == NULL)
772 1.1 cgd dbp = db_array;
773 1.1 cgd
774 1.1 cgd if (pfp == NULL && (pfp = fopen(*dbp, "r")) == NULL) {
775 1.1 cgd (void)cgetclose();
776 1.1 cgd return (-1);
777 1.1 cgd }
778 1.1 cgd for(;;) {
779 1.1 cgd if (toprec && !gottoprec) {
780 1.1 cgd gottoprec = 1;
781 1.1 cgd line = toprec;
782 1.1 cgd } else {
783 1.6 cgd line = fgetln(pfp, &len);
784 1.1 cgd if (line == NULL && pfp) {
785 1.1 cgd if (ferror(pfp)) {
786 1.1 cgd (void)cgetclose();
787 1.1 cgd return (-1);
788 1.1 cgd } else {
789 1.19 tv (void)fclose(pfp);
790 1.19 tv pfp = NULL;
791 1.1 cgd if (*++dbp == NULL) {
792 1.1 cgd (void)cgetclose();
793 1.1 cgd return (0);
794 1.1 cgd } else if ((pfp =
795 1.1 cgd fopen(*dbp, "r")) == NULL) {
796 1.1 cgd (void)cgetclose();
797 1.1 cgd return (-1);
798 1.1 cgd } else
799 1.1 cgd continue;
800 1.1 cgd }
801 1.5 cgd } else
802 1.5 cgd line[len - 1] = '\0';
803 1.5 cgd if (len == 1) {
804 1.1 cgd slash = 0;
805 1.1 cgd continue;
806 1.1 cgd }
807 1.26 christos if (isspace((unsigned char)*line) ||
808 1.1 cgd *line == ':' || *line == '#' || slash) {
809 1.5 cgd if (line[len - 2] == '\\')
810 1.1 cgd slash = 1;
811 1.1 cgd else
812 1.1 cgd slash = 0;
813 1.1 cgd continue;
814 1.1 cgd }
815 1.5 cgd if (line[len - 2] == '\\')
816 1.1 cgd slash = 1;
817 1.1 cgd else
818 1.1 cgd slash = 0;
819 1.1 cgd }
820 1.1 cgd
821 1.1 cgd
822 1.1 cgd /*
823 1.1 cgd * Line points to a name line.
824 1.1 cgd */
825 1.35 groo if (len > sizeof(nbuf))
826 1.35 groo return -1;
827 1.1 cgd done = 0;
828 1.1 cgd np = nbuf;
829 1.1 cgd for (;;) {
830 1.1 cgd for (cp = line; *cp != '\0'; cp++) {
831 1.1 cgd if (*cp == ':') {
832 1.1 cgd *np++ = ':';
833 1.1 cgd done = 1;
834 1.1 cgd break;
835 1.1 cgd }
836 1.1 cgd if (*cp == '\\')
837 1.1 cgd break;
838 1.1 cgd *np++ = *cp;
839 1.1 cgd }
840 1.1 cgd if (done) {
841 1.1 cgd *np = '\0';
842 1.1 cgd break;
843 1.1 cgd } else { /* name field extends beyond the line */
844 1.6 cgd line = fgetln(pfp, &len);
845 1.1 cgd if (line == NULL && pfp) {
846 1.1 cgd if (ferror(pfp)) {
847 1.1 cgd (void)cgetclose();
848 1.1 cgd return (-1);
849 1.1 cgd }
850 1.19 tv (void)fclose(pfp);
851 1.19 tv pfp = NULL;
852 1.19 tv *np = '\0';
853 1.19 tv break;
854 1.5 cgd } else
855 1.5 cgd line[len - 1] = '\0';
856 1.1 cgd }
857 1.1 cgd }
858 1.35 groo if (len > sizeof(buf))
859 1.35 groo return -1;
860 1.1 cgd rp = buf;
861 1.12 pk for(cp = nbuf; *cp != '\0'; cp++)
862 1.1 cgd if (*cp == '|' || *cp == ':')
863 1.1 cgd break;
864 1.1 cgd else
865 1.1 cgd *rp++ = *cp;
866 1.1 cgd
867 1.1 cgd *rp = '\0';
868 1.1 cgd /*
869 1.1 cgd * XXX
870 1.1 cgd * Last argument of getent here should be nbuf if we want true
871 1.1 cgd * sequential access in the case of duplicates.
872 1.1 cgd * With NULL, getent will return the first entry found
873 1.1 cgd * rather than the duplicate entry record. This is a
874 1.1 cgd * matter of semantics that should be resolved.
875 1.1 cgd */
876 1.1 cgd status = getent(bp, &dummy, db_array, -1, buf, 0, NULL);
877 1.1 cgd if (status == -2 || status == -3)
878 1.1 cgd (void)cgetclose();
879 1.1 cgd
880 1.1 cgd return (status + 1);
881 1.1 cgd }
882 1.1 cgd /* NOTREACHED */
883 1.1 cgd }
884 1.1 cgd
885 1.1 cgd /*
886 1.1 cgd * Cgetstr retrieves the value of the string capability cap from the
887 1.1 cgd * capability record pointed to by buf. A pointer to a decoded, NUL
888 1.1 cgd * terminated, malloc'd copy of the string is returned in the char *
889 1.1 cgd * pointed to by str. The length of the string not including the trailing
890 1.1 cgd * NUL is returned on success, -1 if the requested string capability
891 1.1 cgd * couldn't be found, -2 if a system error was encountered (storage
892 1.1 cgd * allocation failure).
893 1.1 cgd */
894 1.1 cgd int
895 1.1 cgd cgetstr(buf, cap, str)
896 1.21 mycroft char *buf;
897 1.21 mycroft const char *cap;
898 1.1 cgd char **str;
899 1.1 cgd {
900 1.16 perry u_int m_room;
901 1.21 mycroft const char *bp;
902 1.21 mycroft char *mp;
903 1.1 cgd int len;
904 1.33 itojun char *mem, *newmem;
905 1.1 cgd
906 1.30 lukem _DIAGASSERT(buf != NULL);
907 1.30 lukem _DIAGASSERT(cap != NULL);
908 1.30 lukem _DIAGASSERT(str != NULL);
909 1.30 lukem
910 1.1 cgd /*
911 1.1 cgd * Find string capability cap
912 1.1 cgd */
913 1.1 cgd bp = cgetcap(buf, cap, '=');
914 1.1 cgd if (bp == NULL)
915 1.1 cgd return (-1);
916 1.1 cgd
917 1.1 cgd /*
918 1.1 cgd * Conversion / storage allocation loop ... Allocate memory in
919 1.1 cgd * chunks SFRAG in size.
920 1.1 cgd */
921 1.1 cgd if ((mem = malloc(SFRAG)) == NULL) {
922 1.1 cgd errno = ENOMEM;
923 1.1 cgd return (-2); /* couldn't even allocate the first fragment */
924 1.1 cgd }
925 1.1 cgd m_room = SFRAG;
926 1.1 cgd mp = mem;
927 1.1 cgd
928 1.1 cgd while (*bp != ':' && *bp != '\0') {
929 1.1 cgd /*
930 1.1 cgd * Loop invariants:
931 1.1 cgd * There is always room for one more character in mem.
932 1.1 cgd * Mp always points just past last character in mem.
933 1.1 cgd * Bp always points at next character in buf.
934 1.1 cgd */
935 1.1 cgd if (*bp == '^') {
936 1.1 cgd bp++;
937 1.1 cgd if (*bp == ':' || *bp == '\0')
938 1.1 cgd break; /* drop unfinished escape */
939 1.1 cgd *mp++ = *bp++ & 037;
940 1.1 cgd } else if (*bp == '\\') {
941 1.1 cgd bp++;
942 1.1 cgd if (*bp == ':' || *bp == '\0')
943 1.1 cgd break; /* drop unfinished escape */
944 1.1 cgd if ('0' <= *bp && *bp <= '7') {
945 1.16 perry int n, i;
946 1.1 cgd
947 1.1 cgd n = 0;
948 1.1 cgd i = 3; /* maximum of three octal digits */
949 1.1 cgd do {
950 1.1 cgd n = n * 8 + (*bp++ - '0');
951 1.1 cgd } while (--i && '0' <= *bp && *bp <= '7');
952 1.1 cgd *mp++ = n;
953 1.1 cgd }
954 1.1 cgd else switch (*bp++) {
955 1.1 cgd case 'b': case 'B':
956 1.1 cgd *mp++ = '\b';
957 1.1 cgd break;
958 1.1 cgd case 't': case 'T':
959 1.1 cgd *mp++ = '\t';
960 1.1 cgd break;
961 1.1 cgd case 'n': case 'N':
962 1.1 cgd *mp++ = '\n';
963 1.1 cgd break;
964 1.1 cgd case 'f': case 'F':
965 1.1 cgd *mp++ = '\f';
966 1.1 cgd break;
967 1.1 cgd case 'r': case 'R':
968 1.1 cgd *mp++ = '\r';
969 1.1 cgd break;
970 1.1 cgd case 'e': case 'E':
971 1.1 cgd *mp++ = ESC;
972 1.1 cgd break;
973 1.1 cgd case 'c': case 'C':
974 1.1 cgd *mp++ = ':';
975 1.1 cgd break;
976 1.1 cgd default:
977 1.1 cgd /*
978 1.1 cgd * Catches '\', '^', and
979 1.1 cgd * everything else.
980 1.1 cgd */
981 1.1 cgd *mp++ = *(bp-1);
982 1.1 cgd break;
983 1.1 cgd }
984 1.1 cgd } else
985 1.1 cgd *mp++ = *bp++;
986 1.1 cgd m_room--;
987 1.1 cgd
988 1.1 cgd /*
989 1.1 cgd * Enforce loop invariant: if no room left in current
990 1.1 cgd * buffer, try to get some more.
991 1.1 cgd */
992 1.1 cgd if (m_room == 0) {
993 1.1 cgd size_t size = mp - mem;
994 1.1 cgd
995 1.33 itojun if ((newmem = realloc(mem, size + SFRAG)) == NULL) {
996 1.33 itojun free(mem);
997 1.1 cgd return (-2);
998 1.33 itojun }
999 1.33 itojun mem = newmem;
1000 1.1 cgd m_room = SFRAG;
1001 1.1 cgd mp = mem + size;
1002 1.1 cgd }
1003 1.1 cgd }
1004 1.1 cgd *mp++ = '\0'; /* loop invariant let's us do this */
1005 1.1 cgd m_room--;
1006 1.1 cgd len = mp - mem - 1;
1007 1.1 cgd
1008 1.1 cgd /*
1009 1.1 cgd * Give back any extra memory and return value and success.
1010 1.1 cgd */
1011 1.33 itojun if (m_room != 0) {
1012 1.33 itojun if ((newmem = realloc(mem, (size_t)(mp - mem))) == NULL) {
1013 1.33 itojun free(mem);
1014 1.1 cgd return (-2);
1015 1.33 itojun }
1016 1.33 itojun mem = newmem;
1017 1.33 itojun }
1018 1.1 cgd *str = mem;
1019 1.1 cgd return (len);
1020 1.1 cgd }
1021 1.1 cgd
1022 1.1 cgd /*
1023 1.1 cgd * Cgetustr retrieves the value of the string capability cap from the
1024 1.1 cgd * capability record pointed to by buf. The difference between cgetustr()
1025 1.1 cgd * and cgetstr() is that cgetustr does not decode escapes but rather treats
1026 1.1 cgd * all characters literally. A pointer to a NUL terminated malloc'd
1027 1.1 cgd * copy of the string is returned in the char pointed to by str. The
1028 1.1 cgd * length of the string not including the trailing NUL is returned on success,
1029 1.1 cgd * -1 if the requested string capability couldn't be found, -2 if a system
1030 1.1 cgd * error was encountered (storage allocation failure).
1031 1.1 cgd */
1032 1.1 cgd int
1033 1.1 cgd cgetustr(buf, cap, str)
1034 1.21 mycroft char *buf;
1035 1.21 mycroft const char *cap;
1036 1.21 mycroft char **str;
1037 1.1 cgd {
1038 1.16 perry u_int m_room;
1039 1.21 mycroft const char *bp;
1040 1.21 mycroft char *mp;
1041 1.1 cgd int len;
1042 1.33 itojun char *mem, *newmem;
1043 1.1 cgd
1044 1.30 lukem _DIAGASSERT(buf != NULL);
1045 1.30 lukem _DIAGASSERT(cap != NULL);
1046 1.30 lukem _DIAGASSERT(str != NULL);
1047 1.30 lukem
1048 1.1 cgd /*
1049 1.1 cgd * Find string capability cap
1050 1.1 cgd */
1051 1.1 cgd if ((bp = cgetcap(buf, cap, '=')) == NULL)
1052 1.1 cgd return (-1);
1053 1.1 cgd
1054 1.1 cgd /*
1055 1.1 cgd * Conversion / storage allocation loop ... Allocate memory in
1056 1.1 cgd * chunks SFRAG in size.
1057 1.1 cgd */
1058 1.1 cgd if ((mem = malloc(SFRAG)) == NULL) {
1059 1.1 cgd errno = ENOMEM;
1060 1.1 cgd return (-2); /* couldn't even allocate the first fragment */
1061 1.1 cgd }
1062 1.1 cgd m_room = SFRAG;
1063 1.1 cgd mp = mem;
1064 1.1 cgd
1065 1.1 cgd while (*bp != ':' && *bp != '\0') {
1066 1.1 cgd /*
1067 1.1 cgd * Loop invariants:
1068 1.1 cgd * There is always room for one more character in mem.
1069 1.1 cgd * Mp always points just past last character in mem.
1070 1.1 cgd * Bp always points at next character in buf.
1071 1.1 cgd */
1072 1.1 cgd *mp++ = *bp++;
1073 1.1 cgd m_room--;
1074 1.1 cgd
1075 1.1 cgd /*
1076 1.1 cgd * Enforce loop invariant: if no room left in current
1077 1.1 cgd * buffer, try to get some more.
1078 1.1 cgd */
1079 1.1 cgd if (m_room == 0) {
1080 1.1 cgd size_t size = mp - mem;
1081 1.1 cgd
1082 1.33 itojun if ((newmem = realloc(mem, size + SFRAG)) == NULL) {
1083 1.33 itojun free(mem);
1084 1.1 cgd return (-2);
1085 1.33 itojun }
1086 1.33 itojun mem = newmem;
1087 1.1 cgd m_room = SFRAG;
1088 1.1 cgd mp = mem + size;
1089 1.1 cgd }
1090 1.1 cgd }
1091 1.1 cgd *mp++ = '\0'; /* loop invariant let's us do this */
1092 1.1 cgd m_room--;
1093 1.1 cgd len = mp - mem - 1;
1094 1.1 cgd
1095 1.1 cgd /*
1096 1.1 cgd * Give back any extra memory and return value and success.
1097 1.1 cgd */
1098 1.33 itojun if (m_room != 0) {
1099 1.33 itojun if ((newmem = realloc(mem, (size_t)(mp - mem))) == NULL) {
1100 1.33 itojun free(mem);
1101 1.1 cgd return (-2);
1102 1.33 itojun }
1103 1.33 itojun mem = newmem;
1104 1.33 itojun }
1105 1.1 cgd *str = mem;
1106 1.1 cgd return (len);
1107 1.1 cgd }
1108 1.1 cgd
1109 1.1 cgd /*
1110 1.1 cgd * Cgetnum retrieves the value of the numeric capability cap from the
1111 1.1 cgd * capability record pointed to by buf. The numeric value is returned in
1112 1.1 cgd * the long pointed to by num. 0 is returned on success, -1 if the requested
1113 1.1 cgd * numeric capability couldn't be found.
1114 1.1 cgd */
1115 1.1 cgd int
1116 1.1 cgd cgetnum(buf, cap, num)
1117 1.21 mycroft char *buf;
1118 1.21 mycroft const char *cap;
1119 1.1 cgd long *num;
1120 1.1 cgd {
1121 1.16 perry long n;
1122 1.16 perry int base, digit;
1123 1.21 mycroft const char *bp;
1124 1.1 cgd
1125 1.30 lukem _DIAGASSERT(buf != NULL);
1126 1.30 lukem _DIAGASSERT(cap != NULL);
1127 1.30 lukem _DIAGASSERT(num != NULL);
1128 1.30 lukem
1129 1.1 cgd /*
1130 1.1 cgd * Find numeric capability cap
1131 1.1 cgd */
1132 1.1 cgd bp = cgetcap(buf, cap, '#');
1133 1.1 cgd if (bp == NULL)
1134 1.1 cgd return (-1);
1135 1.1 cgd
1136 1.1 cgd /*
1137 1.1 cgd * Look at value and determine numeric base:
1138 1.1 cgd * 0x... or 0X... hexadecimal,
1139 1.1 cgd * else 0... octal,
1140 1.1 cgd * else decimal.
1141 1.1 cgd */
1142 1.1 cgd if (*bp == '0') {
1143 1.1 cgd bp++;
1144 1.1 cgd if (*bp == 'x' || *bp == 'X') {
1145 1.1 cgd bp++;
1146 1.1 cgd base = 16;
1147 1.1 cgd } else
1148 1.1 cgd base = 8;
1149 1.1 cgd } else
1150 1.1 cgd base = 10;
1151 1.1 cgd
1152 1.1 cgd /*
1153 1.1 cgd * Conversion loop ...
1154 1.1 cgd */
1155 1.1 cgd n = 0;
1156 1.1 cgd for (;;) {
1157 1.1 cgd if ('0' <= *bp && *bp <= '9')
1158 1.1 cgd digit = *bp - '0';
1159 1.1 cgd else if ('a' <= *bp && *bp <= 'f')
1160 1.1 cgd digit = 10 + *bp - 'a';
1161 1.1 cgd else if ('A' <= *bp && *bp <= 'F')
1162 1.1 cgd digit = 10 + *bp - 'A';
1163 1.1 cgd else
1164 1.1 cgd break;
1165 1.1 cgd
1166 1.1 cgd if (digit >= base)
1167 1.1 cgd break;
1168 1.1 cgd
1169 1.1 cgd n = n * base + digit;
1170 1.1 cgd bp++;
1171 1.1 cgd }
1172 1.1 cgd
1173 1.1 cgd /*
1174 1.1 cgd * Return value and success.
1175 1.1 cgd */
1176 1.1 cgd *num = n;
1177 1.1 cgd return (0);
1178 1.1 cgd }
1179 1.1 cgd
1180 1.1 cgd
1181 1.1 cgd /*
1182 1.1 cgd * Compare name field of record.
1183 1.1 cgd */
1184 1.1 cgd static int
1185 1.1 cgd nfcmp(nf, rec)
1186 1.1 cgd char *nf, *rec;
1187 1.1 cgd {
1188 1.1 cgd char *cp, tmp;
1189 1.1 cgd int ret;
1190 1.30 lukem
1191 1.30 lukem _DIAGASSERT(nf != NULL);
1192 1.30 lukem _DIAGASSERT(rec != NULL);
1193 1.30 lukem
1194 1.1 cgd for (cp = rec; *cp != ':'; cp++)
1195 1.1 cgd ;
1196 1.1 cgd
1197 1.1 cgd tmp = *(cp + 1);
1198 1.1 cgd *(cp + 1) = '\0';
1199 1.1 cgd ret = strcmp(nf, rec);
1200 1.1 cgd *(cp + 1) = tmp;
1201 1.1 cgd
1202 1.1 cgd return (ret);
1203 1.1 cgd }
1204