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 */
28*a9e41468SRobert Mustacchi /* 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 */
39bd6433ebSRobert Mustacchi /*
404e6070e8SRobert Mustacchi * Copyright (c) 2014, Joyent, Inc. All rights reserved.
41*a9e41468SRobert Mustacchi * Copyright 2020 Oxide Computer Company
42bd6433ebSRobert Mustacchi */
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate #include <stdio.h>
467c478bd9Sstevel@tonic-gate #include <stdlib.h>
477c478bd9Sstevel@tonic-gate #include <unistd.h>
487c478bd9Sstevel@tonic-gate #include <locale.h>
497c478bd9Sstevel@tonic-gate #include <string.h>
507c478bd9Sstevel@tonic-gate #include <ctype.h>
51*a9e41468SRobert Mustacchi #include <err.h>
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate #define DEF_LINE_COUNT 10
547c478bd9Sstevel@tonic-gate
55bd6433ebSRobert Mustacchi static void copyout(off_t, int);
567c478bd9Sstevel@tonic-gate static void Usage();
574e6070e8SRobert Mustacchi static FILE *input;
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate /*
61bd6433ebSRobert Mustacchi * head - give the first few lines of a stream or of each of a set of files.
62bd6433ebSRobert Mustacchi * Optionally shows a specific number of bytes instead.
637c478bd9Sstevel@tonic-gate */
647c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)657c478bd9Sstevel@tonic-gate main(int argc, char **argv)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate int fileCount;
687c478bd9Sstevel@tonic-gate int around = 0;
697c478bd9Sstevel@tonic-gate int i;
707c478bd9Sstevel@tonic-gate int opt;
717c478bd9Sstevel@tonic-gate off_t linecnt = DEF_LINE_COUNT;
72bd6433ebSRobert Mustacchi int isline = 1;
737c478bd9Sstevel@tonic-gate int error = 0;
74bd6433ebSRobert Mustacchi int quiet = 0;
75*a9e41468SRobert Mustacchi int verbose = 0;
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
787c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
797c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
807c478bd9Sstevel@tonic-gate #endif
817c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate /* check for non-standard "-line-count" option */
847c478bd9Sstevel@tonic-gate for (i = 1; i < argc; i++) {
857c478bd9Sstevel@tonic-gate if (strcmp(argv[i], "--") == 0)
867c478bd9Sstevel@tonic-gate break;
877c478bd9Sstevel@tonic-gate
887c478bd9Sstevel@tonic-gate if ((argv[i][0] == '-') && isdigit(argv[i][1])) {
897c478bd9Sstevel@tonic-gate if (strlen(&argv[i][1]) !=
907c478bd9Sstevel@tonic-gate strspn(&argv[i][1], "0123456789")) {
917c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
927c478bd9Sstevel@tonic-gate "%s: Badly formed number\n"), argv[0]);
937c478bd9Sstevel@tonic-gate Usage();
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate
96bd6433ebSRobert Mustacchi linecnt = (off_t)strtoll(&argv[i][1], (char **)NULL,
977c478bd9Sstevel@tonic-gate 10);
987c478bd9Sstevel@tonic-gate while (i < argc) {
997c478bd9Sstevel@tonic-gate argv[i] = argv[i + 1];
1007c478bd9Sstevel@tonic-gate i++;
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate argc--;
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate /* get options */
107bd6433ebSRobert Mustacchi while ((opt = getopt(argc, argv, "qvn:c:")) != EOF) {
1087c478bd9Sstevel@tonic-gate switch (opt) {
1097c478bd9Sstevel@tonic-gate case 'n':
110bd6433ebSRobert Mustacchi case 'c':
1117c478bd9Sstevel@tonic-gate if ((strcmp(optarg, "--") == 0) || (optind > argc)) {
1127c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
113bd6433ebSRobert Mustacchi "%s: Missing -%c argument\n"), argv[0],
114bd6433ebSRobert Mustacchi optopt);
1157c478bd9Sstevel@tonic-gate Usage();
1167c478bd9Sstevel@tonic-gate }
117bd6433ebSRobert Mustacchi linecnt = (off_t)strtoll(optarg, (char **)NULL, 10);
1187c478bd9Sstevel@tonic-gate if (linecnt <= 0) {
1197c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
120bd6433ebSRobert Mustacchi "%s: Invalid \"-%c %s\" option\n"),
121bd6433ebSRobert Mustacchi argv[0], optopt, optarg);
1227c478bd9Sstevel@tonic-gate Usage();
1237c478bd9Sstevel@tonic-gate }
124bd6433ebSRobert Mustacchi isline = optopt != 'c';
125bd6433ebSRobert Mustacchi break;
126bd6433ebSRobert Mustacchi case 'q':
127bd6433ebSRobert Mustacchi quiet = 1;
128*a9e41468SRobert Mustacchi verbose = 0;
129bd6433ebSRobert Mustacchi break;
130bd6433ebSRobert Mustacchi case 'v':
131bd6433ebSRobert Mustacchi quiet = 0;
132*a9e41468SRobert Mustacchi verbose = 1;
1337c478bd9Sstevel@tonic-gate break;
1347c478bd9Sstevel@tonic-gate default:
1357c478bd9Sstevel@tonic-gate Usage();
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate
1397c478bd9Sstevel@tonic-gate fileCount = argc - optind;
1407c478bd9Sstevel@tonic-gate
1417c478bd9Sstevel@tonic-gate do {
1427c478bd9Sstevel@tonic-gate if ((argv[optind] == NULL) && around)
1437c478bd9Sstevel@tonic-gate break;
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate if (argv[optind] != NULL) {
1464e6070e8SRobert Mustacchi if (input != NULL)
1474e6070e8SRobert Mustacchi (void) fclose(input);
1484e6070e8SRobert Mustacchi if ((input = fopen(argv[optind], "r")) == NULL) {
1497c478bd9Sstevel@tonic-gate perror(argv[optind]);
1507c478bd9Sstevel@tonic-gate error = 1;
1517c478bd9Sstevel@tonic-gate optind++;
1527c478bd9Sstevel@tonic-gate continue;
1537c478bd9Sstevel@tonic-gate }
1544e6070e8SRobert Mustacchi } else {
1554e6070e8SRobert Mustacchi input = stdin;
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate
158bd6433ebSRobert Mustacchi if (quiet == 0) {
159bd6433ebSRobert Mustacchi if (around)
160bd6433ebSRobert Mustacchi (void) putchar('\n');
1617c478bd9Sstevel@tonic-gate
162*a9e41468SRobert Mustacchi if (fileCount > 1 || verbose != 0) {
163*a9e41468SRobert Mustacchi const char *file = argv[optind];
164*a9e41468SRobert Mustacchi if (file == NULL) {
165*a9e41468SRobert Mustacchi file = "standard input";
166*a9e41468SRobert Mustacchi }
167*a9e41468SRobert Mustacchi (void) printf("==> %s <==\n", file);
168*a9e41468SRobert Mustacchi }
169bd6433ebSRobert Mustacchi }
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate if (argv[optind] != NULL)
1727c478bd9Sstevel@tonic-gate optind++;
1737c478bd9Sstevel@tonic-gate
174bd6433ebSRobert Mustacchi copyout(linecnt, isline);
1757c478bd9Sstevel@tonic-gate (void) fflush(stdout);
1767c478bd9Sstevel@tonic-gate around++;
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate } while (argv[optind] != NULL);
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate return (error);
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate
1837c478bd9Sstevel@tonic-gate static void
copyout(off_t cnt,int isline)184bd6433ebSRobert Mustacchi copyout(off_t cnt, int isline)
1857c478bd9Sstevel@tonic-gate {
1867c478bd9Sstevel@tonic-gate char lbuf[BUFSIZ];
1877c478bd9Sstevel@tonic-gate size_t len;
1887c478bd9Sstevel@tonic-gate
189*a9e41468SRobert Mustacchi while (cnt > 0) {
190*a9e41468SRobert Mustacchi len = fread(lbuf, sizeof (char), sizeof (lbuf) / sizeof (char),
191*a9e41468SRobert Mustacchi input);
192*a9e41468SRobert Mustacchi if (len == 0) {
193*a9e41468SRobert Mustacchi return;
194*a9e41468SRobert Mustacchi }
195*a9e41468SRobert Mustacchi
196bd6433ebSRobert Mustacchi if (isline) {
197*a9e41468SRobert Mustacchi size_t i;
198*a9e41468SRobert Mustacchi for (i = 0; i < len; i++) {
199*a9e41468SRobert Mustacchi if (lbuf[i] == '\n') {
200bd6433ebSRobert Mustacchi cnt--;
201*a9e41468SRobert Mustacchi if (cnt == 0) {
202*a9e41468SRobert Mustacchi i++;
203*a9e41468SRobert Mustacchi break;
204*a9e41468SRobert Mustacchi }
205bd6433ebSRobert Mustacchi }
2067c478bd9Sstevel@tonic-gate }
207*a9e41468SRobert Mustacchi len = i;
208bd6433ebSRobert Mustacchi } else {
209bd6433ebSRobert Mustacchi if (len > cnt) {
210bd6433ebSRobert Mustacchi len = cnt;
211bd6433ebSRobert Mustacchi }
212bd6433ebSRobert Mustacchi cnt -= len;
213*a9e41468SRobert Mustacchi }
214*a9e41468SRobert Mustacchi
215*a9e41468SRobert Mustacchi if (fwrite(lbuf, sizeof (char), len, stdout) != len) {
216*a9e41468SRobert Mustacchi err(EXIT_FAILURE, "failed to write to stdout");
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate static void
Usage()2227c478bd9Sstevel@tonic-gate Usage()
2237c478bd9Sstevel@tonic-gate {
224bd6433ebSRobert Mustacchi (void) printf(gettext("usage: head [-q] [-v] [-n #] [-c #] [-#] "
225bd6433ebSRobert Mustacchi "[filename...]\n"));
2267c478bd9Sstevel@tonic-gate exit(1);
2277c478bd9Sstevel@tonic-gate }
228