home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume6 / dissect2 / dissect.c < prev    next >
C/C++ Source or Header  |  1989-03-04  |  3KB  |  103 lines

  1. /****************************************************************************\
  2. |*                                                                          *|
  3. |*  Dissect.c: a rough-and-ready heap of junk to split a file in mbox       *|
  4. |*             format into a number of mbox-format files, each containing   *|
  5. |*             all the messages from a sender whose mail was in the         *|
  6. |*             original mbox, and named after that sender.                  *|
  7. |*                                                                          *|
  8. |*  Also:      it will count the number of articles in each mbox in its     *|
  9. |*             argument list, when called with argv[0] not equal to         *|
  10. |*             dissect.                                                     *|
  11. |*                                                                          *|
  12. |*  This program written in the early hours of 21st January 1989.           *|
  13. |*  Copyright (C) 1989 by Mike Taylor.  No rights reserved - copy me!       *|
  14. |*                                                                          *|
  15. \****************************************************************************/
  16.  
  17. #include <stdio.h>
  18. #include <strings.h>
  19.  
  20. #define LINELEN 1024
  21.  
  22. extern char *fgets ();
  23. static int onlycount = 0;
  24.  
  25. /*--------------------------------------------------------------------------*/
  26.  
  27. int handle (argv, index)
  28.   char **argv;
  29.   int index;
  30. {
  31.   FILE *fp;
  32.   FILE *to = NULL;
  33.   static char name[LINELEN];
  34.   static char line[LINELEN];
  35.   static char last[LINELEN] = "\n";
  36.   char *cp;
  37.   int flag = 0;
  38.  
  39.   if ((fp = fopen (argv[index], "r")) == NULL) {
  40.     (void) fprintf (stderr, "%s: couldn't open input file %s.\n",
  41.             argv[0], argv[index]);
  42.     return (1);
  43.   }
  44.  
  45.   while (fgets (line, LINELEN, fp) != NULL) {
  46.     if ((!strncmp (line, "From ", 5)) && (*last == '\n')) {
  47.       flag++;
  48.       if (!onlycount) {
  49.     (void) fclose (to);
  50.     (void) strcpy (name, line+5);
  51.     for (cp = name; (*cp != ' ') && (*cp != '@') && (*cp != '%'); cp++);
  52.     *cp = '\0';
  53.     if (!strcmp (name, argv[index])) {
  54.       (void) fprintf (stderr, "%s: won't overwrite input file %s.\n",
  55.               argv[0], argv[index]);
  56.       continue;
  57.     }
  58.     if ((to = fopen (name, "a")) == NULL) {
  59.       (void) fprintf (stderr, "%s: couldn't open output file %s.\n",
  60.               argv[0], name);
  61.       return (1);
  62.     }
  63.       }
  64.     }
  65.     if ((to != NULL) && (!onlycount))
  66.       (void) fputs (line, to);
  67.     (void) strcpy (last, line);
  68.   }
  69.   if (flag == 0)
  70.     (void) fprintf (stderr, "%s: found no mail in input file %s.\n",
  71.             argv[0], argv[index]);
  72.   else
  73.     if (onlycount)
  74.       (void) printf ("%s: %3d items of mail in input file %s.\n",
  75.              argv[0], flag, argv[index]);
  76.   return (flag == 0);
  77. }
  78.  
  79. /*--------------------------------------------------------------------------*/
  80.  
  81. main (argc, argv)
  82.   int argc;
  83.   char **argv;
  84. {
  85.   int status = 0;
  86.   int i;
  87.  
  88.   if (argc == 1) {
  89.     (void) fprintf (stderr, "Usage: %s file [ file ... ]\n", argv[0]);
  90.     exit (255);
  91.   }
  92.  
  93.   if (strcmp (argv[0], "dissect"))
  94.     onlycount = 1;
  95.  
  96.   for (i = 1; i < argc; i++)
  97.     status += handle (argv, i);
  98.  
  99.   exit (status);
  100. }
  101.  
  102. /*--------------------------------------------------------------------------*/
  103.