home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / duucp-1.17 / AU-117b4-src.lha / src / util / trimfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-24  |  2.5 KB  |  158 lines

  1. /*
  2.  *  TRIMFILE file1 file2 .. filen -lines
  3.  *
  4.  *  (C) Copyright 1989-1990 by Matthew Dillon,    All Rights Reserved.
  5.  *
  6.  *  Trims the specified files to the specified number of lines.  Each
  7.  *  file is read and the last N lines written back.
  8.  *
  9.  *  Normally used to trim log files based on a crontab entry.  If no
  10.  *  -lines argument is given the file is trimmed to 100 lines.
  11.  *
  12.  *  Each line may be up to 255 characters in length.
  13.  */
  14.  
  15. #include <ctype.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include "version.h"
  19. #include "config.h"
  20. #include <owndevunit.h>
  21.  
  22. IDENT (".02");
  23.  
  24. #define LINSIZE 256
  25.  
  26. char
  27.     **LineBuf = NULL;
  28. long
  29.     Lines = 100;
  30. struct Library
  31.     *OwnDevUnitBase = NULL;
  32. static const char
  33.     LockLib  [] = { ODU_NAME },
  34.     LockName [] = { "LOG-UPDATE" };
  35.  
  36. Local void myexit (void);
  37. Local void MemErr (void);
  38. Local void TrimFile (const char *);
  39.  
  40. int
  41. main (int ac, char **av)
  42. {
  43.     int
  44.         lineset = 0,
  45.         i;
  46.  
  47.     atexit (myexit);
  48.     if ((OwnDevUnitBase = OpenLibrary ((UBYTE *) LockLib, 0)) == NULL) {
  49.         fprintf (stderr, "Couldn't open %s\n", LockLib);
  50.         exit (30);
  51.     }
  52.  
  53.     for (i = 1; i < ac; ++i) {
  54.         if (av [i] [0] == '-') {
  55.             if (lineset) {
  56.                 fprintf (stderr, "Can't set linesize more than once\n");
  57.                 exit (30);
  58.             }
  59.             if (!isdigit (av [i] [1])) {
  60.                 fprintf (stderr, "Linesize must be an integer\n");
  61.                 exit (30);
  62.             }
  63.             lineset = 1;
  64.             Lines = atol (av [i] + 1);
  65.         }
  66.     }
  67.  
  68.     /*
  69.      *  Allocating one more than necessary handles the Lines == 0 case
  70.      *  as well as supplying a scratch buffer for the last fgets that
  71.      *  fails.
  72.      */
  73.  
  74.     LineBuf = malloc (sizeof (char *) * (Lines + 1));
  75.     if (LineBuf == NULL)
  76.         MemErr ();
  77.  
  78.     for (i = 0; i <= Lines; ++i) {
  79.         LineBuf [i] = malloc (LINSIZE);
  80.         if (LineBuf [i] == NULL)
  81.             MemErr ();
  82.     }
  83.  
  84.     for (i = 1; i < ac; ++i) {
  85.         char
  86.             *ptr = av [i];
  87.  
  88.         if (*ptr == '-')
  89.             continue;
  90.  
  91.         LockFile (LockName);
  92.         TrimFile (ptr);
  93.         UnLockFile (LockName);
  94.     }
  95.  
  96.     exit (0);
  97. }
  98.  
  99. void
  100. myexit (void)
  101. {
  102.     UnLockFiles ();
  103.  
  104.     if (OwnDevUnitBase) {
  105.         CloseLibrary (OwnDevUnitBase);
  106.         OwnDevUnitBase = NULL;
  107.     }
  108.  
  109.     return;
  110. }
  111.  
  112. void
  113. MemErr (void)
  114. {
  115.     fprintf (stderr, "Not enough memory!\n");
  116.     exit (30);
  117. }
  118.  
  119. void
  120. TrimFile (const char *name)
  121. {
  122.     FILE
  123.         *fi = fopen (name, "r");
  124.     long
  125.         rep,
  126.         i;
  127.  
  128.     if (fi == NULL)
  129.         return;
  130.  
  131.     i = 0;
  132.     rep = 0;
  133.     while (fgets (LineBuf [i], LINSIZE, fi)) {
  134.         if (++i > Lines) {
  135.             i = 0;
  136.             rep = 1;
  137.         }
  138.     }
  139.     fclose (fi);
  140.  
  141.     if (rep == 0)
  142.         return;
  143.  
  144.     if (fi = fopen (name, "w")) {
  145.         long
  146.             j;
  147.  
  148.         for (j = Lines; j; --j) {
  149.             if (++i > Lines)
  150.                 i = 0;
  151.             fputs (LineBuf [i], fi);
  152.         }
  153.         fclose (fi);
  154.     }
  155.  
  156.     return;
  157. }
  158.