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 */ 39bd6433ebSRobert Mustacchi /* 40*4e6070e8SRobert Mustacchi * Copyright (c) 2014, Joyent, Inc. All rights reserved. 41bd6433ebSRobert 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 53bd6433ebSRobert Mustacchi static void copyout(off_t, int); 547c478bd9Sstevel@tonic-gate static void Usage(); 55*4e6070e8SRobert Mustacchi static FILE *input; 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate /* 59bd6433ebSRobert Mustacchi * head - give the first few lines of a stream or of each of a set of files. 60bd6433ebSRobert Mustacchi * Optionally shows a specific number of bytes instead. 617c478bd9Sstevel@tonic-gate */ 627c478bd9Sstevel@tonic-gate int 637c478bd9Sstevel@tonic-gate main(int argc, char **argv) 647c478bd9Sstevel@tonic-gate { 657c478bd9Sstevel@tonic-gate int fileCount; 667c478bd9Sstevel@tonic-gate int around = 0; 677c478bd9Sstevel@tonic-gate int i; 687c478bd9Sstevel@tonic-gate int opt; 697c478bd9Sstevel@tonic-gate off_t linecnt = DEF_LINE_COUNT; 70bd6433ebSRobert Mustacchi int isline = 1; 717c478bd9Sstevel@tonic-gate int error = 0; 72bd6433ebSRobert Mustacchi int quiet = 0; 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 757c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 767c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 777c478bd9Sstevel@tonic-gate #endif 787c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate /* check for non-standard "-line-count" option */ 817c478bd9Sstevel@tonic-gate for (i = 1; i < argc; i++) { 827c478bd9Sstevel@tonic-gate if (strcmp(argv[i], "--") == 0) 837c478bd9Sstevel@tonic-gate break; 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate if ((argv[i][0] == '-') && isdigit(argv[i][1])) { 867c478bd9Sstevel@tonic-gate if (strlen(&argv[i][1]) != 877c478bd9Sstevel@tonic-gate strspn(&argv[i][1], "0123456789")) { 887c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 897c478bd9Sstevel@tonic-gate "%s: Badly formed number\n"), argv[0]); 907c478bd9Sstevel@tonic-gate Usage(); 917c478bd9Sstevel@tonic-gate } 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate linecnt = (off_t)strtoll(&argv[i][1], (char **)NULL, 947c478bd9Sstevel@tonic-gate 10); 957c478bd9Sstevel@tonic-gate while (i < argc) { 967c478bd9Sstevel@tonic-gate argv[i] = argv[i + 1]; 977c478bd9Sstevel@tonic-gate i++; 987c478bd9Sstevel@tonic-gate } 997c478bd9Sstevel@tonic-gate argc--; 1007c478bd9Sstevel@tonic-gate } 1017c478bd9Sstevel@tonic-gate } 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate /* get options */ 104bd6433ebSRobert Mustacchi while ((opt = getopt(argc, argv, "qvn:c:")) != EOF) { 1057c478bd9Sstevel@tonic-gate switch (opt) { 1067c478bd9Sstevel@tonic-gate case 'n': 107bd6433ebSRobert Mustacchi case 'c': 1087c478bd9Sstevel@tonic-gate if ((strcmp(optarg, "--") == 0) || (optind > argc)) { 1097c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 110bd6433ebSRobert Mustacchi "%s: Missing -%c argument\n"), argv[0], 111bd6433ebSRobert Mustacchi optopt); 1127c478bd9Sstevel@tonic-gate Usage(); 1137c478bd9Sstevel@tonic-gate } 1147c478bd9Sstevel@tonic-gate linecnt = (off_t)strtoll(optarg, (char **)NULL, 10); 1157c478bd9Sstevel@tonic-gate if (linecnt <= 0) { 1167c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 117bd6433ebSRobert Mustacchi "%s: Invalid \"-%c %s\" option\n"), 118bd6433ebSRobert Mustacchi argv[0], optopt, optarg); 1197c478bd9Sstevel@tonic-gate Usage(); 1207c478bd9Sstevel@tonic-gate } 121bd6433ebSRobert Mustacchi isline = optopt != 'c'; 1227c478bd9Sstevel@tonic-gate break; 123bd6433ebSRobert Mustacchi case 'q': 124bd6433ebSRobert Mustacchi quiet = 1; 125bd6433ebSRobert Mustacchi break; 126bd6433ebSRobert Mustacchi case 'v': 127bd6433ebSRobert Mustacchi quiet = 0; 128bd6433ebSRobert Mustacchi break; 1297c478bd9Sstevel@tonic-gate default: 1307c478bd9Sstevel@tonic-gate Usage(); 1317c478bd9Sstevel@tonic-gate } 1327c478bd9Sstevel@tonic-gate } 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate fileCount = argc - optind; 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate do { 1377c478bd9Sstevel@tonic-gate if ((argv[optind] == NULL) && around) 1387c478bd9Sstevel@tonic-gate break; 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate if (argv[optind] != NULL) { 141*4e6070e8SRobert Mustacchi if (input != NULL) 142*4e6070e8SRobert Mustacchi (void) fclose(input); 143*4e6070e8SRobert Mustacchi if ((input = fopen(argv[optind], "r")) == NULL) { 1447c478bd9Sstevel@tonic-gate perror(argv[optind]); 1457c478bd9Sstevel@tonic-gate error = 1; 1467c478bd9Sstevel@tonic-gate optind++; 1477c478bd9Sstevel@tonic-gate continue; 1487c478bd9Sstevel@tonic-gate } 149*4e6070e8SRobert Mustacchi } else { 150*4e6070e8SRobert Mustacchi input = stdin; 1517c478bd9Sstevel@tonic-gate } 1527c478bd9Sstevel@tonic-gate 153bd6433ebSRobert Mustacchi if (quiet == 0) { 1547c478bd9Sstevel@tonic-gate if (around) 1557c478bd9Sstevel@tonic-gate (void) putchar('\n'); 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate if (fileCount > 1) 1587c478bd9Sstevel@tonic-gate (void) printf("==> %s <==\n", argv[optind]); 159bd6433ebSRobert Mustacchi } 1607c478bd9Sstevel@tonic-gate 1617c478bd9Sstevel@tonic-gate if (argv[optind] != NULL) 1627c478bd9Sstevel@tonic-gate optind++; 1637c478bd9Sstevel@tonic-gate 164bd6433ebSRobert Mustacchi copyout(linecnt, isline); 1657c478bd9Sstevel@tonic-gate (void) fflush(stdout); 1667c478bd9Sstevel@tonic-gate around++; 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate } while (argv[optind] != NULL); 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate return (error); 1717c478bd9Sstevel@tonic-gate } 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate static void 174bd6433ebSRobert Mustacchi copyout(off_t cnt, int isline) 1757c478bd9Sstevel@tonic-gate { 1767c478bd9Sstevel@tonic-gate char lbuf[BUFSIZ]; 1777c478bd9Sstevel@tonic-gate size_t len; 1787c478bd9Sstevel@tonic-gate 179*4e6070e8SRobert Mustacchi while (cnt > 0 && fgets(lbuf, sizeof (lbuf), input) != 0) { 180bd6433ebSRobert Mustacchi len = strlen(lbuf); 181bd6433ebSRobert Mustacchi if (isline) { 1827c478bd9Sstevel@tonic-gate (void) printf("%s", lbuf); 183bd6433ebSRobert Mustacchi /* 184bd6433ebSRobert Mustacchi * only count as a line if buffer read ends with newline 185bd6433ebSRobert Mustacchi */ 186bd6433ebSRobert Mustacchi if (len > 0) { 1877c478bd9Sstevel@tonic-gate if (lbuf[len - 1] == '\n') { 1887c478bd9Sstevel@tonic-gate (void) fflush(stdout); 1897c478bd9Sstevel@tonic-gate cnt--; 1907c478bd9Sstevel@tonic-gate } 1917c478bd9Sstevel@tonic-gate } 192bd6433ebSRobert Mustacchi } else { 193bd6433ebSRobert Mustacchi if (len > cnt) { 194bd6433ebSRobert Mustacchi lbuf[cnt] = '\0'; 195bd6433ebSRobert Mustacchi len = cnt; 196bd6433ebSRobert Mustacchi } 197bd6433ebSRobert Mustacchi (void) printf("%s", lbuf); 198bd6433ebSRobert Mustacchi cnt -= len; 199bd6433ebSRobert Mustacchi (void) fflush(stdout); 200bd6433ebSRobert Mustacchi } 2017c478bd9Sstevel@tonic-gate } 2027c478bd9Sstevel@tonic-gate } 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate static void 2057c478bd9Sstevel@tonic-gate Usage() 2067c478bd9Sstevel@tonic-gate { 207bd6433ebSRobert Mustacchi (void) printf(gettext("usage: head [-q] [-v] [-n #] [-c #] [-#] " 208bd6433ebSRobert Mustacchi "[filename...]\n")); 2097c478bd9Sstevel@tonic-gate exit(1); 2107c478bd9Sstevel@tonic-gate } 211