home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / text_cla / tail.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-23  |  1.9 KB  |  77 lines

  1. /*  Tail.C     John Tal      Based on Unix tail command but not fully implemented here */
  2.  
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <malloc.h>
  7.  
  8. struct tails{
  9.        char *inptr; /*  data read from file  */
  10.        int  next;   /*  which tail[] member is next, numeric array index */
  11.         };
  12.  
  13.  
  14. main(argc,argv)
  15. int argc;
  16. char **argv;
  17. {
  18.      int   lines,i,n,head,oldi,pass;
  19.      FILE  *fp;
  20.      struct tails tail[25];
  21.      char  buffer[255];
  22.  
  23.  
  24.      printf("tail, pc-dos version by john tal, 09/1988\n");
  25.      if(argc == 2)
  26.           lines = 20;
  27.      else{
  28.           printf("usage:  tail filename");
  29.           return(1);
  30.      }
  31.  
  32.  
  33.      for(i = 0; i < lines; i++)
  34.         tail[i].inptr = malloc(255);
  35.  
  36.      i = 0;
  37.      head = 0;   /*  initial head of circular buffer */
  38.      n = 255;    /*  max bytes to read into each buffer  */
  39.      pass = 1;   /*  passes through internal buffers */
  40.      if((fp = fopen(argv[1],"r")) != NULL){
  41.         while(fgets(buffer,n,fp) != NULL){
  42.            oldi = i;   /*  remember */
  43.            i++;        /*  next slot */
  44.  
  45.            if(i == lines){
  46.               i = 0;      /*  reset to beginning of buffers */
  47.               pass++;     /*  another pass */
  48.            }
  49.            if(pass != 1)
  50.               head = tail[i].next;   /* new head */
  51.  
  52.            tail[oldi].next = i;   /*  connect last to this one */
  53.            tail[i].next = -1;     /*  this one is new tail */
  54.            strcpy(tail[i].inptr,buffer);  /*  get data */
  55.         }
  56.         /* head = i; */  /* final head */
  57.         fclose(fp);
  58.         i = head;
  59.         while(i != -1){
  60.        printf("%s",tail[i].inptr);
  61.        i = tail[i].next;
  62.         }
  63.  
  64.         /*  free-up malloc'd stuff */
  65.         for(i = 0; i < lines; i++)
  66.            free(tail[i].inptr);
  67.  
  68.      }
  69.      else{
  70.          printf("error: could not open file %s\n",argv[1]);
  71.      }
  72.  
  73.  
  74. }
  75.  
  76.  
  77.