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
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.
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]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * University Copyright- Copyright (c) 1982, 1986, 1988
32 * The Regents of the University of California
35 * University Acknowledgment- Portions of this document are derived from
36 * software developed by the University of California, Berkeley, and its
40 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
51 #define DEF_LINE_COUNT 10
53 static void copyout(off_t
, int);
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.
63 main(int argc
, char **argv
)
69 off_t linecnt
= DEF_LINE_COUNT
;
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 */
78 (void) textdomain(TEXT_DOMAIN
);
80 /* check for non-standard "-line-count" option */
81 for (i
= 1; i
< argc
; i
++) {
82 if (strcmp(argv
[i
], "--") == 0)
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]);
93 linecnt
= (off_t
)strtoll(&argv
[i
][1], (char **)NULL
,
96 argv
[i
] = argv
[i
+ 1];
104 while ((opt
= getopt(argc
, argv
, "qvn:c:")) != EOF
) {
108 if ((strcmp(optarg
, "--") == 0) || (optind
> argc
)) {
109 (void) fprintf(stderr
, gettext(
110 "%s: Missing -%c argument\n"), argv
[0],
114 linecnt
= (off_t
)strtoll(optarg
, (char **)NULL
, 10);
116 (void) fprintf(stderr
, gettext(
117 "%s: Invalid \"-%c %s\" option\n"),
118 argv
[0], optopt
, optarg
);
121 isline
= optopt
!= 'c';
134 fileCount
= argc
- optind
;
137 if ((argv
[optind
] == NULL
) && around
)
140 if (argv
[optind
] != NULL
) {
142 (void) fclose(input
);
143 if ((input
= fopen(argv
[optind
], "r")) == NULL
) {
144 perror(argv
[optind
]);
155 (void) putchar('\n');
158 (void) printf("==> %s <==\n", argv
[optind
]);
161 if (argv
[optind
] != NULL
)
164 copyout(linecnt
, isline
);
165 (void) fflush(stdout
);
168 } while (argv
[optind
] != NULL
);
174 copyout(off_t cnt
, int isline
)
179 while (cnt
> 0 && fgets(lbuf
, sizeof (lbuf
), input
) != 0) {
182 (void) printf("%s", lbuf
);
184 * only count as a line if buffer read ends with newline
187 if (lbuf
[len
- 1] == '\n') {
188 (void) fflush(stdout
);
197 (void) printf("%s", lbuf
);
199 (void) fflush(stdout
);
207 (void) printf(gettext("usage: head [-q] [-v] [-n #] [-c #] [-#] "