home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 154_01 / head.c < prev    next >
Text File  |  1979-12-31  |  1KB  |  57 lines

  1. /* head.c:    display first n lines of text files -
  2.  
  3. -----
  4.     (c) Chuck Allison, 1985
  5. -----
  6. */
  7.  
  8. #include <stdio.h>
  9. #define MAXLINE 80
  10. #define MAXFILES 150
  11.  
  12. main(argc,argv)
  13. int argc;
  14. char *argv[];
  15. {
  16.     int i, count, n = 5, xargc; 
  17.     char line[MAXLINE], *xargv[MAXFILES];
  18.     FILE *f;
  19.  
  20.     if (argc < 2)
  21.     {
  22.         puts("usage:  head [-n] file1 file2 ...");
  23.         exit(1);
  24.     }
  25.  
  26.     /* ..process optional head count.. */
  27.     if (argv[1][0] == '-')
  28.     {
  29.         if (argc == 2)
  30.         {
  31.             puts("usage:  head [-n] file1 file2 ...");
  32.             exit(1);
  33.         }
  34.         n = atoi(argv[1]+1);
  35.         ++argv;
  36.         --argc;
  37.     }
  38.  
  39.     /* ..expand filespecs (Mark Williams C only!).. */
  40.     xargc = exargs("files",argc,argv,xargv,MAXFILES);
  41.  
  42.     for (i = 0 ; i < xargc; ++i)
  43.         if ((f = fopen(xargv[i],"r")) != NULL)
  44.         {
  45.             printf("-----  %s  -----\n",xargv[i]);
  46.  
  47.             /* ..get lines.. */
  48.             for (count=0; count<n && fgets(line,MAXLINE-1,f); count++)
  49.                 fputs(line,stdout);
  50.  
  51.             puts("-----  end  -----\n\n\n");
  52.             fclose(f);
  53.         }
  54.         else
  55.             fprintf(stderr,"couldn't open %s\n",xargv[i]);
  56. }
  57.