home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / apple2 / 32 < prev    next >
Encoding:
Text File  |  1990-12-02  |  3.9 KB  |  214 lines

  1. Path: wuarchive!swbatl!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!usc!apple!rutgers!njin!paul.rutgers.edu!yoko.rutgers.edu!jac
  2. From: jac@yoko.rutgers.edu (Jonathan A. Chandross)
  3. Newsgroups: comp.sources.apple2
  4. Subject: v001SRC018:  tail -- Print Part Of A File
  5. Message-ID: <Dec.1.17.00.39.1990.24924@yoko.rutgers.edu>
  6. Date: 1 Dec 90 22:00:39 GMT
  7. Organization: Rutgers Univ., New Brunswick, N.J.
  8. Lines: 203
  9. Approved: jac@paul.rutgers.edu
  10.  
  11.  
  12. Submitted-by: NONE
  13. Posting-number: Volume 1, Source:18
  14. Archive-name: util/tail
  15. Architecture: ANY_2
  16. Version-number: 1.00
  17.  
  18. This C utility prints the first/last N lines/characters of a
  19. file.  Like more or page, but allows you to see only a portion
  20. of the beginning/end of a file. 
  21.  
  22. Enjoy.
  23.  
  24. =tail.c
  25. -
  26. -/*
  27. - * tail.c
  28. - *
  29. - * Print first/last N lines/characters part of a file.
  30. - *
  31. - * Usage:
  32. - *     tail [(-|+)number][lc] [file_1] [file_2] [file_3] [...]
  33. - *    tail [(-|+)number][lc] [file_1] [file_2] [file_3] [...]
  34. - *
  35. - *    Options:
  36. - *            -    tail last <number> lines from file
  37. - *            +    begin with line <number>
  38. - *            l    <number> indicates lines (default)
  39. - *            c    <number> indicates characters
  40. - *
  41. - *    If no files are specified, tail reads from standard in.
  42. - *
  43. - * Contributed Anonymously.  Written: November 1983
  44. - *
  45. - * Version 1.00
  46. - *
  47. - */
  48. -
  49. -#include "stdio.h"
  50. -
  51. -#define TRUE  1
  52. -#define FALSE 0
  53. -#define BLANK ' '
  54. -#define TAB   '\t'
  55. -#define NL    '\n'
  56. -#define EOS   '\0'
  57. -
  58. -int lines, chars ;
  59. -
  60. -main(argc, argv)
  61. -int argc ;
  62. -char *argv[] ;
  63. -{
  64. -    char *s ;
  65. -    FILE *input ;
  66. -    int count ;
  67. -
  68. -
  69. -    argc-- ; argv++ ;
  70. -    lines = TRUE ;
  71. -    chars = FALSE ;
  72. -    count = -10 ;
  73. -
  74. -    if( argc == 0 ) {
  75. -        tail( stdin, count ) ;
  76. -        exit(0) ;
  77. -    }
  78. -
  79. -    s = *argv ;
  80. -    if( *s == '-' || *s == '+' ) {
  81. -        s++ ;
  82. -        if( *s >= '0' && *s <= '9' ) {
  83. -            count = stoi( *argv ) ;
  84. -            s++ ;
  85. -            while( *s >= '0' && *s <= '9' )
  86. -                s++ ;
  87. -        }
  88. -        if( *s == 'c' ) {
  89. -            chars = TRUE ;
  90. -            lines = FALSE ;
  91. -        }
  92. -        else if( *s != 'l' && *s != EOS ) {
  93. -            fprintf(stderr, "tail: unknown option %c\n", *s ) ;
  94. -            argc = 0 ;
  95. -        }
  96. -        argc-- ; argv++ ;
  97. -    }
  98. -
  99. -    if( argc < 0 ) {
  100. -        fprintf(stderr, "usage: tail [+/-[number][lc]] [files]\n");
  101. -        exit(1) ;
  102. -    }
  103. -
  104. -    if( argc == 0 )
  105. -        tail( stdin, count ) ;
  106. -
  107. -    else if( (input=fopen(*argv,"r")) == NULL ) {
  108. -        fprintf(stderr, "tail: can't open %s\n", *argv) ;
  109. -        exit(1) ;
  110. -    }
  111. -    else {
  112. -        tail( input, count ) ;
  113. -        fclose( input ) ;
  114. -    }
  115. -
  116. -    exit(0) ;
  117. -
  118. -} /* end main */
  119. -
  120. -/* stoi - convert string to integer */
  121. -
  122. -stoi(s)
  123. -char *s ;
  124. -{
  125. -    int n, sign ;
  126. -
  127. -    while( *s == BLANK || *s == NL || *s == TAB )
  128. -        s++ ;
  129. -
  130. -    sign = 1 ;
  131. -    if( *s == '+' )
  132. -        s++ ;
  133. -    else if( *s == '-' ) {
  134. -        sign = -1 ;
  135. -        s++ ;
  136. -    }
  137. -    for( n=0 ; *s >= '0' && *s <= '9' ; s++ )
  138. -        n = 10 * n + *s - '0' ;
  139. -    return( sign * n ) ;
  140. -}
  141. -
  142. -/* tail - print 'count' lines/chars */
  143. -
  144. -#define INCR(p)  if(p >= end) p=cbuf ; else p++
  145. -#define BUFSIZE 4098
  146. -
  147. -char cbuf[ BUFSIZE ] ;
  148. -
  149. -tail( in, goal )
  150. -FILE *in ;
  151. -int goal ;
  152. -{
  153. -    int c, count ;
  154. -    char *start, *finish, *end ;
  155. -
  156. -    count = 0 ;
  157. -
  158. -    if( goal > 0 ) {    /* skip */
  159. -
  160. -        if( lines )        /* lines */
  161. -            while( (c=agetc(in)) != EOF ) {
  162. -                if( c == NL )
  163. -                    count++ ; 
  164. -                if( count >= goal )
  165. -                    break ;
  166. -            }
  167. -        else            /* chars */
  168. -            while( agetc(in) != EOF ) {
  169. -                count++ ;
  170. -                if( count >= goal )
  171. -                    break ;
  172. -            }
  173. -        if( count >= goal )
  174. -            while( (c=agetc(in)) != EOF )
  175. -                aputc(c, stdout ) ;
  176. -    }
  177. -
  178. -    else {                /* tail */
  179. -
  180. -        goal = -goal ;
  181. -        start = finish = cbuf ;
  182. -        end = &cbuf[ BUFSIZE - 1 ] ;
  183. -
  184. -        while( (c=agetc(in)) != EOF ) {
  185. -            *finish = c ;
  186. -            INCR( finish ) ;
  187. -
  188. -            if( start == finish )
  189. -                INCR( start ) ;
  190. -            if( !lines || c == NL )
  191. -                count++ ;
  192. -
  193. -            if( count > goal ) {
  194. -                count = goal ;
  195. -                if( lines )
  196. -                    while( *start != NL )
  197. -                        INCR( start ) ;
  198. -                INCR( start ) ;
  199. -            }
  200. -
  201. -        } /* end while */
  202. -
  203. -        while( start != finish ) {
  204. -            aputc( *start, stdout ) ;
  205. -            INCR( start ) ;
  206. -        }
  207. -
  208. -    } /* end else */
  209. -
  210. -} /* end tail */
  211. -
  212. -
  213. + END OF ARCHIVE
  214.