home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume34 / unpackmaps / part01 / unpacker.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-29  |  5.1 KB  |  247 lines

  1. /* Copyright 1992, Chris Lewis.  All Rights Reserved
  2.    Please see the README for the terms of the copyright.
  3.    1.3 92/07/12
  4.  */
  5.  
  6. #ifndef lint
  7. static char SCCSid[] = "@(#)unpacker.c 1.3 92/07/12 16:33:55";
  8. #endif
  9. #define    UNPACKMAPS
  10. #define    INIT(x)    = x
  11. #include "unpack.h"
  12.  
  13. #include <signal.h>
  14.  
  15. quit() {
  16.     fatal(1, "Interrupted");
  17. }
  18.  
  19. char *
  20. checkabs(value)
  21. char *value; {
  22.     static char pwd[512];
  23.     static char buf[512];
  24.     if (*value != '/') {
  25.     if (!*pwd) {
  26.         FILE *p;
  27.         char *bp;
  28.         p = popen("pwd", "r");
  29.         if (!p) {
  30.         fprintf(stderr, "%s: can't popen pwd!\n", progname);
  31.         exit(1);
  32.         }
  33.         fgets(pwd, sizeof(pwd), p);
  34.         pclose(p);
  35.         bp = strchr(pwd, '\n');
  36.         if (bp)
  37.         *bp = '\0';
  38.     }
  39.     strcpy(buf, pwd);
  40.     strcat(buf, "/");
  41.     strcat(buf, value);
  42.     return(buf);
  43.     } else
  44.     return(value);
  45. }
  46.  
  47.  
  48. main(argc, argv)
  49. int argc;
  50. char **argv; {
  51.     extern int optind;
  52.     extern char *optarg;
  53.     int c;
  54.  
  55.     progname = strrchr(argv[0], '/');
  56.     if (progname)
  57.     progname++;
  58.     else
  59.     progname = argv[0];
  60.     
  61.     
  62.     while((c = getopt(argc, argv, "vM:tn:VI:cf:s:d:uXpPm:iwWl:U")) != EOF)
  63.     switch(c) {
  64.         case 'V':
  65.         (void) printf("%s\n", VERSION);
  66.         exit(0);
  67.         case 'v':
  68.         verbose = 1;
  69.         break;
  70.         case 't':
  71.         dontunpack = 1;
  72.         break;
  73.         case 'I':
  74.         internet = optarg;
  75.         break;
  76.         case 'l':
  77.         (void) strcpy(tempbuf, optarg);
  78.         break;
  79.         case 'U':
  80.         unlinkskip = 1;
  81.         break;
  82.         case 'W':
  83.         forcepath = 1;
  84.         /*fallthru*/
  85.         case 'w':
  86.         whereonly = runpath = 1;
  87.         break;
  88.         case 'c':
  89.         compflag = 1;
  90.         break;
  91.         case 'i':
  92.         initialize = 1;
  93.         break;
  94.         case 'f':
  95.         savestr(&localmaps, checkabs(optarg));
  96.         break;
  97.         case 'X':
  98.         debug = 1;
  99.         break;
  100.         case 's':
  101.         (void) strcpy(spooldir, checkabs(optarg));
  102.         break;
  103.         case 'd':
  104.         (void) strcpy(unpackdir, checkabs(optarg));
  105.         break;
  106.         case 'P':
  107.         forcepath = runpath = 1;
  108.         break;
  109.         case 'M':
  110.         pathalias = optarg;
  111.         runpath = 1;
  112.         break;
  113.         case 'm':
  114.         (void) strcpy(pathfile, checkabs(optarg));
  115.         case 'p':
  116.         runpath = 1;
  117.         break;
  118.         case 'u':
  119.         unlinkflag = 1;
  120.         break;
  121.         case 'n':
  122.         notify = optarg;
  123.         break;
  124.         case '?':
  125.         usage = 1;
  126.         break;
  127.     }
  128.  
  129.     if (usage) {
  130.     static char *puse[] = {
  131.         "usage: unpacker <options>",
  132.         "  options: -f <local map files>",
  133.         "           -i   (initialize from comp.mail.maps group)",
  134.         "           -X   (debug)",
  135.         "           -c   (compress maps)",
  136.         "           -s <spooldir>",
  137.         "           -d <map/unpackdir>",
  138.         "           -m <output map> (implies -p)",
  139.         "           -M <pathalias pipeline) (implies -p)",
  140.         "           -p   (run pathalias if paths out of date)",
  141.         "           -P   (run pathalias unconditionally)",
  142.         "           -u   (unlink maps after processing)",
  143.         "           -V   (print unpackmaps version)",
  144.         "           -v   (verbose diagnostics)",
  145.         "           -w   (do where file only if out of date)",
  146.         "           -W   (do where file unconditionally)",
  147.         "           -U   (unlink maps mentioned in ignorefile)",
  148.         "           -l <ignorefile>",
  149.         "           -n <mail command to send output>",
  150.         "           -t   (don't unpack - ignore newsgroup)",
  151.         NULL
  152.     };
  153.     register char **p;
  154.  
  155.     for (p = puse; *p; p++)
  156.         (void) fprintf(stderr, "%s\n", *p);
  157.  
  158.     exit(1);
  159.     }
  160.  
  161.     if (!dontunpack)
  162.     (void) checkids();
  163.  
  164.     if (SIG_DFL == signal(SIGINT, SIG_IGN))
  165.     signal(SIGINT, quit);
  166.     (void) signal(SIGTERM, quit);
  167.  
  168.     togofile = makepath(unpackdir, "togo");
  169.     workfile = makepath(unpackdir, "togo.work");
  170.     wheredb = makepath(unpackdir, "where.db");
  171.  
  172.  
  173.     /* gets linked, so must be in unpackdir */
  174.     tempfile = makepath(unpackdir, "_temp_");
  175.  
  176.     if (notify) {
  177.  
  178.     /* Normally these should be created ala mktemp, but we have
  179.        to make sure that we clobber on subsequent runs.
  180.        Otherwise, there goes your disk if something *really*
  181.        wierd goes wrong and they don't get deleted for some reason. */
  182.  
  183.     wheretmp = makepath(TMPDIR2, "UNPAwhere");
  184.     pathtmp = makepath(TMPDIR, "UNPApathtmp1");
  185.     pathtmp2 = makepath(TMPDIR2, "UNPApathtmp2");
  186.     } else {
  187.     wheretmp = makepath(TMPDIR2, ".where");
  188.     pathtmp = makepath(TMPDIR, ".pathtmp1");
  189.     pathtmp2 = makepath(TMPDIR2, ".pathtmp2");
  190.     }
  191.  
  192.     unlink(tempfile);
  193.     unlink(wheretmp);
  194.     unlink(pathtmp);
  195.     unlink(pathtmp2);
  196.  
  197.     if (tempbuf[0])
  198.     (void) keeplist(tempbuf);
  199.  
  200.     if (notify)
  201.     (void) startlog();
  202.     
  203.     if (!dontunpack)
  204.     (void) unpack();
  205.  
  206.     if (runpath)
  207.     (void) dorunpath();
  208.  
  209.     myexit(0);
  210.     /* NOTREACHED */
  211. }
  212.  
  213. keeplist(file)
  214. char *file; {
  215.     FILE *f;
  216.     register char *p;
  217.     fatal(!(f = fopen(file, "r")), "Can't open keeplist");
  218.  
  219.     while(fgets(tempbuf, sizeof(tempbuf), f)) {
  220.  
  221.     p = strchr(tempbuf, '\n');
  222.  
  223.     if (p)
  224.         *p = '\0';
  225.  
  226.     p = strrchr(tempbuf, '.');
  227.     if (p && 0 == strcmp(p, ".Z"))
  228.         *p = '\0';
  229.  
  230.     if (tempbuf[0])
  231.         savestr(&lmapfiles, tempbuf);
  232.     }
  233.     (void) fclose(f);
  234. }
  235.  
  236. #include <pwd.h>
  237. checkids() {
  238.     struct passwd *p;
  239.     extern struct passwd *getpwnam();
  240.  
  241.     fatal(!(p = getpwnam(NEWSID)), "Can't getpwnam(NEWSID)");
  242.     if (geteuid() == 0)
  243.     (void) setuid(p->pw_uid);
  244.     fatal(p->pw_uid != geteuid() || p->pw_uid != getuid(),
  245.     "Must be run as NEWSID");
  246. }
  247.