home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NNTP-1.000 / NNTP-1 / nntp.1.5.11t / support / mkgrdates.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-17  |  3.7 KB  |  179 lines

  1. #ifndef lint
  2. static char    *sccsid = "@(#)mkgrdates.c    1.5    (Berkeley) 5/30/89";
  3. #endif
  4.  
  5. /*
  6.  * Make a list of newsgroups which are "new" in the file NGDATE_FILE.
  7.  * "New" means having an entry in the active file, and having
  8.  * a message numbered "1" in the appropriate news directory.
  9.  * Since this involves a stat of all the newsgroups, we try
  10.  * to be intelligent about things -- if the active file's size
  11.  * since we last ran -- stored in STAT_FILE -- hasn't changed
  12.  * since last time, we assume things are ok, and exit without
  13.  * doing anything.  This could fail in extreme circumstances,
  14.  * but is "too painful to do right".
  15.  *
  16.  * Output in NGDATE_FILE is of the form
  17.  *
  18.  *    date newsgroup
  19.  *
  20.  * where "date" is the date the newsgroup was created, expressed as
  21.  * the number of seconds since 000000 Jan 1, 1970, GMT.  This file
  22.  * winds up sorted in cronological order.
  23.  *
  24.  *    Phil Lapsley
  25.  *    College of Engineering
  26.  *    University of California, Berkeley
  27.  *    (ARPA: phil@Berkeley.ARPA; UUCP: ...!ucbvax!phil)
  28.  */
  29.  
  30. #include <stdio.h>
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include "../common/conf.h"
  34. #ifdef USG
  35. #include <time.h>
  36. #else not USG
  37. #include <sys/time.h>
  38. #endif not USG
  39.  
  40. #define MODE        0644    /* Better be readable by nntpd! */
  41.  
  42. #ifndef MAXPATHLEN
  43. #define    MAXPATHLEN    1024
  44. #endif
  45.  
  46. #ifndef MAXGROUPLEN
  47. #define    MAXGROUPLEN    256
  48. #endif
  49.  
  50. extern    int    linecmp();
  51. extern    char    *index(), *malloc(), *strcpy(), *strcat();
  52.  
  53. main(argc, argv)
  54. int    argc;
  55. char    *argv[];
  56. {
  57.     char    *groups[1024];        /* XXX 1024 groups max */
  58.     char    line[MAXPATHLEN], gr_name[MAXGROUPLEN];
  59.     char    *cp;
  60.     int    i, j;
  61.     long    lastsize, crntsize;
  62.     long    birthtime;
  63.     struct tm *tmptr;
  64.     FILE    *stat_fp, *active_fp, *date_fp;
  65.     long    birthof();
  66.  
  67.     stat_fp = fopen(STAT_FILE, "r");
  68.  
  69.     if (stat_fp != NULL) {
  70.         (void) fscanf(stat_fp, "%d", &lastsize);
  71.         (void) fclose(stat_fp);
  72.     }
  73.  
  74.     active_fp = fopen(ACTIVE_FILE, "r");
  75.     if (active_fp == NULL) {
  76.         fprintf(stderr, "Can't read active file?\n");
  77.         perror(ACTIVE_FILE);
  78.         exit(1);
  79.     }
  80.  
  81.     /* Check length; if it's the same as last time, quit */
  82.  
  83.     (void) fseek(active_fp, (long) 0, 2);
  84.     crntsize = ftell(active_fp);
  85.     if (crntsize == lastsize) {
  86.         (void) fclose(active_fp);
  87.         exit(0);
  88.     }
  89.  
  90.     /* Ok, time to rebuild the date file */
  91.  
  92.     date_fp = fopen(NGDATE_FILE, "w");
  93.  
  94.     if (date_fp == NULL) {
  95.         perror(NGDATE_FILE);
  96.         (void) fclose(active_fp);
  97.         exit(1);
  98.     }
  99.  
  100.     rewind(active_fp);
  101.  
  102.     i = 0;
  103.     while (fgets(line, sizeof(line), active_fp) != NULL) {
  104.         if ((cp = index(line, ' ')) != NULL)
  105.             *cp = '\0';
  106.         (void) strcpy(gr_name, line);
  107.         birthtime = birthof(line, atoi(cp + 1));
  108.         
  109.         if (birthtime == 0)    /* Skip ancient newsgroups */
  110.             continue;
  111.  
  112.         (void) sprintf(line, "%ld %s", birthtime, gr_name);
  113.         groups[i] = malloc(strlen(line)+1);
  114.         if (groups[i] != NULL)
  115.             (void) strcpy(groups[i++], line);
  116.         else {
  117.             perror("malloc");
  118.             exit(1);
  119.         }
  120.     }
  121.  
  122.     (void) fclose(active_fp);
  123.  
  124.     qsort((char *) groups, i, sizeof(char *), linecmp);
  125.  
  126.     for (j = 0; j < i; ++j)
  127.         fprintf(date_fp, "%s\n", groups[j]);
  128.  
  129.     (void) fclose(date_fp);
  130.  
  131.     (void) chmod(NGDATE_FILE, MODE);
  132.  
  133.     stat_fp = fopen(STAT_FILE, "w");
  134.     if (stat_fp == NULL) {
  135.         perror(STAT_FILE);
  136.         exit(1);
  137.     }
  138.  
  139.     fprintf(stat_fp, "%d\n", crntsize);
  140.  
  141.     (void) fclose(stat_fp);
  142.  
  143.     (void) chmod(STAT_FILE, MODE);
  144.  
  145.     exit(0);
  146. }
  147.  
  148. linecmp(line1, line2)
  149. char     **line1, **line2;
  150. {
  151.     return(0 - strcmp(*line1, *line2));
  152. }
  153.  
  154.  
  155. /* return creation time of newsgroup */
  156.  
  157. long
  158. birthof(group, highart)
  159. char    *group;
  160. int    highart;
  161. {
  162.     char    *cp, *index();
  163.     char    tst[128];
  164.     struct stat statbuf;
  165.  
  166.     while ((cp = index(group, '.')))
  167.         *cp = '/';
  168.  
  169.     (void) strcpy(tst, SPOOLDIR);
  170.     (void) strcat(tst, "/");
  171.     (void) strcat(tst, group);
  172.     if (highart)
  173.         (void) strcat(tst, "/1");
  174.     if (stat(tst, &statbuf) < 0)
  175.         return 0L;        /* not there, assume ancient */
  176.     else
  177.         return(statbuf.st_mtime);
  178. }
  179.