]> git.saurik.com Git - apple/libc.git/blame - gen/getcap-fbsd.c
Libc-594.1.4.tar.gz
[apple/libc.git] / gen / getcap-fbsd.c
CommitLineData
224c7076
A
1/*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. 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)
38static char sccsid[] = "@(#)getcap.c 8.3 (Berkeley) 3/25/94";
39#endif /* LIBC_SCCS and not lint */
40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD: src/lib/libc/gen/getcap.c,v 1.19 2003/01/02 10:19:43 thomas Exp $");
42
43#include "xlocale_private.h"
44
45#include "namespace.h"
46#include <sys/types.h>
47
48#include <ctype.h>
49#include <errno.h>
50#include <fcntl.h>
51#include <limits.h>
52#include <stdio.h>
53#include <stdlib.h>
54#include <string.h>
55#include <unistd.h>
56#include "un-namespace.h"
57
58#include <db.h>
59
60#define BFRAG 1024
61#define BSIZE 1024
62#define ESC ('[' & 037) /* ASCII ESC */
63#define MAX_RECURSION 32 /* maximum getent recursion */
64#define SFRAG 100 /* cgetstr mallocs in SFRAG chunks */
65
66#define RECOK (char)0
67#define TCERR (char)1
68#define SHADOW (char)2
69
70static size_t topreclen; /* toprec length */
71static char *toprec; /* Additional record specified by cgetset() */
72static int gottoprec; /* Flag indicating retrieval of toprecord */
73
74static int cdbget(DB *, char **, const char *);
75static int getent(char **, u_int *, char **, int, const char *, int, char *, locale_t);
76static int nfcmp(char *, char *);
77
78/*
79 * Cgetset() allows the addition of a user specified buffer to be added
80 * to the database array, in effect "pushing" the buffer on top of the
81 * virtual database. 0 is returned on success, -1 on failure.
82 */
83int
84cgetset(const 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 */
115char *
116cgetcap(char *buf, const char *cap, int type)
117{
118 char *bp;
119 const char *cp;
120
121 bp = buf;
122 for (;;) {
123 /*
124 * Skip past the current capability field - it's either the
125 * name field if this is the first time through the loop, or
126 * the remainder of a field whose name failed to match cap.
127 */
128 for (;;)
129 if (*bp == '\0')
130 return (NULL);
131 else
132 if (*bp++ == ':')
133 break;
134
135 /*
136 * Try to match (cap, type) in buf.
137 */
138 for (cp = cap; *cp == *bp && *bp != '\0'; cp++, bp++)
139 continue;
140 if (*cp != '\0')
141 continue;
142 if (*bp == '@')
143 return (NULL);
144 if (type == ':') {
145 if (*bp != '\0' && *bp != ':')
146 continue;
147 return(bp);
148 }
149 if (*bp != type)
150 continue;
151 bp++;
152 return (*bp == '@' ? NULL : bp);
153 }
154 /* NOTREACHED */
155}
156
157/*
158 * Cgetent extracts the capability record name from the NULL terminated file
159 * array db_array and returns a pointer to a malloc'd copy of it in buf.
160 * Buf must be retained through all subsequent calls to cgetcap, cgetnum,
161 * cgetflag, and cgetstr, but may then be free'd. 0 is returned on success,
162 * -1 if the requested record couldn't be found, -2 if a system error was
163 * encountered (couldn't open/read a file, etc.), and -3 if a potential
164 * reference loop is detected.
165 */
166int
167cgetent(char **buf, char **db_array, const char *name)
168{
169 u_int dummy;
170
171 return (getent(buf, &dummy, db_array, -1, name, 0, NULL, __current_locale()));
172}
173
174/*
175 * Getent implements the functions of cgetent. If fd is non-negative,
176 * *db_array has already been opened and fd is the open file descriptor. We
177 * do this to save time and avoid using up file descriptors for tc=
178 * recursions.
179 *
180 * Getent returns the same success/failure codes as cgetent. On success, a
181 * pointer to a malloc'ed capability record with all tc= capabilities fully
182 * expanded and its length (not including trailing ASCII NUL) are left in
183 * *cap and *len.
184 *
185 * Basic algorithm:
186 * + Allocate memory incrementally as needed in chunks of size BFRAG
187 * for capability buffer.
188 * + Recurse for each tc=name and interpolate result. Stop when all
189 * names interpolated, a name can't be found, or depth exceeds
190 * MAX_RECURSION.
191 */
192static int
193getent(char **cap, u_int *len, char **db_array, int fd, const char *name,
194 int depth, char *nfield, locale_t loc)
195{
196 DB *capdbp;
197 char *r_end, *rp, **db_p;
198 int myfd, eof, foundit, retval, clen;
199 char *record, *cbuf;
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, SEEK_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);
254 if (retval < 0) {
255 /* no record available */
256 (void)capdbp->close(capdbp);
257 return (retval);
258 }
259 /* save the data; close frees it */
260 clen = strlen(record);
261 cbuf = malloc(clen + 1);
262 memcpy(cbuf, record, clen + 1);
263 if (capdbp->close(capdbp) < 0) {
264 free(cbuf);
265 return (-2);
266 }
267 *len = clen;
268 *cap = cbuf;
269 return (retval);
270 } else {
271 fd = _open(*db_p, O_RDONLY, 0);
272 if (fd < 0)
273 continue;
274 myfd = 1;
275 }
276 }
277 /*
278 * Find the requested capability record ...
279 */
280 {
281 char buf[BUFSIZ];
282 char *b_end, *bp;
283 int c;
284
285 /*
286 * Loop invariants:
287 * There is always room for one more character in record.
288 * R_end always points just past end of record.
289 * Rp always points just past last character in record.
290 * B_end always points just past last character in buf.
291 * Bp always points at next character in buf.
292 */
293 b_end = buf;
294 bp = buf;
295 for (;;) {
296
297 /*
298 * Read in a line implementing (\, newline)
299 * line continuation.
300 */
301 rp = record;
302 for (;;) {
303 if (bp >= b_end) {
304 int n;
305
306 n = _read(fd, buf, sizeof(buf));
307 if (n <= 0) {
308 if (myfd)
309 (void)_close(fd);
310 if (n < 0) {
311 free(record);
312 return (-2);
313 } else {
314 fd = -1;
315 eof = 1;
316 break;
317 }
318 }
319 b_end = buf+n;
320 bp = buf;
321 }
322
323 c = *bp++;
324 if (c == '\n') {
325 if (rp > record && *(rp-1) == '\\') {
326 rp--;
327 continue;
328 } else
329 break;
330 }
331 *rp++ = c;
332
333 /*
334 * Enforce loop invariant: if no room
335 * left in record buffer, try to get
336 * some more.
337 */
338 if (rp >= r_end) {
339 u_int pos;
340 size_t newsize;
341
342 pos = rp - record;
343 newsize = r_end - record + BFRAG;
344 record = reallocf(record, newsize);
345 if (record == NULL) {
346 errno = ENOMEM;
347 if (myfd)
348 (void)_close(fd);
349 return (-2);
350 }
351 r_end = record + newsize;
352 rp = record + pos;
353 }
354 }
355 /* loop invariant let's us do this */
356 *rp++ = '\0';
357
358 /*
359 * If encountered eof check next file.
360 */
361 if (eof)
362 break;
363
364 /*
365 * Toss blank lines and comments.
366 */
367 if (*record == '\0' || *record == '#')
368 continue;
369
370 /*
371 * See if this is the record we want ...
372 */
373 if (cgetmatch(record, name) == 0) {
374 if (nfield == NULL || !nfcmp(nfield, record)) {
375 foundit = 1;
376 break; /* found it! */
377 }
378 }
379 }
380 }
381 if (foundit)
382 break;
383 }
384
385 if (!foundit) {
386 free(record);
387 return (-1);
388 }
389
390 /*
391 * Got the capability record, but now we have to expand all tc=name
392 * references in it ...
393 */
394tc_exp: {
395 char *newicap, *s;
396 int newilen;
397 u_int ilen;
398 int diff, iret, tclen;
399 char *icap, *scan, *tc, *tcstart, *tcend;
400
401 /*
402 * Loop invariants:
403 * There is room for one more character in record.
404 * R_end points just past end of record.
405 * Rp points just past last character in record.
406 * Scan points at remainder of record that needs to be
407 * scanned for tc=name constructs.
408 */
409 scan = record;
410 tc_not_resolved = 0;
411 for (;;) {
412 if ((tc = cgetcap(scan, "tc", '=')) == NULL)
413 break;
414
415 /*
416 * Find end of tc=name and stomp on the trailing `:'
417 * (if present) so we can use it to call ourselves.
418 */
419 s = tc;
420 for (;;)
421 if (*s == '\0')
422 break;
423 else
424 if (*s++ == ':') {
425 *(s - 1) = '\0';
426 break;
427 }
428 tcstart = tc - 3;
429 tclen = s - tcstart;
430 tcend = s;
431
432 iret = getent(&icap, &ilen, db_p, fd, tc, depth+1,
433 NULL, loc);
434 newicap = icap; /* Put into a register. */
435 newilen = ilen;
436 if (iret != 0) {
437 /* an error */
438 if (iret < -1) {
439 if (myfd)
440 (void)_close(fd);
441 free(record);
442 return (iret);
443 }
444 if (iret == 1)
445 tc_not_resolved = 1;
446 /* couldn't resolve tc */
447 if (iret == -1) {
448 *(s - 1) = ':';
449 scan = s - 1;
450 tc_not_resolved = 1;
451 continue;
452
453 }
454 }
455 /* not interested in name field of tc'ed record */
456 s = newicap;
457 for (;;)
458 if (*s == '\0')
459 break;
460 else
461 if (*s++ == ':')
462 break;
463 newilen -= s - newicap;
464 newicap = s;
465
466 /* make sure interpolated record is `:'-terminated */
467 s += newilen;
468 if (*(s-1) != ':') {
469 *s = ':'; /* overwrite NUL with : */
470 newilen++;
471 }
472
473 /*
474 * Make sure there's enough room to insert the
475 * new record.
476 */
477 diff = newilen - tclen;
478 if (diff >= r_end - rp) {
479 u_int pos, tcpos, tcposend;
480 size_t newsize;
481
482 pos = rp - record;
483 newsize = r_end - record + diff + BFRAG;
484 tcpos = tcstart - record;
485 tcposend = tcend - record;
486 record = reallocf(record, newsize);
487 if (record == NULL) {
488 errno = ENOMEM;
489 if (myfd)
490 (void)_close(fd);
491 free(icap);
492 return (-2);
493 }
494 r_end = record + newsize;
495 rp = record + pos;
496 tcstart = record + tcpos;
497 tcend = record + tcposend;
498 }
499
500 /*
501 * Insert tc'ed record into our record.
502 */
503 s = tcstart + newilen;
504 bcopy(tcend, s, rp - tcend);
505 bcopy(newicap, tcstart, newilen);
506 rp += diff;
507 free(icap);
508
509 /*
510 * Start scan on `:' so next cgetcap works properly
511 * (cgetcap always skips first field).
512 */
513 scan = s-1;
514 }
515
516 }
517 /*
518 * Close file (if we opened it), give back any extra memory, and
519 * return capability, length and success.
520 */
521 if (myfd)
522 (void)_close(fd);
523 *len = rp - record - 1; /* don't count NUL */
524 if (r_end > rp)
525 if ((record =
526 reallocf(record, (size_t)(rp - record))) == NULL) {
527 errno = ENOMEM;
528 return (-2);
529 }
530
531 *cap = record;
532 if (tc_not_resolved)
533 return (1);
534 return (0);
535}
536
537static int
538cdbget(DB *capdbp, char **bp, const char *name)
539{
540 DBT key, data;
541 char *namebuf;
542
543 namebuf = strdup(name);
544 if (namebuf == NULL)
545 return (-2);
546 key.data = namebuf;
547 key.size = strlen(namebuf);
548
549 for (;;) {
550 /* Get the reference. */
551 switch(capdbp->get(capdbp, &key, &data, 0)) {
552 case -1:
553 free(namebuf);
554 return (-2);
555 case 1:
556 free(namebuf);
557 return (-1);
558 }
559
560 /* If not an index to another record, leave. */
561 if (((char *)data.data)[0] != SHADOW)
562 break;
563
564 key.data = (char *)data.data + 1;
565 key.size = data.size - 1;
566 }
567
568 *bp = (char *)data.data + 1;
569 free(namebuf);
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 */
577int
578cgetmatch(const char *buf, const char *name)
579{
580 const char *np, *bp;
581
582 if (name == NULL || *name == '\0')
583 return -1;
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
621int
622cgetfirst(char **buf, char **db_array)
623{
624 (void)cgetclose();
625 return (cgetnext(buf, db_array));
626}
627
628static FILE *pfp;
629static int slash;
630static char **dbp;
631
632int
633cgetclose(void)
634{
635 if (pfp != NULL) {
636 (void)fclose(pfp);
637 pfp = NULL;
638 }
639 dbp = NULL;
640 gottoprec = 0;
641 slash = 0;
642 return(0);
643}
644
645/*
646 * Cgetnext() gets either the first or next entry in the logical database
647 * specified by db_array. It returns 0 upon completion of the database, 1
648 * upon returning an entry with more remaining, and -1 if an error occurs.
649 */
650int
651cgetnext(char **bp, char **db_array)
652{
653 size_t len;
654 int done, hadreaderr, i, savederrno, status;
655 char *cp, *line, *rp, *np, buf[BSIZE], nbuf[BSIZE];
656 u_int dummy;
657 locale_t loc = __current_locale();
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 {
671 line = fgetln(pfp, &len);
672 if (line == NULL && pfp) {
673 hadreaderr = ferror(pfp);
674 if (hadreaderr)
675 savederrno = errno;
676 fclose(pfp);
677 pfp = NULL;
678 if (hadreaderr) {
679 cgetclose();
680 errno = savederrno;
681 return (-1);
682 } else {
683 if (*++dbp == NULL) {
684 (void)cgetclose();
685 return (0);
686 } else if ((pfp =
687 fopen(*dbp, "r")) == NULL) {
688 (void)cgetclose();
689 return (-1);
690 } else
691 continue;
692 }
693 } else
694 line[len - 1] = '\0';
695 if (len == 1) {
696 slash = 0;
697 continue;
698 }
699 if (isspace_l((unsigned char)*line, loc) ||
700 *line == ':' || *line == '#' || slash) {
701 if (line[len - 2] == '\\')
702 slash = 1;
703 else
704 slash = 0;
705 continue;
706 }
707 if (line[len - 2] == '\\')
708 slash = 1;
709 else
710 slash = 0;
711 }
712
713
714 /*
715 * Line points to a name line.
716 */
717 i = 0;
718 done = 0;
719 np = nbuf;
720 for (;;) {
721 for (cp = line; *cp != '\0'; cp++) {
722 if (*cp == ':') {
723 *np++ = ':';
724 done = 1;
725 break;
726 }
727 if (*cp == '\\')
728 break;
729 *np++ = *cp;
730 }
731 if (done) {
732 *np = '\0';
733 break;
734 } else { /* name field extends beyond the line */
735 line = fgetln(pfp, &len);
736 if (line == NULL && pfp) {
737 /* Name extends beyond the EOF! */
738 hadreaderr = ferror(pfp);
739 if (hadreaderr)
740 savederrno = errno;
741 fclose(pfp);
742 pfp = NULL;
743 if (hadreaderr) {
744 cgetclose();
745 errno = savederrno;
746 return (-1);
747 } else {
748 cgetclose();
749 return (-1);
750 }
751 } else
752 line[len - 1] = '\0';
753 }
754 }
755 rp = buf;
756 for(cp = nbuf; *cp != '\0'; cp++)
757 if (*cp == '|' || *cp == ':')
758 break;
759 else
760 *rp++ = *cp;
761
762 *rp = '\0';
763 /*
764 * XXX
765 * Last argument of getent here should be nbuf if we want true
766 * sequential access in the case of duplicates.
767 * With NULL, getent will return the first entry found
768 * rather than the duplicate entry record. This is a
769 * matter of semantics that should be resolved.
770 */
771 status = getent(bp, &dummy, db_array, -1, buf, 0, NULL, loc);
772 if (status == -2 || status == -3)
773 (void)cgetclose();
774
775 return (status + 1);
776 }
777 /* NOTREACHED */
778}
779
780/*
781 * Cgetstr retrieves the value of the string capability cap from the
782 * capability record pointed to by buf. A pointer to a decoded, NUL
783 * terminated, malloc'd copy of the string is returned in the char *
784 * pointed to by str. The length of the string not including the trailing
785 * NUL is returned on success, -1 if the requested string capability
786 * couldn't be found, -2 if a system error was encountered (storage
787 * allocation failure).
788 */
789int
790cgetstr(char *buf, const char *cap, char **str)
791{
792 u_int m_room;
793 char *bp, *mp;
794 int len;
795 char *mem;
796
797 /*
798 * Find string capability cap
799 */
800 bp = cgetcap(buf, cap, '=');
801 if (bp == NULL)
802 return (-1);
803
804 /*
805 * Conversion / storage allocation loop ... Allocate memory in
806 * chunks SFRAG in size.
807 */
808 if ((mem = malloc(SFRAG)) == NULL) {
809 errno = ENOMEM;
810 return (-2); /* couldn't even allocate the first fragment */
811 }
812 m_room = SFRAG;
813 mp = mem;
814
815 while (*bp != ':' && *bp != '\0') {
816 /*
817 * Loop invariants:
818 * There is always room for one more character in mem.
819 * Mp always points just past last character in mem.
820 * Bp always points at next character in buf.
821 */
822 if (*bp == '^') {
823 bp++;
824 if (*bp == ':' || *bp == '\0')
825 break; /* drop unfinished escape */
826 if (*bp == '?') {
827 *mp++ = '\177';
828 bp++;
829 } else
830 *mp++ = *bp++ & 037;
831 } else if (*bp == '\\') {
832 bp++;
833 if (*bp == ':' || *bp == '\0')
834 break; /* drop unfinished escape */
835 if ('0' <= *bp && *bp <= '7') {
836 int n, i;
837
838 n = 0;
839 i = 3; /* maximum of three octal digits */
840 do {
841 n = n * 8 + (*bp++ - '0');
842 } while (--i && '0' <= *bp && *bp <= '7');
843 *mp++ = n;
844 }
845 else switch (*bp++) {
846 case 'b': case 'B':
847 *mp++ = '\b';
848 break;
849 case 't': case 'T':
850 *mp++ = '\t';
851 break;
852 case 'n': case 'N':
853 *mp++ = '\n';
854 break;
855 case 'f': case 'F':
856 *mp++ = '\f';
857 break;
858 case 'r': case 'R':
859 *mp++ = '\r';
860 break;
861 case 'e': case 'E':
862 *mp++ = ESC;
863 break;
864 case 'c': case 'C':
865 *mp++ = ':';
866 break;
867 default:
868 /*
869 * Catches '\', '^', and
870 * everything else.
871 */
872 *mp++ = *(bp-1);
873 break;
874 }
875 } else
876 *mp++ = *bp++;
877 m_room--;
878
879 /*
880 * Enforce loop invariant: if no room left in current
881 * buffer, try to get some more.
882 */
883 if (m_room == 0) {
884 size_t size = mp - mem;
885
886 if ((mem = reallocf(mem, size + SFRAG)) == NULL)
887 return (-2);
888 m_room = SFRAG;
889 mp = mem + size;
890 }
891 }
892 *mp++ = '\0'; /* loop invariant let's us do this */
893 m_room--;
894 len = mp - mem - 1;
895
896 /*
897 * Give back any extra memory and return value and success.
898 */
899 if (m_room != 0)
900 if ((mem = reallocf(mem, (size_t)(mp - mem))) == NULL)
901 return (-2);
902 *str = mem;
903 return (len);
904}
905
906/*
907 * Cgetustr retrieves the value of the string capability cap from the
908 * capability record pointed to by buf. The difference between cgetustr()
909 * and cgetstr() is that cgetustr does not decode escapes but rather treats
910 * all characters literally. A pointer to a NUL terminated malloc'd
911 * copy of the string is returned in the char pointed to by str. The
912 * length of the string not including the trailing NUL is returned on success,
913 * -1 if the requested string capability couldn't be found, -2 if a system
914 * error was encountered (storage allocation failure).
915 */
916int
917cgetustr(char *buf, const char *cap, char **str)
918{
919 u_int m_room;
920 char *bp, *mp;
921 int len;
922 char *mem;
923
924 /*
925 * Find string capability cap
926 */
927 if ((bp = cgetcap(buf, cap, '=')) == NULL)
928 return (-1);
929
930 /*
931 * Conversion / storage allocation loop ... Allocate memory in
932 * chunks SFRAG in size.
933 */
934 if ((mem = malloc(SFRAG)) == NULL) {
935 errno = ENOMEM;
936 return (-2); /* couldn't even allocate the first fragment */
937 }
938 m_room = SFRAG;
939 mp = mem;
940
941 while (*bp != ':' && *bp != '\0') {
942 /*
943 * Loop invariants:
944 * There is always room for one more character in mem.
945 * Mp always points just past last character in mem.
946 * Bp always points at next character in buf.
947 */
948 *mp++ = *bp++;
949 m_room--;
950
951 /*
952 * Enforce loop invariant: if no room left in current
953 * buffer, try to get some more.
954 */
955 if (m_room == 0) {
956 size_t size = mp - mem;
957
958 if ((mem = reallocf(mem, size + SFRAG)) == NULL)
959 return (-2);
960 m_room = SFRAG;
961 mp = mem + size;
962 }
963 }
964 *mp++ = '\0'; /* loop invariant let's us do this */
965 m_room--;
966 len = mp - mem - 1;
967
968 /*
969 * Give back any extra memory and return value and success.
970 */
971 if (m_room != 0)
972 if ((mem = reallocf(mem, (size_t)(mp - mem))) == NULL)
973 return (-2);
974 *str = mem;
975 return (len);
976}
977
978/*
979 * Cgetnum retrieves the value of the numeric capability cap from the
980 * capability record pointed to by buf. The numeric value is returned in
981 * the long pointed to by num. 0 is returned on success, -1 if the requested
982 * numeric capability couldn't be found.
983 */
984int
985cgetnum(char *buf, const char *cap, long *num)
986{
987 long n;
988 int base, digit;
989 char *bp;
990
991 /*
992 * Find numeric capability cap
993 */
994 bp = cgetcap(buf, cap, '#');
995 if (bp == NULL)
996 return (-1);
997
998 /*
999 * Look at value and determine numeric base:
1000 * 0x... or 0X... hexadecimal,
1001 * else 0... octal,
1002 * else decimal.
1003 */
1004 if (*bp == '0') {
1005 bp++;
1006 if (*bp == 'x' || *bp == 'X') {
1007 bp++;
1008 base = 16;
1009 } else
1010 base = 8;
1011 } else
1012 base = 10;
1013
1014 /*
1015 * Conversion loop ...
1016 */
1017 n = 0;
1018 for (;;) {
1019 if ('0' <= *bp && *bp <= '9')
1020 digit = *bp - '0';
1021 else if ('a' <= *bp && *bp <= 'f')
1022 digit = 10 + *bp - 'a';
1023 else if ('A' <= *bp && *bp <= 'F')
1024 digit = 10 + *bp - 'A';
1025 else
1026 break;
1027
1028 if (digit >= base)
1029 break;
1030
1031 n = n * base + digit;
1032 bp++;
1033 }
1034
1035 /*
1036 * Return value and success.
1037 */
1038 *num = n;
1039 return (0);
1040}
1041
1042
1043/*
1044 * Compare name field of record.
1045 */
1046static int
1047nfcmp(char *nf, char *rec)
1048{
1049 char *cp, tmp;
1050 int ret;
1051
1052 for (cp = rec; *cp != ':'; cp++)
1053 ;
1054
1055 tmp = *(cp + 1);
1056 *(cp + 1) = '\0';
1057 ret = strcmp(nf, rec);
1058 *(cp + 1) = tmp;
1059
1060 return (ret);
1061}