home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume3 / head / head.c < prev   
Encoding:
C/C++ Source or Header  |  1986-11-30  |  4.8 KB  |  144 lines

  1. /* head - replace head command */
  2. /*
  3.  * call: head [ +n ] [ -n ] [ file ... ]
  4.  *
  5.  *       +n = number of lines to skip before starting display
  6.  *            default = 0
  7.  *       -n = number of lines to display, starting with top of file
  8.  *            default = 10
  9.  *       file = any number of files may be present
  10.  *                if name is `-', read from standard input
  11.  */
  12. #include    <stdio.h>
  13.  
  14. #define FALSE 0
  15. #define TRUE 1
  16.  
  17. void dofile ();
  18.  
  19. main (ac, av)
  20. int     ac;
  21. char    **av;
  22. {
  23.     register FILE *in = stdin;          /* default is standard input    */
  24.  
  25.     register int header = FALSE;        /* TRUE if multiple files       */
  26.  
  27.     register char *cp;                  /* general purpose pointer      */
  28.  
  29.     register long nlines = 10;          /* number of lines to display   */
  30.     register long skip = 0;             /* number of lines to skip      */
  31.  
  32.     register char *pgm;                 /* program name                 */
  33.  
  34.     if (pgm = strrchr (av[0], '/')) pgm++;
  35.     else                            pgm = av[0];
  36.  
  37.     /* ------------------------------------------------------------ */
  38.     /* Adjust args to skip program name                             */
  39.     /* ------------------------------------------------------------ */
  40.  
  41.     av++; ac--;
  42.  
  43.     /* ------------------------------------------------------------ */
  44.     /* process arguments/options                                    */
  45.     /*   it is assumed that the options will precede all file names */
  46.     /*   on the command line                                        */
  47.     /* ------------------------------------------------------------ */
  48.  
  49.     while (ac)
  50.     {
  51.         if (*av[0] == '+')              /* lines to skip            */
  52.         {
  53.             skip = atoi (&av[0][1]);
  54.             ac--; av++;
  55.         }
  56.         else if (*av[0] == '-')         /* lines to display         */
  57.         {
  58.             nlines = atoi (&av[0][1]);
  59.             ac--; av++;
  60.         }
  61.         else break;
  62.     }
  63.  
  64.     /* ------------------------------------------------------------ */
  65.     /* If any files, then process each                              */
  66.     /* ------------------------------------------------------------ */
  67.  
  68.     if (ac)
  69.     {
  70.     /* ------------------------------------------------------------ */
  71.     /* If more than one file name specified, display headers        */
  72.     /* ------------------------------------------------------------ */
  73.  
  74.         header = ac > 1;
  75.         while (ac--)
  76.         {
  77.             if (!strcmp (*av, "-"))
  78.             {
  79.                 dofile (stdin, "standard input", skip, nlines, header);
  80.             }
  81.             else if (!(in = fopen (*av, "r")))
  82.             {
  83.                 fprintf (stderr,
  84.                     "head: cannot read file %s - skipping\n", *av);
  85.             }
  86.             else
  87.             {
  88.                 dofile (in, *av, skip, nlines, header);
  89.                 fclose (in);
  90.             }
  91.             av++;
  92.         }
  93.     }
  94.     else dofile (stdin, "standard input", skip, nlines, FALSE);
  95.  
  96.     exit (0);
  97. }
  98. /* ------------------------------------------------------------ */
  99. /* Process a file, skipping and displaying as necessary         */
  100. /* ------------------------------------------------------------ */
  101. void
  102. dofile (in, inname, skip, nlines, dohead)
  103. register FILE *in;          /* input stream                         */
  104. register char *inname;      /* input file name                      */
  105. register long skip;         /* number of lines to skip              */
  106. register long nlines;       /* number of lines to display           */
  107.          int dohead;        /* TRUE if header is to be displayed    */
  108. {
  109.     register char *c;           /* convenient local pointer         */
  110.     register long lineno = 0;   /* line number in file              */
  111.  
  112.     char    inbuf[1024];        /* line buffer                      */
  113.  
  114.     /* ------------------------------------------------------------ */
  115.     /* Print header if necessary                                    */
  116.     /* ------------------------------------------------------------ */
  117.  
  118.     if (dohead)
  119.     {
  120.         printf ("\
  121. +-----------------------------------------------------------------+\n\
  122. +                %-40.40s         +\n\
  123. +-----------------------------------------------------------------+\n",
  124.             inname);
  125.     }
  126.  
  127.     /* ------------------------------------------------------------ */
  128.     /* Skip lines                                                   */
  129.     /* ------------------------------------------------------------ */
  130.  
  131.     while (lineno++ < skip && fgets (inbuf, sizeof inbuf, in));
  132.  
  133.     /* ------------------------------------------------------------ */
  134.     /* Display lines                                                */
  135.     /* ------------------------------------------------------------ */
  136.  
  137.     lineno = 0;
  138.  
  139.     while (lineno++ < nlines && fgets (inbuf, sizeof inbuf, in))
  140.         fputs (inbuf, stdout);
  141.     
  142.     return;
  143. }
  144.