home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / t / tail.zip / TAIL.C < prev    next >
Text File  |  1988-07-28  |  3KB  |  165 lines

  1. /* Subject: Aztec C source - tail.c */
  2. /* tail - print last part of file */
  3.  
  4. #include "stdio.h"
  5. #define TRUE 1
  6. #define FALSE 0
  7. #define BLANK ' '
  8. #define NL 0x0a
  9. #define TAB 0x09
  10.  
  11. int lines, chars ;
  12.  
  13. main(argc, argv)
  14. int argc ;
  15. char *argv[] ;
  16. {
  17.     char *s ;
  18.     FILE *input ;
  19.     int count ;
  20.  
  21.  
  22.     argc-- ; argv++ ;
  23.     lines = TRUE ;
  24.     chars = FALSE ;
  25.     count = -10 ;
  26.  
  27.     if( argc == 0 ) {
  28.         tail( stdin, count ) ;
  29.         exit(0) ;
  30.     }
  31.  
  32.     s = *argv ;
  33.     if( *s == '-' || *s == '+' ) {
  34.         s++ ;
  35.         if( *s >= '0' && *s <= '9' ) {
  36.             count = stoi( *argv ) ;
  37.             s++ ;
  38.             while( *s >= '0' && *s <= '9' )
  39.                 s++ ;
  40.         }
  41.         if( *s == 'c' ) {
  42.             chars = TRUE ;
  43.             lines = FALSE ;
  44.         }
  45.         else if( *s != 'l' && *s != EOS ) {
  46.             fprintf(stderr, "tail: unknown option %c\n", *s ) ;
  47.             argc = 0 ;
  48.         }
  49.         argc-- ; argv++ ;
  50.     }
  51.  
  52.     if( argc < 0 ) {
  53.         fprintf(stderr, "usage: tail [+/-[number][lc]] [files]\n");
  54.         exit(1) ;
  55.     }
  56.  
  57.     if( argc == 0 )
  58.         tail( stdin, count ) ;
  59.  
  60.     else if( (input=fopen(*argv,"r")) == NULL ) {
  61.         fprintf(stderr, "tail: can't open %s\n", *argv) ;
  62.         exit(1) ;
  63.     }
  64.     else {
  65.         tail( input, count ) ;
  66.         fclose( input ) ;
  67.     }
  68.  
  69.     exit(0) ;
  70.  
  71. } /* end main */
  72.  
  73. /* stoi - convert string to integer */
  74.  
  75. stoi(s)
  76. char *s ;
  77. {
  78.     int n, sign ;
  79.  
  80.     while( *s == BLANK || *s == NL || *s == TAB )
  81.         s++ ;
  82.  
  83.     sign = 1 ;
  84.     if( *s == '+' )
  85.         s++ ;
  86.     else if( *s == '-' ) {
  87.         sign = -1 ;
  88.         s++ ;
  89.     }
  90.     for( n=0 ; *s >= '0' && *s <= '9' ; s++ )
  91.         n = 10 * n + *s - '0' ;
  92.     return( sign * n ) ;
  93. }
  94.  
  95. /* tail - print 'count' lines/chars */
  96.  
  97. #define INCR(p)  if(p >= end) p=cbuf ; else p++
  98. #define BUFSIZE 4098
  99.  
  100. char cbuf[ BUFSIZE ] ;
  101.  
  102. tail( in, goal )
  103. FILE *in ;
  104. int goal ;
  105. {
  106.     int c, count ;
  107.     char *start, *finish, *end ;
  108.  
  109.     count = 0 ;
  110.  
  111.     if( goal > 0 ) {    /* skip */
  112.  
  113.         if( lines )     /* lines */
  114.             while( (c=getc(in)) != EOF ) {
  115.                 if( c == NL )
  116.                     count++ ; 
  117.                 if( count >= goal )
  118.                     break ;
  119.             }
  120.         else            /* chars */
  121.             while( getc(in) != EOF ) {
  122.                 count++ ;
  123.                 if( count >= goal )
  124.                     break ;
  125.             }
  126.         if( count >= goal )
  127.             while( (c=getc(in)) != EOF )
  128.                 putc(c, stdout ) ;
  129.     }
  130.  
  131.     else {              /* tail */
  132.  
  133.         goal = -goal ;
  134.         start = finish = cbuf ;
  135.         end = &cbuf[ BUFSIZE - 1 ] ;
  136.  
  137.         while( (c=getc(in)) != EOF ) {
  138.             *finish = c ;
  139.             INCR( finish ) ;
  140.  
  141.             if( start == finish )
  142.                 INCR( start ) ;
  143.             if( !lines || c == NL )
  144.                 count++ ;
  145.  
  146.             if( count > goal ) {
  147.                 count = goal ;
  148.                 if( lines )
  149.                     while( *start != NL )
  150.                         INCR( start ) ;
  151.                 INCR( start ) ;
  152.             }
  153.  
  154.         } /* end while */
  155.  
  156.         while( start != finish ) {
  157.             putc( *start, stdout ) ;
  158.             INCR( start ) ;
  159.         }
  160.  
  161.     } /* end else */
  162.  
  163. } /* end tail */
  164.  
  165.