xref: /titanic_52/usr/src/cmd/head/head.c (revision bd6433eb4d5b16c480f15cd77e2201c318b2556d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate  * The Regents of the University of California
337c478bd9Sstevel@tonic-gate  * All Rights Reserved
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate  * contributors.
387c478bd9Sstevel@tonic-gate  */
39*bd6433ebSRobert Mustacchi /*
40*bd6433ebSRobert Mustacchi  * Copyright (c) 2013, Joyent, Inc.  All rights reserved.
41*bd6433ebSRobert Mustacchi  */
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include <stdio.h>
457c478bd9Sstevel@tonic-gate #include <stdlib.h>
467c478bd9Sstevel@tonic-gate #include <unistd.h>
477c478bd9Sstevel@tonic-gate #include <locale.h>
487c478bd9Sstevel@tonic-gate #include <string.h>
497c478bd9Sstevel@tonic-gate #include <ctype.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #define	DEF_LINE_COUNT	10
527c478bd9Sstevel@tonic-gate 
53*bd6433ebSRobert Mustacchi static	void	copyout(off_t, int);
547c478bd9Sstevel@tonic-gate static	void	Usage();
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /*
58*bd6433ebSRobert Mustacchi  * head - give the first few lines of a stream or of each of a set of files.
59*bd6433ebSRobert Mustacchi  * Optionally shows a specific number of bytes instead.
607c478bd9Sstevel@tonic-gate  */
617c478bd9Sstevel@tonic-gate int
627c478bd9Sstevel@tonic-gate main(int argc, char **argv)
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate 	int	fileCount;
657c478bd9Sstevel@tonic-gate 	int	around = 0;
667c478bd9Sstevel@tonic-gate 	int	i;
677c478bd9Sstevel@tonic-gate 	int	opt;
687c478bd9Sstevel@tonic-gate 	off_t	linecnt	= DEF_LINE_COUNT;
69*bd6433ebSRobert Mustacchi 	int	isline = 1;
707c478bd9Sstevel@tonic-gate 	int	error = 0;
71*bd6433ebSRobert Mustacchi 	int	quiet = 0;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
747c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
757c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
767c478bd9Sstevel@tonic-gate #endif
777c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	/* check for non-standard "-line-count" option */
807c478bd9Sstevel@tonic-gate 	for (i = 1; i < argc; i++) {
817c478bd9Sstevel@tonic-gate 		if (strcmp(argv[i], "--") == 0)
827c478bd9Sstevel@tonic-gate 			break;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 		if ((argv[i][0] == '-') && isdigit(argv[i][1])) {
857c478bd9Sstevel@tonic-gate 			if (strlen(&argv[i][1]) !=
867c478bd9Sstevel@tonic-gate 			    strspn(&argv[i][1], "0123456789")) {
877c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
887c478bd9Sstevel@tonic-gate 				    "%s: Badly formed number\n"), argv[0]);
897c478bd9Sstevel@tonic-gate 				Usage();
907c478bd9Sstevel@tonic-gate 			}
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 			linecnt = (off_t)strtoll(&argv[i][1], (char **)NULL,
937c478bd9Sstevel@tonic-gate 			    10);
947c478bd9Sstevel@tonic-gate 			while (i < argc) {
957c478bd9Sstevel@tonic-gate 				argv[i] = argv[i + 1];
967c478bd9Sstevel@tonic-gate 				i++;
977c478bd9Sstevel@tonic-gate 			}
987c478bd9Sstevel@tonic-gate 			argc--;
997c478bd9Sstevel@tonic-gate 		}
1007c478bd9Sstevel@tonic-gate 	}
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	/* get options */
103*bd6433ebSRobert Mustacchi 	while ((opt = getopt(argc, argv, "qvn:c:")) != EOF) {
1047c478bd9Sstevel@tonic-gate 		switch (opt) {
1057c478bd9Sstevel@tonic-gate 		case 'n':
106*bd6433ebSRobert Mustacchi 		case 'c':
1077c478bd9Sstevel@tonic-gate 			if ((strcmp(optarg, "--") == 0) || (optind > argc)) {
1087c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
109*bd6433ebSRobert Mustacchi 				    "%s: Missing -%c argument\n"), argv[0],
110*bd6433ebSRobert Mustacchi 				    optopt);
1117c478bd9Sstevel@tonic-gate 				Usage();
1127c478bd9Sstevel@tonic-gate 			}
1137c478bd9Sstevel@tonic-gate 			linecnt = (off_t)strtoll(optarg, (char **)NULL, 10);
1147c478bd9Sstevel@tonic-gate 			if (linecnt <= 0) {
1157c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
116*bd6433ebSRobert Mustacchi 				    "%s: Invalid \"-%c %s\" option\n"),
117*bd6433ebSRobert Mustacchi 				    argv[0], optopt, optarg);
1187c478bd9Sstevel@tonic-gate 				Usage();
1197c478bd9Sstevel@tonic-gate 			}
120*bd6433ebSRobert Mustacchi 			isline = optopt != 'c';
1217c478bd9Sstevel@tonic-gate 			break;
122*bd6433ebSRobert Mustacchi 		case 'q':
123*bd6433ebSRobert Mustacchi 			quiet = 1;
124*bd6433ebSRobert Mustacchi 			break;
125*bd6433ebSRobert Mustacchi 		case 'v':
126*bd6433ebSRobert Mustacchi 			quiet = 0;
127*bd6433ebSRobert Mustacchi 			break;
1287c478bd9Sstevel@tonic-gate 		default:
1297c478bd9Sstevel@tonic-gate 			Usage();
1307c478bd9Sstevel@tonic-gate 		}
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	fileCount = argc - optind;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	do {
1367c478bd9Sstevel@tonic-gate 		if ((argv[optind] == NULL) && around)
1377c478bd9Sstevel@tonic-gate 			break;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 		if (argv[optind] != NULL) {
1407c478bd9Sstevel@tonic-gate 			(void) close(0);
1417c478bd9Sstevel@tonic-gate 			if (freopen(argv[optind], "r", stdin) == NULL) {
1427c478bd9Sstevel@tonic-gate 				perror(argv[optind]);
1437c478bd9Sstevel@tonic-gate 				error = 1;
1447c478bd9Sstevel@tonic-gate 				optind++;
1457c478bd9Sstevel@tonic-gate 				continue;
1467c478bd9Sstevel@tonic-gate 			}
1477c478bd9Sstevel@tonic-gate 		}
1487c478bd9Sstevel@tonic-gate 
149*bd6433ebSRobert Mustacchi 		if (quiet == 0) {
1507c478bd9Sstevel@tonic-gate 			if (around)
1517c478bd9Sstevel@tonic-gate 				(void) putchar('\n');
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 			if (fileCount > 1)
1547c478bd9Sstevel@tonic-gate 				(void) printf("==> %s <==\n", argv[optind]);
155*bd6433ebSRobert Mustacchi 		}
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 		if (argv[optind] != NULL)
1587c478bd9Sstevel@tonic-gate 			optind++;
1597c478bd9Sstevel@tonic-gate 
160*bd6433ebSRobert Mustacchi 		copyout(linecnt, isline);
1617c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
1627c478bd9Sstevel@tonic-gate 		around++;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	} while (argv[optind] != NULL);
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	return (error);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate static void
170*bd6433ebSRobert Mustacchi copyout(off_t cnt, int isline)
1717c478bd9Sstevel@tonic-gate {
1727c478bd9Sstevel@tonic-gate 	char lbuf[BUFSIZ];
1737c478bd9Sstevel@tonic-gate 	size_t len;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	while (cnt > 0 && fgets(lbuf, sizeof (lbuf), stdin) != 0) {
176*bd6433ebSRobert Mustacchi 		len = strlen(lbuf);
177*bd6433ebSRobert Mustacchi 		if (isline) {
1787c478bd9Sstevel@tonic-gate 			(void) printf("%s", lbuf);
179*bd6433ebSRobert Mustacchi 			/*
180*bd6433ebSRobert Mustacchi 			 * only count as a line if buffer read ends with newline
181*bd6433ebSRobert Mustacchi 			 */
182*bd6433ebSRobert Mustacchi 			if (len > 0) {
1837c478bd9Sstevel@tonic-gate 				if (lbuf[len - 1] == '\n') {
1847c478bd9Sstevel@tonic-gate 					(void) fflush(stdout);
1857c478bd9Sstevel@tonic-gate 					cnt--;
1867c478bd9Sstevel@tonic-gate 				}
1877c478bd9Sstevel@tonic-gate 			}
188*bd6433ebSRobert Mustacchi 		} else {
189*bd6433ebSRobert Mustacchi 			if (len > cnt) {
190*bd6433ebSRobert Mustacchi 				lbuf[cnt] = '\0';
191*bd6433ebSRobert Mustacchi 				len = cnt;
192*bd6433ebSRobert Mustacchi 			}
193*bd6433ebSRobert Mustacchi 			(void) printf("%s", lbuf);
194*bd6433ebSRobert Mustacchi 			cnt -= len;
195*bd6433ebSRobert Mustacchi 			(void) fflush(stdout);
196*bd6433ebSRobert Mustacchi 		}
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate static void
2017c478bd9Sstevel@tonic-gate Usage()
2027c478bd9Sstevel@tonic-gate {
203*bd6433ebSRobert Mustacchi 	(void) printf(gettext("usage: head [-q] [-v] [-n #] [-c #] [-#] "
204*bd6433ebSRobert Mustacchi 	    "[filename...]\n"));
2057c478bd9Sstevel@tonic-gate 	exit(1);
2067c478bd9Sstevel@tonic-gate }
207