home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume22 / pathalias10 / part03 / makedb.c < prev    next >
C/C++ Source or Header  |  1990-06-07  |  2KB  |  130 lines

  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. }
  130.