4 * Copyright (c) 1997-2009 Erez Zadok
5 * Copyright (c) 1990 Jan-Simon Pendry
6 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
7 * Copyright (c) 1990 The Regents of the University of California.
10 * This code is derived from software contributed to Berkeley by
11 * Jan-Simon Pendry at Imperial College, London.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgment:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * File: am-utils/mk-amd-map/mk-amd-map.c
46 * Convert a file map into an ndbm map
51 #endif /* HAVE_CONFIG_H */
54 /* (libdb version 2) uses .db extensions but an old dbm API */
55 /* check for libgdbm to distinguish it from linux systems */
56 #if defined(DBM_SUFFIX) && !defined(HAVE_LIBGDBM)
57 # define HAVE_DB_SUFFIX
58 #endif /* not defined(DBM_SUFFIX) && !defined(HAVE_LIBGDBM) */
63 store_data(voidp db
, char *k
, char *v
)
69 key
.dsize
= strlen(k
) + 1;
70 val
.dsize
= strlen(v
) + 1;
71 return dbm_store((DBM
*) db
, key
, val
, DBM_INSERT
);
76 * Read one line from file.
79 read_line(char *buf
, int size
, FILE *fp
)
84 while (fgets(buf
, size
, fp
)) {
85 int len
= strlen(buf
);
88 if (len
> 1 && buf
[len
- 2] == '\\' && buf
[len
- 1] == '\n') {
96 * Skip leading white space on next line
98 while ((ch
= getc(fp
)) != EOF
&& isascii((unsigned char)ch
) && isspace((unsigned char)ch
)) ;
99 (void) ungetc(ch
, fp
);
104 } while (size
> 0 && !feof(fp
));
111 * Read through a map.
114 read_file(FILE *fp
, char *map
, voidp db
)
121 while (read_line(key_val
, 2048, fp
)) {
125 int len
= strlen(key_val
);
130 * Make sure we got the whole line
132 if (key_val
[len
- 1] != '\n') {
133 fprintf(stderr
, "line %d in \"%s\" is too long", line_no
, map
);
136 key_val
[len
- 1] = '\0';
142 hash
= strchr(key_val
, '#');
149 for (kp
= key_val
; *kp
&& isascii((unsigned char)*kp
) && isspace((unsigned char)*kp
); kp
++) ;
160 for (cp
= kp
; *cp
&& (!isascii((unsigned char)*cp
) || !isspace((unsigned char)*cp
)); cp
++) ;
163 * Check whether key matches, or whether
164 * the entry is a wildcard entry.
168 while (*cp
&& isascii((unsigned char)*cp
) && isspace((unsigned char)*cp
))
171 fprintf(stderr
, "Can't interpolate %s\n", kp
);
175 if (store_data(db
, kp
, cp
) < 0) {
176 fprintf(stderr
, "Could store %s -> %s\n", kp
, cp
);
180 printf("%s\t%s\n", kp
, cp
);
183 fprintf(stderr
, "%s: line %d has no value field", map
, line_no
);
189 * If the last read didn't get a whole line then
190 * throw away the remainder before continuing...
193 while (fgets(key_val
, sizeof(key_val
), fp
) &&
194 !strchr(key_val
, '\n')) ;
205 if (unlink(f
) < 0 && errno
!= ENOENT
)
213 main(int argc
, char *argv
[])
215 FILE *mapf
; /* the input file to read from */
219 static char maptmp
[] = "dbmXXXXXX";
220 #ifdef HAVE_DB_SUFFIX
222 char *map_name_db
= (char *) NULL
;
223 #else /* not HAVE_DB_SUFFIX */
224 char maptpag
[16], maptdir
[16];
225 char *map_name_pag
= (char *) NULL
, *map_name_dir
= (char *) NULL
;
226 #endif /* not HAVE_DB_SUFFIX */
235 while ((ch
= getopt(argc
, argv
, "p")) != -1)
245 if (usage
|| optind
!= (argc
- 1)) {
246 fputs("Usage: mk-amd-map [-p] file-map\n", stderr
);
249 mapsrc
= argv
[optind
];
251 /* test if can get to the map directory */
252 sl
= strrchr(mapsrc
, '/');
255 if (chdir(mapsrc
) < 0) {
256 fputs("Can't chdir to ", stderr
);
263 /* open source file */
264 mapf
= fopen(mapsrc
, "r");
266 fprintf(stderr
, "cannot open source file ");
272 signal(SIGINT
, SIG_IGN
);
276 /* enough space for ".db" or ".pag" or ".dir" appended */
277 l
= strlen(mapsrc
) + 5;
278 #ifdef HAVE_DB_SUFFIX
279 map_name_db
= (char *) malloc(l
);
280 error
= (map_name_db
== NULL
);
281 #else /* not HAVE_DB_SUFFIX */
282 map_name_pag
= (char *) malloc(l
);
283 map_name_dir
= (char *) malloc(l
);
284 error
= (map_name_pag
== NULL
|| map_name_dir
== NULL
);
285 #endif /* not HAVE_DB_SUFFIX */
287 perror("mk-amd-map: malloc");
294 * XXX: hack to avoid compiler complaints about mktemp not being
295 * secure, since we have to do a dbm_open on this anyway. So use
296 * mkstemp if you can, and then close the fd, but we get a safe
297 * and unique file name.
300 dummyfd
= mkstemp(maptmp
);
304 #else /* not HAVE_MKSTEMP */
306 #endif /* not HAVE_MKSTEMP */
308 /* remove existing temps (if any) */
309 #ifdef HAVE_DB_SUFFIX
310 xsnprintf(maptdb
, sizeof(maptdb
), "%s.db", maptmp
);
311 if (remove_file(maptdb
) < 0) {
312 fprintf(stderr
, "Can't remove existing temporary file; ");
316 #else /* not HAVE_DB_SUFFIX */
317 xsnprintf(maptpag
, sizeof(maptpag
), "%s.pag", maptmp
);
318 xsnprintf(maptdir
, sizeof(maptdir
), "%s.dir", maptmp
);
319 if (remove_file(maptpag
) < 0 || remove_file(maptdir
) < 0) {
320 fprintf(stderr
, "Can't remove existing temporary files; %s and ", maptpag
);
324 #endif /* not HAVE_DB_SUFFIX */
326 db
= dbm_open(maptmp
, O_RDWR
|O_CREAT
|O_EXCL
, 0444);
328 fprintf(stderr
, "cannot initialize temporary database: %s", maptmp
);
333 /* print db to stdout or to temp database */
334 error
= read_file(mapf
, mapsrc
, db
);
338 fprintf(stderr
, "Error reading source file %s\n", mapsrc
);
340 fprintf(stderr
, "Error creating database map for %s\n", mapsrc
);
345 exit(0); /* nothing more to do */
347 /* if gets here, we wrote to a database */
352 #ifdef HAVE_DB_SUFFIX
353 /* sizeof(map_name_db) is malloc'ed above */
354 xsnprintf(map_name_db
, l
, "%s.db", mapsrc
);
355 if (rename(maptdb
, map_name_db
) < 0) {
356 fprintf(stderr
, "Couldn't rename %s to ", maptdb
);
358 /* Throw away the temporary map */
362 #else /* not HAVE_DB_SUFFIX */
363 /* sizeof(map_name_{pag,dir}) are malloc'ed above */
364 xsnprintf(map_name_pag
, l
, "%s.pag", mapsrc
);
365 xsnprintf(map_name_dir
, l
, "%s.dir", mapsrc
);
366 if (rename(maptpag
, map_name_pag
) < 0) {
367 fprintf(stderr
, "Couldn't rename %s to ", maptpag
);
368 perror(map_name_pag
);
369 /* Throw away the temporary map */
374 if (rename(maptdir
, map_name_dir
) < 0) {
375 fprintf(stderr
, "Couldn't rename %s to ", maptdir
);
376 perror(map_name_dir
);
377 /* remove the (presumably bad) .pag file */
378 unlink(map_name_pag
);
379 /* throw away remaining part of original map */
380 unlink(map_name_dir
);
381 /* throw away the temporary map */
383 fprintf(stderr
, "WARNING: existing map \"%s.{dir,pag}\" destroyed\n",
387 #endif /* not HAVE_DB_SUFFIX */
392 #else /* not HAVE_MAP_NDBM */
397 fputs("mk-amd-map: This system does not support hashed database files\n", stderr
);
401 #endif /* not HAVE_MAP_NDBM */