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