home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume34 / unpackmaps / part01 / uuwhere.c < prev   
C/C++ Source or Header  |  1992-11-29  |  4KB  |  180 lines

  1. /* Copyright 1992, Chris Lewis.  All Rights Reserved
  2.    Please see the README for the terms of the copyright.
  3.    1.2 92/08/14
  4.  */
  5.  
  6. #ifndef lint
  7. static char SCCSid[] = "@(#)uuwhere.c 1.2 92/08/14 20:48:55";
  8. #endif
  9. #define    INIT(x)    = x
  10. #include "unpack.h"
  11.  
  12. int verbose;
  13. int lflag;
  14. int emitmap;
  15.  
  16. int rc = 0;
  17.  
  18. main(argc, argv)
  19. int argc;
  20. char **argv; {
  21.     int cost;
  22.     register int i;
  23.     char buf[BUFSIZ];
  24.     char smarthost[BUFSIZ];
  25.     int smarthosted;
  26.     int c;
  27.     char *region = NULL;
  28.     register char **p, *rp;
  29.     extern char *optarg, *gpathdata;
  30.     extern int optind;
  31.     extern long pathlength;
  32.  
  33.     progname = argv[0];
  34.  
  35.     while((c = getopt(argc, argv, "Xd:m:evr:l")) != EOF)
  36.     switch(c) {
  37.         case 'd':
  38.         (void) strcpy(unpackdir, optarg);
  39.         break;
  40.         case 'm':
  41.         (void) strcpy(pathfile, optarg);
  42.         break;
  43.         case 'v':
  44.         verbose = 1;
  45.         break;
  46.         case 'e':
  47.         emitmap = 1;
  48.         break;
  49.         case 'l':
  50.         lflag = 1;
  51.         break;
  52.         case 'r':
  53.         region = optarg;
  54.         break;
  55.         case 'X':
  56.         debug = 1;
  57.         break;
  58.         case '?':
  59.         usage = 1;
  60.         break;
  61.     }
  62.  
  63.     if (usage) {
  64.     static char *uuse[] = {
  65.         "usage: uuwhere <options>",
  66.         "  options: -d mapdir      map directory",
  67.         "           -m pathfile    pathalias map file",
  68.         "           -e             display map entry)",
  69.         "           -v             display interesting path information",
  70.         "           -l             display map file names",
  71.         "           -r region      display map (region) file",
  72.         NULL
  73.     };
  74.  
  75.     for (p = uuse; *p; p++)
  76.         (void) fprintf(stderr, "%s\n", *p);
  77.  
  78.     exit(1);
  79.     }
  80.  
  81.  
  82.     if (chdir(unpackdir)) {
  83.     (void) fprintf(stderr, "%s: Can't change to %s\n", progname, unpackdir);
  84.     exit(1);
  85.     }
  86.  
  87.     wheredb = makepath(unpackdir, "where.db");
  88.     if (debug)
  89.     (void) fprintf(stderr, "wheredb: %s\n", wheredb);
  90.  
  91.     if (lflag)
  92.     exit(dolist());
  93.  
  94.     if (region) {
  95.     int list[2];
  96.     list[0] = 1;
  97.     list[1] = 0;
  98.     exit(dumpfile(region, list, 0));
  99.     }
  100.  
  101.     gpathdata = pathfile;
  102.     pathlength = 0;
  103.  
  104.     if (getpath("smart-host", smarthost, &cost))
  105.     smarthost[0] = '\0';
  106.     
  107.     for (i = optind; i < argc; i++) {
  108.     buf[0] = 0;
  109.     smarthosted = 0;
  110.     if (emitmap)
  111.         pathlength = 0;
  112.     if (getpath(argv[i], buf, &cost)) {
  113.         if (smarthost[0]) {
  114.         (void) sprintf(buf, smarthost, argv[i]);
  115.         smarthosted = 1;
  116.         rc |= 2;
  117.         } else {
  118.         buf[0] = '\0';
  119.         rc |= 4;
  120.         }
  121.     } else {
  122.         rp = strrchr(buf, '!');
  123.         if (rp)
  124.         *rp = '\0';
  125.         else
  126.         (void) strcpy(buf, argv[i]);
  127.     }
  128.  
  129.     if (verbose) {
  130.         (void) printf("route %s: %s!<user>", argv[i], buf[0]? buf: "<noroute>");
  131.         if (smarthosted)
  132.         (void) printf(" (smarthosted)");
  133.         (void) putchar('\n');
  134.     } else
  135.         if (buf[0])
  136.         (void) printf("%s\n", buf);
  137.     if (emitmap)
  138.         (void) dumpmap(argv[i]);
  139.     }
  140.     exit(rc);
  141.     /* NOTREACHED */
  142. }
  143.  
  144. struct stringlist sl;
  145. dolist() {
  146.     register char *p, *q;
  147.     char **mapptr;
  148.     struct stat stb;
  149.     extern char *ctime();
  150.  
  151.     getmaps(&sl, 0);
  152.     for (mapptr = sl.list; *mapptr; mapptr++)
  153.     if (verbose) {
  154.         if (stat(*mapptr, &stb))
  155.         (void) fprintf(stderr, "%s: can't stat %s\n", progname, *mapptr);
  156.         else {
  157.         p = ctime(&stb.st_mtime);
  158.         q = strchr(p, '\n');
  159.         if (q)
  160.             *q = '\0';
  161.         (void) printf("%-14s %6ld %s\n", *mapptr, stb.st_size, p);
  162.         }
  163.     } else {
  164.         q = strrchr(*mapptr, '.');
  165.         if (q && strcmp(q, ".Z") == 0)
  166.         *q = '\0';
  167.         (void) printf("%s\n", *mapptr);
  168.     }
  169.     return(0);
  170. }
  171.  
  172. fatal(e, str)
  173. int e;
  174. char *str; {
  175.     if (e) {
  176.     (void) fprintf(stderr, "%s: %s\n", progname, str);
  177.     exit(e);
  178.     }
  179. }
  180.