1: /* pathalias -- by steve bellovin, as told to peter honeyman */
   2: #ifndef lint
   3: static char *sccsid = "@(#)makedb.c	9.1 87/10/04";
   4: #endif /* lint */
   5: 
   6: #include <stdio.h>
   7: #include "config.h"
   8: 
   9: typedef struct {
  10:     char *dptr;
  11:     int dsize;
  12: } datum;
  13: 
  14: char *Ofile = ALIASDB, *ProgName;
  15: 
  16: #define USAGE "%s [-o dbmname] [-a] [file ...]\n"
  17: 
  18: main(argc, argv)
  19:     char *argv[];
  20: {   char *ofptr;
  21:     int c, append = 0;
  22:     extern int optind;
  23:     extern char *optarg;
  24: 
  25:     ProgName = argv[0];
  26:     while ((c = getopt(argc, argv, "o:a")) != EOF)
  27:         switch(c) {
  28:         case 'o':   /* dbm output file */
  29:             Ofile = optarg;
  30:             break;
  31: 
  32:         case 'a':   /* append mode */
  33:             append++;
  34:             break;
  35: 
  36:         default:
  37:             fprintf(stderr, USAGE, ProgName);
  38:             exit(1);
  39:             break;
  40:         }
  41: 
  42: 
  43:     if ((ofptr = rindex(Ofile, '/')) != 0)
  44:         ofptr++;
  45:     else
  46:         ofptr = Ofile;
  47:     if (strlen(ofptr) > 10) {
  48:         ofptr[10] = 0;
  49:         fprintf(stderr, "%s: using %s for dbm output\n", ProgName, Ofile);
  50:     }
  51: 
  52:     if (append == 0 && dbfile(Ofile) != 0) {
  53:         perror_(Ofile);
  54:         exit(1);
  55:     }
  56: 
  57:     if (dbminit(Ofile) < 0) {
  58:         perror_(Ofile);
  59:         exit(1);
  60:     }
  61: 
  62:     if (optind == argc)
  63:         makedb((char *) 0);
  64:     else for ( ; optind < argc; optind++)
  65:         makedb(argv[optind]);
  66:     exit(0);
  67: }
  68: 
  69: dbfile(dbf)
  70:     char *dbf;
  71: {
  72:     return (dbcreat(dbf, "dir") != 0 || dbcreat(dbf, "pag") != 0);
  73: }
  74: 
  75: dbcreat(dbf, suffix)
  76:     char *dbf, *suffix;
  77: {   char buf[BUFSIZ];
  78:     int fd;
  79: 
  80:     (void) sprintf(buf, "%s.%s", dbf, suffix);
  81:     if ((fd = creat(buf, 0666)) < 0)
  82:         return(-1);
  83:     (void) close(fd);
  84:     return(0);
  85: }
  86: 
  87: 
  88: makedb(ifile)
  89:     char *ifile;
  90: {   char line[BUFSIZ];
  91:     datum key, val;
  92: 
  93:     if (ifile && (freopen(ifile, "r", stdin) == NULL)) {
  94:         perror_(ifile);
  95:         return;
  96:     }
  97: 
  98:     /*
  99: 	 * keys and values are 0 terminated.  this wastes time and (disk) space,
 100: 	 * but does lend simplicity and backwards compatibility.
 101: 	 */
 102:     key.dptr = line;
 103:     while (fgets(line, sizeof(line), stdin) != NULL) {
 104:         char *op, *end;
 105: 
 106:         end = line + strlen(line);
 107:         end[-1] = 0;    /* kill newline, stuff null terminator */
 108:         op = index(line, '\t');
 109:         if (op != 0) {
 110:             *op++ = 0;
 111:             key.dsize = op - line;      /* 0 terminated */
 112:             val.dptr = op;
 113:             val.dsize = end - op;       /* 0 terminated */
 114:         } else {
 115:             key.dsize = end - line;     /* 0 terminated */
 116:             val.dptr = "\0";        /* why must i do this? */
 117:             val.dsize = 1;
 118:         }
 119:         if (store(key, val) < 0)
 120:             perror_(Ofile);
 121:     }
 122: }
 123: 
 124: perror_(str)
 125:     char    *str;
 126: {
 127:     fprintf(stderr, "%s: ", ProgName);
 128:     perror(str);
 129: }

Defined functions

dbcreat defined in line 75; used 2 times
  • in line 72(2)
dbfile defined in line 69; used 1 times
  • in line 52
main defined in line 18; never used
makedb defined in line 88; used 2 times
perror_ defined in line 124; used 4 times

Defined variables

Ofile defined in line 14; used 9 times
sccsid defined in line 3; never used

Defined macros

USAGE defined in line 16; used 1 times
  • in line 37
Last modified: 1987-11-29
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1225
Valid CSS Valid XHTML 1.0 Strict