]>
git.cameronkatri.com Git - apple_cmds.git/blob - adv_cmds/cap_mkdb/cap_mkdb.c
2 * Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * The NEXTSTEP Software License Agreement specifies the terms
8 * and conditions for redistribution.
10 * @(#)cap_mkdb.c 8.2 (Berkeley) 4/27/95
13 #include <sys/param.h>
26 void db_build
__P((char **));
27 void dounlink
__P((void));
28 void usage
__P((void));
32 char *capdb
, *capname
, buf
[8 * 1024];
38 2048 * 1024, /* cachesize */
44 * Mkcapdb creates a capability hash database for quick retrieval of capability
45 * records. The database contains 2 types of entries: records and references
46 * marked by the first byte in the data. A record entry contains the actual
47 * capability record whereas a reference contains the name (key) under which
48 * the correct record is stored.
58 while ((c
= getopt(argc
, argv
, "f:v")) != EOF
) {
78 * The database file is the first argument if no name is specified.
79 * Make arrangements to unlink it if exit badly.
81 (void)snprintf(buf
, sizeof(buf
), "%s.db", capname
? capname
: *argv
);
82 if ((capname
= strdup(buf
)) == NULL
)
84 if ((capdbp
= dbopen(capname
, O_CREAT
| O_TRUNC
| O_RDWR
,
85 DEFFILEMODE
, DB_HASH
, &openinfo
)) == NULL
)
93 if (capdbp
->close(capdbp
) < 0)
94 err(1, "%s", capname
);
103 (void)unlink(capname
);
107 * Any changes to these definitions should be made also in the getcap(3)
110 #define RECOK (char)0
111 #define TCERR (char)1
112 #define SHADOW (char)2
115 * Db_build() builds the name and capabilty databases according to the
130 for (reccnt
= 0, bplen
= 0; (st
= cgetnext(&bp
, ifiles
)) > 0;) {
133 * Allocate enough memory to store record, terminating
134 * NULL and one extra byte.
137 if (bplen
<= len
+ 2) {
138 bplen
+= MAX(256, len
+ 2);
139 if ((data
.data
= realloc(data
.data
, bplen
)) == NULL
)
143 /* Find the end of the name field. */
144 if ((p
= strchr(bp
, ':')) == NULL
) {
145 warnx("no name field: %.*s", MIN(len
, 20), bp
);
149 /* First byte of stored record indicates status. */
152 ((char *)(data
.data
))[0] = RECOK
;
155 ((char *)(data
.data
))[0] = TCERR
;
156 warnx("Record not tc expanded: %.*s", p
- bp
, bp
);
160 /* Create the stored record. */
161 memmove(&((u_char
*)(data
.data
))[1], bp
, len
+ 1);
164 /* Store the record under the name field. */
168 switch(capdbp
->put(capdbp
, &key
, &data
, R_NOOVERWRITE
)) {
173 warnx("ignored duplicate: %.*s",
174 key
.size
, (char *)key
.data
);
179 /* If only one name, ignore the rest. */
180 if ((p
= strchr(bp
, '|')) == NULL
)
183 /* The rest of the names reference the entire name. */
184 ((char *)(data
.data
))[0] = SHADOW
;
185 memmove(&((u_char
*)(data
.data
))[1], key
.data
, key
.size
);
186 data
.size
= key
.size
+ 1;
188 /* Store references for other names. */
189 for (p
= t
= bp
;; ++p
) {
190 if (p
> t
&& (*p
== ':' || *p
== '|')) {
193 switch(capdbp
->put(capdbp
,
194 &key
, &data
, R_NOOVERWRITE
)) {
199 warnx("ignored duplicate: %.*s",
200 key
.size
, (char *)key
.data
);
211 err(1, "file argument");
214 errx(1, "potential reference loop detected");
219 (void)printf("cap_mkdb: %d capability records\n", reccnt
);
225 (void)fprintf(stderr
,
226 "usage: cap_mkdb [-v] [-f outfile] file1 [file2 ...]\n");