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