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. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 static char copyright[] = 36 "@(#) Copyright (c) 1980, 1987, 1992, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 static char sccsid[] = "@(#)head.c 8.1 (Berkeley) 6/6/93"; 42 #endif /* not lint */ 43 44 #include <sys/types.h> 45 #include <errno.h> 46 #include <stdlib.h> 47 #include <string.h> 48 #include <stdio.h> 49 #include <ctype.h> 50 51 /* 52 * head - give the first few lines of a stream or of each of a set of files 53 * 54 * Bill Joy UCB August 24, 1977 55 */ 56 57 void err __P((int, const char *, ...)); 58 void head __P((FILE *, int)); 59 void obsolete __P((char *[])); 60 void usage __P((void)); 61 62 int eval; 63 64 int 65 main(argc, argv) 66 int argc; 67 char *argv[]; 68 { 69 register int ch; 70 FILE *fp; 71 int first, linecnt; 72 char *ep; 73 74 obsolete(argv); 75 linecnt = 10; 76 while ((ch = getopt(argc, argv, "n:")) != EOF) 77 switch(ch) { 78 case 'n': 79 linecnt = strtol(optarg, &ep, 10); 80 if (*ep || linecnt <= 0) 81 err(1, "illegal line count -- %s", optarg); 82 break; 83 case '?': 84 default: 85 usage(); 86 } 87 argc -= optind; 88 argv += optind; 89 90 if (*argv) 91 for (first = 1; *argv; ++argv) { 92 if ((fp = fopen(*argv, "r")) == NULL) { 93 err(0, "%s: %s", *argv, strerror(errno)); 94 continue; 95 } 96 if (argc > 1) { 97 (void)printf("%s==> %s <==\n", 98 first ? "" : "\n", *argv); 99 first = 0; 100 } 101 head(fp, linecnt); 102 (void)fclose(fp); 103 } 104 else 105 head(stdin, linecnt); 106 exit(eval); 107 } 108 109 void 110 head(fp, cnt) 111 FILE *fp; 112 register int cnt; 113 { 114 register int ch; 115 116 while (cnt--) 117 while ((ch = getc(fp)) != EOF) { 118 if (putchar(ch) == EOF) 119 err(1, "stdout: %s", strerror(errno)); 120 if (ch == '\n') 121 break; 122 } 123 } 124 125 void 126 obsolete(argv) 127 char *argv[]; 128 { 129 char *ap; 130 131 while (ap = *++argv) { 132 /* Return if "--" or not "-[0-9]*". */ 133 if (ap[0] != '-' || ap[1] == '-' || !isdigit(ap[1])) 134 return; 135 if ((ap = malloc(strlen(*argv) + 2)) == NULL) 136 err(1, "%s", strerror(errno)); 137 ap[0] = '-'; 138 ap[1] = 'n'; 139 (void)strcpy(ap + 2, *argv + 1); 140 *argv = ap; 141 } 142 } 143 144 void 145 usage() 146 { 147 (void)fprintf(stderr, "usage: head [-n lines] [file ...]\n"); 148 exit(1); 149 } 150 151 #if __STDC__ 152 #include <stdarg.h> 153 #else 154 #include <varargs.h> 155 #endif 156 157 void 158 #if __STDC__ 159 err(int fatal, const char *fmt, ...) 160 #else 161 err(fatal, fmt, va_alist) 162 int fatal; 163 char *fmt; 164 va_dcl 165 #endif 166 { 167 va_list ap; 168 #if __STDC__ 169 va_start(ap, fmt); 170 #else 171 va_start(ap); 172 #endif 173 (void)fprintf(stderr, "head: "); 174 (void)vfprintf(stderr, fmt, ap); 175 va_end(ap); 176 (void)fprintf(stderr, "\n"); 177 if (fatal) 178 exit(1); 179 eval = 1; 180 } 181