home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 1 / GoldFishApril1994_CD2.img / d4xx / d473 / cnewssrc / cnews_src.lzh / relay / amiga / checkgroups.c < prev    next >
C/C++ Source or Header  |  1991-01-04  |  4KB  |  185 lines

  1. /*    :ts=4
  2.  *    checkgroups - this is a C implementation for the Amiga, in an attempt to
  3.  *    reduce the dependency on shell scripts (they aren't capable enough).
  4.  *
  5.  *    Expects no command line arguments, but control article on stdin
  6.  *
  7.  *    $Id$
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <time.h>
  13.  
  14. #ifdef __STDC__
  15. # include <stdarg.h>
  16. #else
  17. # include <varargs.h>
  18. #endif /* __STDC__ */
  19.  
  20. #include "libc.h"
  21. #include "news.h"
  22. #include "config.h"
  23. #include "system.h"
  24.  
  25. #define ADDGROUP    envparm("ADDGROUP")
  26. #define DELGROUP    envparm("DELGROUP")
  27.  
  28. char *progname;
  29. char **grps;
  30. int count;
  31. struct system *me;
  32.  
  33. int gngp(register char *pattern, register char *text)
  34. {
  35.     int returned;
  36.     char savewhite;
  37.     char *whitesp;
  38.     extern char *strpbrk();
  39.  
  40.     whitesp = strpbrk(text, " \t\n");
  41.     if (whitesp) {
  42.         savewhite = *whitesp;
  43.         *whitesp = '\0';
  44.     }
  45.     returned = ngmatch(pattern, text);
  46.     if (whitesp)
  47.         *whitesp = savewhite;
  48.     return( returned );
  49. }
  50.  
  51. int str_cmp(register void *a, register void *b)
  52. {
  53.     return( strcmp( *(char **)a, *(char **)b ) );
  54. }
  55.  
  56. void mail(char *who, char *msg)
  57. {
  58.     FILE *fp;
  59.  
  60.     if (fp = fopen(ctlfile("errlog"), "a+")) {
  61.         fprintf(fp, "\nTo: %s\nSubject: %s error\n\n%s\n", who, progname, msg);
  62.         fclose(fp);
  63.     }
  64. }
  65.  
  66. void add_grp(register char *buf)
  67. {
  68.     register char *ptr;
  69.  
  70.     /*
  71.      *    This strips out JUNK, CONTROL, and GENERAL since they
  72.      *    don't have any periods in them.
  73.      */
  74. #define INCR    128
  75.  
  76.     ptr = strtok(buf, " \t\n");
  77.     if (*ptr != '#' && strchr(ptr, '.')) {
  78.         if (gngp(me->sy_ngs, ptr)) {
  79.             if ((count % INCR) == 0)
  80.                 grps = (char **) realloc(grps, (count+INCR) * sizeof(*grps));
  81.             grps[count++] = strsave(ptr);
  82.         }
  83.     }
  84. }
  85.  
  86. void writediffs(char **ngrp, int ncnt, char **ogrp, int ocnt)
  87. {
  88.     register int i = 0, j = 0, cmp;
  89.     FILE *new = fopen(ctlfile("checkgroups.new"), "a+");
  90.     FILE *del = fopen(ctlfile("checkgroups.del"), "a+");
  91.  
  92.     if (new && del) {
  93.         time_t now;
  94.  
  95.         now = time(NULL);
  96.         fprintf(new, "\n%s", ctime(&now));
  97.         fprintf(del, "\n%s", ctime(&now));
  98.         while (i < ncnt && j < ocnt) {
  99.             if (cmp = strcmp(ngrp[i], ogrp[j])) {
  100.                 if (cmp < 0)
  101.                     fprintf(new, "%s %s\n", ADDGROUP, ngrp[i++]);
  102.                 else
  103.                     fprintf(del, "%s %s\n", DELGROUP, ogrp[j++]);
  104.             } else
  105.                 i++, j++;
  106.         }
  107.         while (i < ncnt)
  108.             fprintf(new, "%s <NIL: %s\n", ADDGROUP, ngrp[i++]);
  109.         while (j < ocnt)
  110.             fprintf(del, "%s %s\n", DELGROUP, ogrp[j++]);
  111.     }
  112.     fclose(new);
  113.     fclose(del);
  114. }
  115.  
  116. void readctl(char *name)
  117. {
  118.     FILE *fp;
  119.     char buffer[BUFSIZ];
  120.  
  121.     if (name)
  122.         fp = fopen(ctlfile(name), "r");
  123.     else {
  124.         fp = stdin;
  125.         while (fgets(buffer, sizeof(buffer), fp))
  126.             if (*buffer == '\n') break;
  127.     }
  128.     if (fp) {
  129.         while (fgets(buffer, sizeof(buffer), fp))
  130.             add_grp(skipsp(buffer));
  131.         fclose(fp);
  132.     }
  133. }
  134.  
  135. int main(int argc, char **argv)
  136. {
  137.     char **newgrp;
  138.     int newcount;
  139.  
  140.     if ((progname=strchr(*argv,'/')) || (progname=strchr(*argv,':')))
  141.         progname++;
  142.     else
  143.         progname = *argv;
  144.  
  145.     if ((me = oursys()) == NULL) {
  146.         mail(newsmaster(), "Couldn't find \"ME\" in sys file!");
  147.         exit( 10 );
  148.     }
  149.     /*
  150.      *    Algorithm goes like this:
  151.      *        1)    obtain newsgroup subscription list
  152.      *        2)    read "localgroups" and call add_grp()
  153.      *        3)    read "newgroups" and call add_grp()
  154.      *        4)    read stdin (checkgroups article body) and call add_grp()
  155.      *        5)    add_grp() will strip out comments, lines without periods in
  156.      *            field 1, and lines which don't match subscription list
  157.      *        6)=    sort the results of (2) through (4)
  158.      *        7)=    strip field 1 from ACTIVE file via add_grp()
  159.      *
  160.      *    Compare (step7) to (step6)
  161.      *    If in (step6) and !(step7), add newsgroup
  162.      *    If in (step7) and !(step6), remove newsgroup
  163.      */
  164.  
  165.     count = 0;
  166.     grps = NULL;
  167.     readctl("newgroups");
  168.     readctl("localgroups");
  169.     readctl(NULL);                /* stdin */
  170.     newcount = count;
  171.     newgrp = grps;
  172.     if (newcount > 1)
  173.         qsort(newgrp, newcount, sizeof(*newgrp), str_cmp);
  174.  
  175.     /*    Now read the ACTIVE file.    */
  176.     count = 0;
  177.     grps = NULL;
  178.     readctl("active");
  179.     if (count > 1)
  180.         qsort(grps, count, sizeof(*grps), str_cmp);
  181.  
  182.     writediffs(newgrp, newcount, grps, count);
  183.     return( 0 );
  184. }
  185.