home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1678 < prev    next >
Encoding:
Internet Message Format  |  1990-12-28  |  3.0 KB

  1. From: jerry@olivey.olivetti.com (Jerry Aguirre)
  2. Newsgroups: news.software.b,alt.sources
  3. Subject: Tool to find duplicate articles
  4. Message-ID: <49290@olivea.atc.olivetti.com>
  5. Date: 16 Aug 90 19:06:16 GMT
  6.  
  7. Here is a tool I thru together when my news history got corrupted and
  8. users started complaining about seeing duplicates of articles.
  9.  
  10. ===BEGIN histdups.c===
  11. #include <stdio.h>
  12. #define LINESIZ 1024
  13. #define MAXF 32
  14.  
  15. /* Expects the stdin to be the history file, sorted.  Stdout is a list
  16.  * of file names which are duplicates of earlier articles.  Run after
  17.  * expire -r and then "rm" the files listed in the output.
  18.  *
  19.  *    sort <history | histdups >dupfiles; xargs <dupfiles rm
  20.  *
  21.  * If the news history becomes corrupted then you can wind up with
  22.  * duplicates.  These are both a waste of space and a pain for people
  23.  * reading news.
  24.  *
  25.  * B news expire -r will find the dups and then enter all of them into
  26.  * the history file.  (It doesn't even match up the cross postings
  27.  * to each other correctly.)  This program will output the names of all
  28.  * but the first duplicate in each news group.  (Where "first" is based
  29.  * on article numbering which presumably represents arrival order.)
  30.  *
  31.  * 16Aug90 Jerry Aguirre <jerry@atc.olivetti.com>
  32.  */
  33.  
  34. char files[MAXF][LINESIZ];
  35. int nf;
  36.  
  37. long atol();
  38. char *index();
  39.  
  40. main()
  41. {
  42.     char c, *p;
  43.     int i, j;
  44.     char line[LINESIZ];
  45.     char id[LINESIZ];
  46.     char lastline[LINESIZ];
  47.  
  48.     nf = 0;
  49.     id[0] = '\0';
  50.     lastline[0] = '\0';
  51.     while (gets(line)) {
  52.     p = index(line, '\t');
  53.     if (p) {
  54.         *p = '\0';
  55.         if (strcmp(line, id) == 0) {    /* we have a dup */
  56.         if (lastline[0] != '\0') {
  57.             parsefiles(lastline);
  58.             lastline[0] = '\0';
  59.         }
  60.         *p = '\t';
  61.         parsefiles(line);
  62.         } else {
  63.         printdups();
  64.         strcpy(id, line);
  65.         *p = '\t';
  66.         strcpy(lastline, line);
  67.         nf = 0;
  68.         }
  69.     }
  70.     }
  71. }
  72. parsefiles(line) char *line;
  73. {
  74.     char *pd, *pf, *p;
  75.  
  76.     pd = index(line, '\t');
  77.     if (pd) pd++;
  78.     else return;
  79.     pf = index(pd, '\t');
  80.     if (pf) pf++;
  81.     else return;
  82.     while (*pf) {
  83.     while (*pf == ' ') pf++;
  84.     if (*pf == '\0') return;
  85.     if (nf >= MAXF) return;
  86.     p = index(pf, ' ');
  87.     if (p) *p = '\0';
  88.     strcpy(files[nf], pf);
  89.     nf++;
  90.     if (p) {
  91.         pf = p + 1;
  92.         *p = ' ';
  93.     }
  94.     else return;
  95.     }
  96. }
  97.  
  98. printdups()
  99. {
  100.     int i1, i2, flags[MAXF];
  101.     long n1, n2;
  102.     char *p1, *p2;
  103.  
  104.     for (i1 = 0; i1 < nf; i1++) flags[i1] = 0;
  105.  
  106.     for (i1 = 0; i1 < nf; i1++) {
  107.     p1 = index(files[i1], '/');
  108.     if (!p1) continue;
  109.     *p1 = '\0';
  110.     n1 = atol(p1+1);
  111.     for (i2 = i1 + 1; i2 < nf; i2++) {
  112.         p2 = index(files[i2], '/');
  113.         if (!p2) continue;
  114.         *p2 = '\0';
  115.         if (strcmp(files[i1], files[i2]) == 0) { /* same group */
  116.         n2 = atol(p2+1);
  117.         if (n2 > n1) flags[i2] = 1;    /* lowest number stays */
  118.         else if (n2 < n1) flags[i1] = 1;
  119.         }
  120.         *p2 = '/';
  121.         n2 = atol(p2+1);
  122.     }
  123.     *p1 = '/';
  124.     }
  125.     for (i1 = 0; i1 < nf; i1++) {
  126.     if (flags[i1] == 1) {
  127.         for (p1 = files[i1]; *p1; p1++) {
  128.         if (*p1 == '.') putchar('/');
  129.         else putchar(*p1);
  130.         }
  131.         putchar('\n');
  132.     }
  133.     }
  134. }
  135. ===END histdups.c===
  136.