home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume4 / unaxcess / part1 / Utilities / uanews.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  5.7 KB  |  221 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. #define NEWSRC        "%newslink"    /* simplified .newsrc */
  17. #define ACTIVE        "/usr/lib/news/active"
  18. #define NEWSDIR        "/usr/spool/news"
  19. /* no need to define special if you're 2.10.2; we can intuit the new active file */
  20.  
  21. #ifdef SYS3
  22. #define Index        strchr
  23. #define RIndex        strrchr
  24. #else  SYS3
  25. #define Index        index
  26. #define RIndex        rindex
  27. #endif
  28.  
  29. extern long atol();
  30. extern char *Index();
  31. extern struct passwd *getpwuid();
  32.  
  33. extern int errno;
  34.  
  35. main(argc, argv)
  36. char **argv; {
  37.     char msgdir[256], conf[33];
  38.     long msg;
  39.     char *cp, *dp;
  40.     int status;
  41.  
  42.     if (argc != 3) {
  43.         fprintf(stderr, "Usage: uanews newsgroup[/article] conf\n");
  44.         exit(1);
  45.     }
  46.     strcpy(msgdir, getpwuid(geteuid())->pw_dir);
  47.     strcat(msgdir, "/msgdir");
  48.     for (cp = argv[1], dp = conf; *cp != '/' && *cp != '\0'; cp++, dp++)
  49.         *dp = *cp;
  50.     *dp = '\0';
  51.     if (*cp == '\0') {
  52.         FILE *fp;
  53.         char tmp[512], ng[512];
  54.         long limit, cnt, minact;
  55.         
  56.         if ((fp = fopen(ACTIVE, "r")) == (FILE *) 0) {
  57.             fprintf(stderr, "Not a Usenet site (no active file)...\n", conf);
  58.             exit(7);
  59.         }
  60.         while (fgets(tmp, sizeof tmp, fp) != NULL) {
  61.             if (sscanf(tmp, "%s %ld %ld", ng, &limit, &minact) == 2)
  62.                 minact = 1;    /* < 2.10.2 */
  63.             if (strcmp(ng, conf) == 0)
  64.                 break;
  65.         }
  66.         fclose(fp);
  67.         if (strcmp(ng, conf) != 0) {
  68.             fprintf(stderr, "I can't find a newsgroup called %s.\n", conf);
  69.             exit(13);
  70.         }
  71.         if (minact == limit) {
  72.             fprintf(stderr, "Newsgroup %s is empty.\n", conf);
  73.             exit(14);
  74.         }
  75.         sprintf(tmp, "%s/%s/%s", msgdir, argv[2], NEWSRC);
  76.         if ((fp = fopen(tmp, "r")) != (FILE *) 0) {
  77.             char tmp2[512];
  78.  
  79.             while (fgets(tmp, sizeof tmp, fp) != (char *) 0) {
  80.                 sscanf(tmp, "%[^:]: %ld", tmp2, &cnt);
  81.                 if (strcmp(tmp2, ng) == 0)
  82.                     break;
  83.             }
  84.             if (strcmp(tmp2, ng) == 0)
  85.                 minact = cnt + 1;
  86.             fclose(fp);
  87.         }
  88.         printf("News articles %d to %d\n", minact, limit);
  89.         for (cnt = 0, msg = minact; msg <= limit; msg++) {
  90.             if ((status = copynews(msgdir, conf, msg, argv[2])) > 0)
  91.                 exit(status);
  92.             else if (status == 0)
  93.                 cnt++;
  94.         }
  95.         printf("%ld news articles posted from %s to %s.\n", cnt, conf, argv[2]);
  96.         exit(cnt == 0? 9: 0);
  97.     }
  98.     msg = atol(++cp);
  99.     if (Index(argv[2], '/') != (char *) 0) {
  100.         fprintf(stderr, "Usage: uanews newsgroup[/article] conf\n");
  101.         exit(1);
  102.     }
  103.     if ((status = copynews(msgdir, conf, msg, argv[2])) == -1)
  104.         fprintf(stderr, "Couldn't read article %ld of %s\n", msg, conf);
  105.     exit(status);
  106. }
  107.  
  108. copynews(base, ng, art, dest)
  109. char *base, *ng, *dest;
  110. long art; {
  111.     char path[512], temp[512], ngpath[512];
  112.     long newmsg;
  113.     FILE *ifp, *ofp;
  114.     struct stat sbuf;
  115.     char *cp, *dp;
  116.     
  117.     for (cp = ng, dp = ngpath; *cp != '\0'; cp++, dp++)
  118.         if (*cp == '.')
  119.             *dp = '/';
  120.         else
  121.             *dp = *cp;
  122.     sprintf(path, "%s/%s/%ld", NEWSDIR, ngpath, art);
  123.     if (stat(path, &sbuf) < 0)
  124.         return -1;    /* likely to be common... */
  125.     sprintf(path, "%s/%s/himsg", base, dest);
  126.     if ((ifp = fopen(path, "r")) == (FILE *) 0) {
  127.         fprintf(stderr, "Conference %s: missing himsg...\n", dest);
  128.         return 2;
  129.     }
  130.     fgets(temp, sizeof temp, ifp);
  131.     fclose(ifp);
  132.     if ((newmsg = atol(temp)) < 0) {
  133.         fprintf(stderr, "Conference %s: invalid himsg...\n", dest);
  134.         return 2;
  135.     }
  136.     newmsg++;
  137.     sprintf(path, "%s/%s/%ld", NEWSDIR, ngpath, art);
  138.     if ((ifp = fopen(path, "r")) == (FILE *) 0) {
  139.         fprintf(stderr, "Are you certain that you have permission to access news?\n");
  140.         return 3;
  141.     }
  142.     sprintf(path, "%s/%s/%ld", base, dest, newmsg);
  143.     if (stat(path, &sbuf) == 0) {
  144.         fprintf(stderr, "Conference %s: corrupted (himsg incorrect)\n", dest);
  145.         fclose(ifp);
  146.         return 4;
  147.     }
  148.     if ((ofp = fopen(path, "w")) == (FILE *) 0) {
  149.         fprintf(stderr, "Conference %s: check permissions (can't create message)\n", dest);
  150.         fclose(ifp);
  151.         return 5;
  152.     }
  153.     while (fgets(temp, sizeof temp, ifp) != (char *) 0) {
  154.         fputs(temp, ofp);
  155.         if (ferror(ofp)) {
  156.             fprintf(stderr, "Write error on %s/%ld\n", dest, newmsg);
  157.             exit(5);    /* fatal! */
  158.         }
  159.     }
  160.     if (ferror(ifp)) {
  161.         fprintf(stderr, "Read error on %s/%ld\n", ng, art);
  162.         fclose(ifp);
  163.         fclose(ofp);
  164.         return 6;
  165.     }
  166.     fclose(ifp);
  167.     fclose(ofp);
  168.     sprintf(path, "%s/%s/himsg", base, dest);
  169.     if ((ifp = fopen(path, "w")) == (FILE *) 0) {
  170.         fprintf(stderr, "Conference %s: check permissions on himsg...\n", dest);
  171.         return 2;
  172.     }
  173.     fprintf(ifp, "%ld\n", newmsg);
  174.     fclose(ifp);
  175.     sprintf(path, "/tmp/uan%05d", getpid());
  176.     if ((ofp = fopen(path, "w")) == (FILE *) 0) {
  177.         fprintf(stderr, "Who moved /tmp?!\n");
  178.         return 16;
  179.     }
  180.     sprintf(path, "%s/%s/%s", base, dest, NEWSRC);
  181.     dp = "-";
  182.     if ((ifp = fopen(path, "r")) != (FILE *) 0) {
  183.         while (fgets(temp, sizeof temp, ifp) != (char *) 0) {
  184.             for (cp = temp; *cp != '\n' && *cp != ':'; cp++)
  185.                 ;
  186.             if (*cp == '\n')
  187.                 continue;    /* silent cleanup */
  188.             else
  189.                 *cp = '\0';
  190.             if (strcmp(temp, ng) == 0) {
  191.                 sprintf(temp, "%s: %ld\n", ng, art);
  192.                 dp = "#";
  193.             }
  194.             else
  195.                 *cp = ':';
  196.             fputs(temp, ofp);
  197.         }
  198.         fclose(ifp);
  199.     }
  200.     if (*dp != '#')
  201.         fprintf(ofp, "%s: %ld\n", ng, art);
  202.     fclose(ofp);
  203.     sprintf(path, "/tmp/uan%05d", getpid());
  204.     if ((ifp = fopen(path, "r")) == (FILE *) 0) {
  205.         fprintf(stderr, "Can't reopen temp file, aborting...\n");
  206.         return 16;
  207.     }
  208.     sprintf(path, "%s/%s/%s", base, dest, NEWSRC);
  209.     if ((ofp = fopen(path, "w")) == (FILE *) 0) {
  210.         fprintf(stderr, "Conference %s: can't update news link file (E:%d)\n", dest, errno);
  211.         return 17;
  212.     }
  213.     while (fgets(temp, sizeof temp, ifp) != (char *) 0)
  214.         fputs(temp, ofp);
  215.     fclose(ofp);
  216.     fclose(ifp);
  217.     sprintf(path, "/tmp/uan%05d", getpid());
  218.     unlink(path);    /* not a tragedy if this fails... */
  219.     return 0;
  220. }
  221.