Prex Home / Browse Source - Prex Version: 0.9.0

root/usr/bin/head/head.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. main
  2. head
  3. usage

   1 /*
   2  * Copyright (c) 1980, 1987, 1992, 1993
   3  *      The Regents of the University of California.  All rights reserved.
   4  *
   5  * Redistribution and use in source and binary forms, with or without
   6  * modification, are permitted provided that the following conditions
   7  * are met:
   8  * 1. Redistributions of source code must retain the above copyright
   9  *    notice, this list of conditions and the following disclaimer.
  10  * 2. Redistributions in binary form must reproduce the above copyright
  11  *    notice, this list of conditions and the following disclaimer in the
  12  *    documentation and/or other materials provided with the distribution.
  13  * 3. Neither the name of the University nor the names of its contributors
  14  *    may be used to endorse or promote products derived from this software
  15  *    without specific prior written permission.
  16  *
  17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27  * SUCH DAMAGE.
  28  */
  29 
  30 #include <sys/types.h>
  31 
  32 #include <ctype.h>
  33 #include <err.h>
  34 #include <errno.h>
  35 #include <stdio.h>
  36 #include <stdlib.h>
  37 #include <string.h>
  38 #include <unistd.h>
  39 
  40 /*
  41  * head - give the first few lines of a stream or of each of a set of files
  42  *
  43  * Bill Joy UCB August 24, 1977
  44  */
  45 
  46 #ifdef CMDBOX
  47 #define main(argc, argv)        head_main(argc, argv)
  48 #endif
  49 
  50 void head(FILE *, int);
  51 static void usage(void);
  52 
  53 int eval;
  54 
  55 int
  56 main(int argc, char *argv[])
  57 {
  58         int ch;
  59         FILE *fp;
  60         int first, linecnt;
  61         char *ep;
  62 
  63         linecnt = 10;
  64         while ((ch = getopt(argc, argv, "n:")) != EOF)
  65                 switch(ch) {
  66                 case 'n':
  67                         linecnt = strtol(optarg, &ep, 10);
  68                         if (*ep || linecnt <= 0)
  69                                 err(1, "illegal line count -- %s", optarg);
  70                         break;
  71                 case '?':
  72                 default:
  73                         usage();
  74                 }
  75         argc -= optind;
  76         argv += optind;
  77 
  78         if (*argv)
  79                 for (first = 1; *argv; ++argv) {
  80                         if ((fp = fopen(*argv, "r")) == NULL) {
  81                                 err(0, "%s: %s", *argv, strerror(errno));
  82                                 continue;
  83                         }
  84                         if (argc > 1) {
  85                                 (void)printf("%s==> %s <==\n",
  86                                     first ? "" : "\n", *argv);
  87                                 first = 0;
  88                         }
  89                         head(fp, linecnt);
  90                         (void)fclose(fp);
  91                 }
  92         else
  93                 head(stdin, linecnt);
  94         exit(eval);
  95 }
  96 
  97 void
  98 head(FILE *fp, int cnt)
  99 {
 100         int ch;
 101 
 102         while (cnt--)
 103                 while ((ch = getc(fp)) != EOF) {
 104                         if (putchar(ch) == EOF)
 105                                 err(1, "stdout: %s", strerror(errno));
 106                         if (ch == '\n')
 107                                 break;
 108                 }
 109 }
 110 
 111 static void
 112 usage()
 113 {
 114         (void)fprintf(stderr, "usage: head [-n lines] [file ...]\n");
 115         exit(1);
 116 }

/* [<][>][^][v][top][bottom][index][help] */