home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / CPM / BDSC / BDSC-4 / TAIL.C < prev    next >
Text File  |  2000-06-30  |  2KB  |  104 lines

  1. /*
  2.     TAIL.C     --   Print last several lines of a text file.
  3.     Written by Leor Zolman, 3/83
  4.  
  5.     ****************************
  6.     *   Requires BDS C v1.5    *
  7.     ****************************
  8.  
  9.     Usage: tail [-n] <filename>
  10.  
  11.     where n, if specified, gives number of lines before EOF at
  12.     which to begin printing. Defaults to 22.
  13. */
  14.  
  15. #include <bdscio.h>
  16.  
  17. #define LINES_DEF 22    /* number of lines before EOF to display    */
  18. #define NBSECTS 20    /* number of sectors before EOF to buffer     */
  19. #define TXTBUFSIZE (BSECTS * SECSIZ + 1) /* size of working text buffer    */
  20.  
  21. main(argc,argv)
  22. char **argv;
  23. {
  24.     int i, fd, sects;
  25.     unsigned filsiz;
  26.     int lineno, lincnt;
  27.     char txtbuf[NBSECTS * SECSIZ + 1];    /* sector buffer */
  28.     char c, *txtptr, *lastsec, *filnam, *temp;
  29.  
  30.     filnam = NULL;
  31.     lineno = LINES_DEF;
  32.  
  33.     for (i = 1; i < argc; i++)
  34.     {
  35.         if (argv[i][0] == '-')
  36.         {
  37.             if (!(lineno = atoi(&argv[i][1])))
  38.                 goto abort;
  39.         }
  40.         else if (filnam)
  41.             goto abort;
  42.         else
  43.             filnam = argv[i];
  44.     }            
  45.  
  46.  
  47.     if (argc < 2 || !filnam || lineno < 0)
  48.     {
  49.    abort:    puts("Usage: tail [-#] <filename>\n");
  50.         exit();
  51.     }
  52.  
  53.     if ((fd = open(filnam, 0)) == ERROR)
  54.     {
  55.         printf("Cannot open %s\n",filnam);
  56.         exit();
  57.     }
  58.  
  59.     filsiz = cfsize(fd);        /* find size of file in sectors */
  60.  
  61.     seek(fd, (NBSECTS < filsiz) ? -NBSECTS : -filsiz, 2); /* seek to EOF */
  62.  
  63.     if (!(sects = read(fd, txtbuf, NBSECTS)))
  64.     {
  65.         printf("%s is an empty file.\n",filnam);
  66.         exit();
  67.     }
  68.  
  69.     lastsec = txtbuf + ((sects-1) * SECSIZ);
  70.     lastsec[SECSIZ] = '\0';
  71.     for (txtptr = lastsec; !lastchar(*txtptr); txtptr++)
  72.         ;
  73.     --txtptr;    /* txtptr now points to last char of text */
  74.  
  75.     lincnt = 0;
  76.  
  77.     while (1)
  78.     {
  79.         if (txtptr < txtbuf)
  80.         {
  81.             puts("Text buffer not large enough. Aborting.\n");
  82.             exit();
  83.         }
  84.         if (*txtptr == '\n')
  85.             lincnt++;
  86.         if (lincnt == lineno)
  87.             break;
  88.         txtptr--;        
  89.     }
  90.  
  91.     while (!lastchar(c = *txtptr++))
  92.             putchar(c);
  93.  
  94.     close(fd);
  95. }
  96.                 
  97.  
  98.  
  99. int lastchar(c)
  100. char c;
  101. {
  102.     return (!c) || (c == CPMEOF);
  103. }
  104.