home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume4 / unaxcess / part1 / Utilities / mvmsg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  4.0 KB  |  173 lines

  1. /*
  2.  * %W% %E% %U% ncoast!bsa %Z%
  3.  * %Z% Copyright (C) 1985 by Brandon S. Allbery, All Rights Reserved %Z%
  4.  */
  5.  
  6. #ifndef lint
  7. static char _SccsId[] = "%W% %E% %U% ncoast!bsa %Z%";
  8. static char _CopyRt[] = "%Z% Copyright (C) 1985 by Brandon S. Allbery %Z%";
  9. #endif  lint
  10.  
  11. #include <stdio.h>
  12. #include <pwd.h>
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15.  
  16. #ifdef SYS3
  17. #define Index        strchr
  18. #define RIndex        strrchr
  19. #else  SYS3
  20. #define Index        index
  21. #define RIndex        rindex
  22. #endif
  23.  
  24. extern long atol();
  25. extern char *Index();
  26. extern struct passwd *getpwuid();
  27.  
  28. main(argc, argv)
  29. char **argv; {
  30.     char msgdir[256], conf[33];
  31.     long msg;
  32.     char *cp, *dp;
  33.     int status;
  34.  
  35.     if (argc != 3) {
  36.         fprintf(stderr, "Usage: mvmsg conf[/n] conf\n");
  37.         exit(1);
  38.     }
  39.     strcpy(msgdir, getpwuid(geteuid())->pw_dir);
  40.     strcat(msgdir, "/msgdir");
  41.     for (cp = argv[1], dp = conf; *cp != '/' && *cp != '\0'; cp++, dp++)
  42.         *dp = *cp;
  43.     *dp = '\0';
  44.     if (*cp == '\0') {
  45.         FILE *fp;
  46.         char tmp[512];
  47.         long limit, cnt;
  48.         
  49.         sprintf(tmp, "%s/%s/himsg", msgdir, conf);
  50.         if ((fp = fopen(tmp, "r")) == (FILE *) 0) {
  51.             fprintf(stderr, "Conference %s: missing himsg...\n", conf);
  52.             exit(7);
  53.         }
  54.         fgets(tmp, sizeof tmp, fp);
  55.         fclose(fp);
  56.         if ((limit = atol(tmp)) <= 0) {
  57.             fprintf(stderr, "Conference %s: invalid himsg...\n", conf);
  58.             exit(8);
  59.         }
  60.         for (cnt = 0, msg = 1; msg <= limit; msg++)
  61.             if ((status = mvmsg(msgdir, conf, msg, argv[2])) > 0)
  62.                 exit(status);
  63.             else if (status == 0)
  64.                 cnt++;
  65.         printf("%ld messages moved from %s to %s.\n", cnt, conf, argv[2]);
  66.         sprintf(tmp, "%s/%s/himsg", msgdir, conf);
  67.         if (unlink(tmp) != 0)
  68.             exit(12);
  69.         sprintf(tmp, "%s/%s", msgdir, conf);
  70.         if (rmdir(tmp) != 0)
  71.             exit(13);
  72.         exit(cnt == 0? 9: 0);
  73.     }
  74.     msg = atol(++cp);
  75.     if (Index(argv[2], '/') != (char *) 0) {
  76.         fprintf(stderr, "Usage: mvmsg conf[/n] conf\n");
  77.         exit(1);
  78.     }
  79.     if ((status = mvmsg(msgdir, conf, msg, argv[2])) == -1)
  80.         fprintf(stderr, "Couldn't read %s/%ld\n", conf, msg);
  81.     exit(status);
  82. }
  83.  
  84. mvmsg(base, conf, msg, dest)
  85. char *base, *conf, *dest;
  86. long msg; {
  87.     char path[512], temp[512];
  88.     long newmsg;
  89.     FILE *ifp, *ofp;
  90.     struct stat sbuf;
  91.     
  92.     sprintf(path, "%s/%s/%ld", base, conf, msg);
  93.     if (stat(path, &sbuf) < 0)
  94.         return -1;    /* likely to be common... */
  95.     sprintf(path, "%s/%s/himsg", base, dest);
  96.     if ((ifp = fopen(path, "r")) == (FILE *) 0) {
  97.         fprintf(stderr, "Conference %s: missing himsg...\n", dest);
  98.         return 2;
  99.     }
  100.     fgets(temp, sizeof temp, ifp);
  101.     fclose(ifp);
  102.     if ((newmsg = atol(temp)) <= 0) {
  103.         fprintf(stderr, "Conference %s: invalid himsg...\n", dest);
  104.         return 2;
  105.     }
  106.     newmsg++;
  107.     sprintf(path, "%s/%s/%ld", base, conf, msg);
  108.     if ((ifp = fopen(path, "r")) == (FILE *) 0) {
  109.         fprintf(stderr, "Conference %s: check permissions on message %ld\n", conf, msg);
  110.         return 3;
  111.     }
  112.     sprintf(path, "%s/%s/%ld", base, dest, newmsg);
  113.     if (stat(path, &sbuf) == 0) {
  114.         fprintf(stderr, "Conference %s: corrupted (himsg incorrect)\n", dest);
  115.         fclose(ifp);
  116.         return 4;
  117.     }
  118.     if ((ofp = fopen(path, "w")) == (FILE *) 0) {
  119.         fprintf(stderr, "Conference %s: check permissions (can't create message)\n", dest);
  120.         fclose(ifp);
  121.         return 5;
  122.     }
  123.     while (fgets(temp, sizeof temp, ifp) != (char *) 0) {
  124.         fputs(temp, ofp);
  125.         if (ferror(ofp)) {
  126.             fprintf(stderr, "Write error on %s/%ld\n", dest, newmsg);
  127.             exit(5);    /* fatal! */
  128.         }
  129.     }
  130.     if (ferror(ifp)) {
  131.         fprintf(stderr, "Read error on %s/%ld\n", conf, msg);
  132.         fclose(ifp);
  133.         fclose(ofp);
  134.         return 6;
  135.     }
  136.     fclose(ifp);
  137.     fclose(ofp);
  138.     sprintf(path, "%s/%s/himsg", base, dest);
  139.     if ((ifp = fopen(path, "w")) == (FILE *) 0) {
  140.         fprintf(stderr, "Conference %s: check permissions on himsg...\n", dest);
  141.         return 2;
  142.     }
  143.     fprintf(ifp, "%ld\n", newmsg);
  144.     fclose(ifp);
  145.     sprintf(path, "%s/%s/%ld", base, conf, msg);
  146.     if (unlink(path) < 0)
  147.         return 10;
  148.     return 0;
  149. }
  150.  
  151. #ifndef BSD4
  152.  
  153. /* Berkeley has a rmdir() system call!  Heaven!  But I'm on sys3 (boo hiss) */
  154.  
  155. rmdir(path)
  156. char *path; {
  157.     int pid, status;
  158.     
  159.     switch (pid = fork()) {
  160.         case -1:
  161.             return -1;
  162.         case 0:
  163.             execl("/bin/rmdir", "rmdir", path, 0);
  164.             exit(-100);
  165.         default:
  166.             while (wait(&status) != pid)
  167.                 ;
  168.             return status;
  169.     }
  170. }
  171.  
  172. #endif BSD4
  173.