home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1995 April / Internet Tools.iso / mail / listserv / utils / clean.c.Z / clean.c
Encoding:
C/C++ Source or Header  |  1991-10-27  |  3.2 KB  |  129 lines

  1. /*############################################################################
  2.   clean: Remove extraneous characters from text files.
  3.   Written by Anastasios Kotsikonas.
  4.   Usage: clean {file}+
  5. */
  6.  
  7. # define MAX_BUF_LINES   100
  8. # define MAX_LINE_LENGTH 133
  9.  
  10. # include <stdio.h>
  11. # include <string.h>
  12.  
  13. main (argc, argv)
  14. int argc;
  15. char **argv;
  16. {
  17.   FILE *fin, *fout;
  18.   signed char command [80], line [MAX_LINE_LENGTH];
  19.   char *lines [MAX_LINE_LENGTH];
  20.   int i, ch, buf;
  21.   long int obytes, newbytes;
  22.  
  23.   if (argc == 1)
  24.     fprintf (stderr, "Usage: clean {file}+\nRemove extraneous characters from text files.\n"),
  25.     exit (1);
  26.   --argc;
  27.   while (argc) {
  28.     if ((fin = fopen (argv [argc], "r")) == NULL) {
  29.       printf ("Unable to open input file '%s'\n", argv [argc]);
  30.       fflush (stdout);
  31.       --argc;
  32.       continue;
  33.     }
  34.     if ((fout = fopen ("/tmp/clean.out", "w")) == NULL) {
  35.       printf ("Unable to write to /tmp. File %s not cleaned.\n",
  36.           argv [argc]);
  37.       fflush (stdout);
  38.       fclose (fin);
  39.       --argc;
  40.       continue;
  41.     }
  42.     obytes = newbytes = buf = 0;
  43.     printf ("Cleaning '%s': ", argv [argc]);
  44.     fflush (stdout);
  45.     while (!feof (fin)) {
  46.       line [0] = '\0';
  47.       fgets (line, MAX_LINE_LENGTH - 1, fin);
  48.       if (strlen (line) > 0) {
  49.     obytes += strlen (line);
  50.     i = 0;
  51.     while ((ch = line [i]) != '\n' && ch != '\0') {
  52.       if (ch == '\n' || ch == '\r' || ch == '\t' || (ch >= ' ' && ch < 127))
  53.         ++i;
  54.       else if (ch >= 0 && ch < 256)
  55.         sprintf (line + i, "%s", line + i + 1);
  56.       else {
  57.         printf ("not a text file; file not cleaned.\n");
  58.         fflush (stdout);
  59.         goto abort;
  60.       }
  61.     }
  62.     newbytes += strlen (line);
  63.     lines[buf] = (char *) malloc ((strlen (line) + 1) * sizeof (char));
  64.     strcpy (lines[buf], line);
  65.     if (++buf > MAX_BUF_LINES - 1) {
  66.       for (i = 0; i < buf; ++i) {
  67.         if (fprintf (fout, "%s", lines[i]) < 0) {
  68.           printf (" unable to write to /tmp anymore; file not cleaned.\n");
  69.           fflush (stdout);
  70. # ifdef unix
  71.           unlink ("/tmp/clean.out");
  72. # else
  73.               command[0] = '\0';
  74.               sprintf (command, "del \\tmp\\clean.out");
  75.               system (command);
  76. # endif
  77.           goto abort;
  78.         }
  79.         free (lines[i]);
  80.         lines[i] = NULL;
  81.       }
  82.       buf = 0;
  83.     }
  84.       }
  85.     }
  86.     for (i = 0; i < buf; ++i) {
  87.       if (fprintf (fout, "%s", lines[i]) < 0) {
  88.     printf (" unable to write to /tmp anymore; file not cleaned.\n");
  89.     fflush (stdout);
  90. # ifdef unix
  91.     unlink ("/tmp/clean.out");
  92. # else
  93.         command[0] = '\0';
  94.         sprintf (command, "del \\tmp\\clean.out");
  95.         system (command);
  96. # endif
  97.     goto abort;
  98.       }
  99.       free (lines[i]);
  100.       lines[i] = NULL;
  101.     }
  102.     buf = 0;
  103.     printf ("%ld bytes before --> %ld bytes after", obytes, newbytes);
  104.     if (obytes == newbytes)
  105.       printf (": NO CHANGES");
  106.     printf ("\n");
  107.     fflush (stdout);
  108.     fflush (fout);
  109.     if (obytes != newbytes)
  110.       command[0] = '\0',
  111. # ifdef unix
  112.       sprintf (command, "mv /tmp/clean.out ./%s", argv [argc]),
  113.       system (command);
  114. # else
  115.       sprintf (command, "copy \\tmp\\clean.out .\\%s", argv [argc]),
  116.       system (command);
  117.     command[0] = '\0';
  118.     sprintf (command, "del \\tmp\\clean.out");
  119.     system (command);
  120. # endif
  121.   abort:
  122.     for (i = 0; i < buf; ++i)
  123.       free (lines[i]);
  124.     fclose (fin);
  125.     fclose (fout);
  126.     --argc;
  127.   }
  128. }
  129.