* Copyright (c) 1990 Jan-Simon Pendry
* Copyright (c) 1990 Imperial College of Science, Technology & Medicine
* Copyright (c) 1990 The Regents of the University of California.
* This code is derived from software contributed to Berkeley by
* Jan-Simon Pendry at Imperial College, London.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* @(#)mk-amd-map.c 5.4 (Berkeley) 5/12/91
* $Id: mk-amd-map.c,v 5.2.1.4 91/05/07 22:18:47 jsp Alpha $
* Convert a file map into an ndbm map
@(#)Copyright (c) 1990 Jan-Simon Pendry\n\
@(#)Copyright (c) 1990 Imperial College of Science, Technology & Medicine\n\
@(#)Copyright (c) 1990 The Regents of the University of California.\n\
@(#)All rights reserved.\n";
static char rcsid
[] = "$Id: mk-amd-map.c,v 5.2.1.4 91/05/07 22:18:47 jsp Alpha $";
static char sccsid
[] = "@(#)mk-amd-map.c 5.4 (Berkeley) 5/12/91";
#define create_database(name) dbm_open(name, O_RDWR|O_CREAT, 0444)
static int store_data(db
, k
, v
)
key
.dptr
= k
; val
.dptr
= v
;
key
.dsize
= strlen(k
) + 1;
val
.dsize
= strlen(v
) + 1;
return dbm_store((DBM
*) db
, key
, val
, DBM_INSERT
);
static int read_line(buf
, size
, fp
)
while (fgets(buf
, size
, fp
)) {
if (len
> 1 && buf
[len
-2] == '\\' &&
*buf
= '\n'; buf
[1] = '\0';
* Skip leading white space on next line
while ((ch
= getc(fp
)) != EOF
&&
isascii(ch
) && isspace(ch
))
} while (size
> 0 && !feof(fp
));
static int read_file(fp
, map
, db
)
while (read_line(key_val
, sizeof(key_val
), fp
)) {
int len
= strlen(key_val
);
* Make sure we got the whole line
if (key_val
[len
-1] != '\n') {
fprintf(stderr
, "line %d in \"%s\" is too long", line_no
, map
);
hash
= strchr(key_val
, '#');
for (kp
= key_val
; *kp
&& isascii(*kp
) && isspace(*kp
); kp
++)
for (cp
= kp
; *cp
&&(!isascii(*cp
)||!isspace(*cp
)); cp
++)
* Check whether key matches, or whether
* the entry is a wildcard entry.
while (*cp
&& isascii(*cp
) && isspace(*cp
))
fprintf(stderr
, "Can't interpolate %s\n", kp
);
if (store_data(db
, kp
, cp
) < 0) {
fprintf(stderr
, "Could store %s -> %s\n", kp
, cp
);
printf("%s\t%s\n", kp
, cp
);
fprintf(stderr
, "%s: line %d has no value field", map
, line_no
);
* If the last read didn't get a whole line then
* throw away the remainder before continuing...
while (fgets(key_val
, sizeof(key_val
), fp
) &&
static int remove_file(f
)
if (unlink(f
) < 0 && errno
!= ENOENT
)
static char maptmp
[] = "dbmXXXXXX";
char maptpag
[16], maptdir
[16];
while ((ch
= getopt(argc
, argv
, "p")) != EOF
)
if (usage
|| optind
!= (argc
- 1)) {
fputs("Usage: mk-amd-map [-p] file-map\n", stderr
);
fputs("Can't chdir to ", stderr
);
mappag
= (char *) malloc(len
+ 5);
mapdir
= (char *) malloc(len
+ 5);
if (!mappag
|| !mapdir
) {
perror("mk-amd-map: malloc");
sprintf(maptpag
, "%s.pag", maptmp
);
sprintf(maptdir
, "%s.dir", maptmp
);
if (remove_file(maptpag
) < 0 || remove_file(maptdir
) < 0) {
fprintf(stderr
, "Can't remove existing temporary files; %s and", maptpag
);
mapd
= create_database(maptmp
);
int error
= read_file(mapf
, map
, mapd
);
fprintf(stderr
, "Error creating ndbm map for %s\n", map
);
fprintf(stderr
, "Error reading source file %s\n", map
);
sprintf(mappag
, "%s.pag", map
);
sprintf(mapdir
, "%s.dir", map
);
if (rename(maptpag
, mappag
) < 0) {
fprintf(stderr
, "Couldn't rename %s to ", maptpag
);
/* Throw away the temporary map */
} else if (rename(maptdir
, mapdir
) < 0) {
fprintf(stderr
, "Couldn't rename %s to ", maptdir
);
/* Put the .pag file back */
/* Throw away remaining part of original map */
"WARNING: existing map \"%s.{dir,pag}\" destroyed\n",
fprintf(stderr
, "Can't open \"%s.{dir,pag}\" for ", map
);
fputs("mk-amd-map: This system does not support hashed database files\n", stderr
);
#endif /* HAS_DATABASE */