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 && (ch = getc(fp)) != EOF) { 117 if (putchar(ch) == EOF) 118 err(1, "stdout: %s", strerror(errno)); 119 if (ch == '\n') 120 cnt--; 121 } 122 } 123 124 void 125 obsolete(argv) 126 char *argv[]; 127 { 128 char *ap; 129 130 while (ap = *++argv) { 131 /* Return if "--" or not "-[0-9]*". */ 132 if (ap[0] != '-' || ap[1] == '-' || !isdigit(ap[1])) 133 return; 134 if ((ap = malloc(strlen(*argv) + 2)) == NULL) 135 err(1, "%s", strerror(errno)); 136 ap[0] = '-'; 137 ap[1] = 'n'; 138 (void)strcpy(ap + 2, *argv + 1); 139 *argv = ap; 140 } 141 } 142 143 void 144 usage() 145 { 146 (void)fprintf(stderr, "usage: head [-n lines] [file ...]\n"); 147 exit(1); 148 } 149 150 #if __STDC__ 151 #include <stdarg.h> 152 #else 153 #include <varargs.h> 154 #endif 155 156 void 157 #if __STDC__ 158 err(int fatal, const char *fmt, ...) 159 #else 160 err(fatal, fmt, va_alist) 161 int fatal; 162 char *fmt; 163 va_dcl 164 #endif 165 { 166 va_list ap; 167 #if __STDC__ 168 va_start(ap, fmt); 169 #else 170 va_start(ap); 171 #endif 172 (void)fprintf(stderr, "head: "); 173 (void)vfprintf(stderr, fmt, ap); 174 va_end(ap); 175 (void)fprintf(stderr, "\n"); 176 if (fatal) 177 exit(1); 178 eval = 1; 179 } 180