xref: /illumos-gate/usr/src/cmd/head/head.c (revision 196c7f05d2deba7404e90ad67f3861185c78ca2d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 /*
40  * Copyright (c) 2014, Joyent, Inc.  All rights reserved.
41  */
42 
43 
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 #include <locale.h>
48 #include <string.h>
49 #include <ctype.h>
50 
51 #define	DEF_LINE_COUNT	10
52 
53 static	void	copyout(off_t, int);
54 static	void	Usage();
55 static	FILE	*input;
56 
57 
58 /*
59  * head - give the first few lines of a stream or of each of a set of files.
60  * Optionally shows a specific number of bytes instead.
61  */
62 int
63 main(int argc, char **argv)
64 {
65 	int	fileCount;
66 	int	around = 0;
67 	int	i;
68 	int	opt;
69 	off_t	linecnt	= DEF_LINE_COUNT;
70 	int	isline = 1;
71 	int	error = 0;
72 	int	quiet = 0;
73 
74 	(void) setlocale(LC_ALL, "");
75 #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
76 #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
77 #endif
78 	(void) textdomain(TEXT_DOMAIN);
79 
80 	/* check for non-standard "-line-count" option */
81 	for (i = 1; i < argc; i++) {
82 		if (strcmp(argv[i], "--") == 0)
83 			break;
84 
85 		if ((argv[i][0] == '-') && isdigit(argv[i][1])) {
86 			if (strlen(&argv[i][1]) !=
87 			    strspn(&argv[i][1], "0123456789")) {
88 				(void) fprintf(stderr, gettext(
89 				    "%s: Badly formed number\n"), argv[0]);
90 				Usage();
91 			}
92 
93 			linecnt = (off_t)strtoll(&argv[i][1], (char **)NULL,
94 			    10);
95 			while (i < argc) {
96 				argv[i] = argv[i + 1];
97 				i++;
98 			}
99 			argc--;
100 		}
101 	}
102 
103 	/* get options */
104 	while ((opt = getopt(argc, argv, "qvn:c:")) != EOF) {
105 		switch (opt) {
106 		case 'n':
107 		case 'c':
108 			if ((strcmp(optarg, "--") == 0) || (optind > argc)) {
109 				(void) fprintf(stderr, gettext(
110 				    "%s: Missing -%c argument\n"), argv[0],
111 				    optopt);
112 				Usage();
113 			}
114 			linecnt = (off_t)strtoll(optarg, (char **)NULL, 10);
115 			if (linecnt <= 0) {
116 				(void) fprintf(stderr, gettext(
117 				    "%s: Invalid \"-%c %s\" option\n"),
118 				    argv[0], optopt, optarg);
119 				Usage();
120 			}
121 			isline = optopt != 'c';
122 			break;
123 		case 'q':
124 			quiet = 1;
125 			break;
126 		case 'v':
127 			quiet = 0;
128 			break;
129 		default:
130 			Usage();
131 		}
132 	}
133 
134 	fileCount = argc - optind;
135 
136 	do {
137 		if ((argv[optind] == NULL) && around)
138 			break;
139 
140 		if (argv[optind] != NULL) {
141 			if (input != NULL)
142 				(void) fclose(input);
143 			if ((input = fopen(argv[optind], "r")) == NULL) {
144 				perror(argv[optind]);
145 				error = 1;
146 				optind++;
147 				continue;
148 			}
149 		} else {
150 			input = stdin;
151 		}
152 
153 		if (quiet == 0) {
154 			if (around)
155 				(void) putchar('\n');
156 
157 			if (fileCount > 1)
158 				(void) printf("==> %s <==\n", argv[optind]);
159 		}
160 
161 		if (argv[optind] != NULL)
162 			optind++;
163 
164 		copyout(linecnt, isline);
165 		(void) fflush(stdout);
166 		around++;
167 
168 	} while (argv[optind] != NULL);
169 
170 	return (error);
171 }
172 
173 static void
174 copyout(off_t cnt, int isline)
175 {
176 	char lbuf[BUFSIZ];
177 	size_t len;
178 
179 	while (cnt > 0 && fgets(lbuf, sizeof (lbuf), input) != 0) {
180 		len = strlen(lbuf);
181 		if (isline) {
182 			(void) printf("%s", lbuf);
183 			/*
184 			 * only count as a line if buffer read ends with newline
185 			 */
186 			if (len > 0) {
187 				if (lbuf[len - 1] == '\n') {
188 					(void) fflush(stdout);
189 					cnt--;
190 				}
191 			}
192 		} else {
193 			if (len > cnt) {
194 				lbuf[cnt] = '\0';
195 				len = cnt;
196 			}
197 			(void) printf("%s", lbuf);
198 			cnt -= len;
199 			(void) fflush(stdout);
200 		}
201 	}
202 }
203 
204 static void
205 Usage()
206 {
207 	(void) printf(gettext("usage: head [-q] [-v] [-n #] [-c #] [-#] "
208 	    "[filename...]\n"));
209 	exit(1);
210 }
211