/* Copyright 2014 by R. Harmsen. For a full explanation see https://rudhar.com/sfreview/truncexp/en.htm */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> enum shorten_mode_e {MV, CAT, WRI, RPLUS}; int emulate (char *mode); int main (int argc, char **argv) { FILE *fpi, *fpo; enum shorten_mode_e mode = CAT; if (argc > 1 && strcmp(argv[1], "-m") == 0) { /* Shorten by means of mv */ mode = MV; } else if (argc > 1 && strcmp(argv[1], "-c") == 0) { /* Shorten by means of cat (normal write mode) */ mode = CAT; } else if (argc > 1 && strcmp(argv[1], "-w") == 0) { /* Emulate cat, open in write mode */ mode = WRI; } else if (argc > 1 && strcmp(argv[1], "-p") == 0) { /* Write in r+ mode */ mode = RPLUS; } // system("ls -lti"); system("stat -f \"Born %SB, modified %Sm, inode %6i, %6Dz bytes: %N\" *"); /* Extract last few hundred lines of logfile, meanwhile created by loglines, which writes 600 and then some */ system("tail -400 logfile > f"); sleep(23); switch (mode) { case MV: system("mv f logfile"); break; case CAT: system("cat f > logfile"); break; case WRI: emulate("w"); break; case RPLUS: emulate("r+"); break; } // system("ls -lti"); fprintf(stdout, "\nAfter:\n"); system("stat -f \"Born %SB, modified %Sm, inode %6i, %6Dz bytes: %N\" *"); sleep(12); fprintf(stdout, "\nLater still:\n"); system("stat -f \"Born %SB, modified %Sm, inode %6i, %6Dz bytes: %N\" *"); return 0; } int emulate (char *mode) { /* Nothing just yet */ char linebuf[129]; FILE *fpi = NULL, *fpo = NULL; fpi = fopen("f", "r"); if (!fpi) { fprintf(stderr, "Error in progline %d\n", __LINE__); return 1; } fpo = fopen("logfile", mode); if (!fpo) { fprintf(stderr, "Error in progline %d\n", __LINE__); return 2; } while (fgets(linebuf, sizeof linebuf, fpi) != NULL) { fputs(linebuf, fpo); } if (fpi) fclose(fpi); if (fpo) fclose(fpo); return 0; } /* Copyright 2014 by R. Harmsen. For a full explanation see https://rudhar.com/sfreview/truncexp/en.htm */