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.2 (Berkeley) 5/4/95"; 42 #endif /* not lint */ 43 44 #include <sys/types.h> 45 46 #include <ctype.h> 47 #include <errno.h> 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <string.h> 51 #include <unistd.h> 52 53 /* 54 * head - give the first few lines of a stream or of each of a set of files 55 * 56 * Bill Joy UCB August 24, 1977 57 */ 58 59 void err __P((int, const char *, ...)); 60 void head __P((FILE *, int)); 61 void obsolete __P((char *[])); 62 void usage __P((void)); 63 64 int eval; 65 66 int 67 main(argc, argv) 68 int argc; 69 char *argv[]; 70 { 71 register int ch; 72 FILE *fp; 73 int first, linecnt; 74 char *ep; 75 76 obsolete(argv); 77 linecnt = 10; 78 while ((ch = getopt(argc, argv, "n:")) != EOF) 79 switch(ch) { 80 case 'n': 81 linecnt = strtol(optarg, &ep, 10); 82 if (*ep || linecnt <= 0) 83 err(1, "illegal line count -- %s", optarg); 84 break; 85 case '?': 86 default: 87 usage(); 88 } 89 argc -= optind; 90 argv += optind; 91 92 if (*argv) 93 for (first = 1; *argv; ++argv) { 94 if ((fp = fopen(*argv, "r")) == NULL) { 95 err(0, "%s: %s", *argv, strerror(errno)); 96 continue; 97 } 98 if (argc > 1) { 99 (void)printf("%s==> %s <==\n", 100 first ? "" : "\n", *argv); 101 first = 0; 102 } 103 head(fp, linecnt); 104 (void)fclose(fp); 105 } 106 else 107 head(stdin, linecnt); 108 exit(eval); 109 } 110 111 void 112 head(fp, cnt) 113 FILE *fp; 114 register int cnt; 115 { 116 register int ch; 117 118 while (cnt && (ch = getc(fp)) != EOF) { 119 if (putchar(ch) == EOF) 120 err(1, "stdout: %s", strerror(errno)); 121 if (ch == '\n') 122 cnt--; 123 } 124 } 125 126 void 127 obsolete(argv) 128 char *argv[]; 129 { 130 char *ap; 131 132 while (ap = *++argv) { 133 /* Return if "--" or not "-[0-9]*". */ 134 if (ap[0] != '-' || ap[1] == '-' || !isdigit(ap[1])) 135 return; 136 if ((ap = malloc(strlen(*argv) + 2)) == NULL) 137 err(1, "%s", strerror(errno)); 138 ap[0] = '-'; 139 ap[1] = 'n'; 140 (void)strcpy(ap + 2, *argv + 1); 141 *argv = ap; 142 } 143 } 144 145 void 146 usage() 147 { 148 (void)fprintf(stderr, "usage: head [-n lines] [file ...]\n"); 149 exit(1); 150 } 151 152 #if __STDC__ 153 #include <stdarg.h> 154 #else 155 #include <varargs.h> 156 #endif 157 158 void 159 #if __STDC__ 160 err(int fatal, const char *fmt, ...) 161 #else 162 err(fatal, fmt, va_alist) 163 int fatal; 164 char *fmt; 165 va_dcl 166 #endif 167 { 168 va_list ap; 169 #if __STDC__ 170 va_start(ap, fmt); 171 #else 172 va_start(ap); 173 #endif 174 (void)fprintf(stderr, "head: "); 175 (void)vfprintf(stderr, fmt, ap); 176 va_end(ap); 177 (void)fprintf(stderr, "\n"); 178 if (fatal) 179 exit(1); 180 eval = 1; 181 } 182