Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1 | /* head - copy first part of files. */ |
| 2 | |
| 3 | /* See Makefile for compilation details. */ |
| 4 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 5 | /* |
| 6 | Copyright (C) 1999-2009 Free Software Foundation, Inc. |
| 7 | |
| 8 | This file is part of GNU Bash. |
| 9 | Bash is free software: you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License as published by |
| 11 | the Free Software Foundation, either version 3 of the License, or |
| 12 | (at your option) any later version. |
| 13 | |
| 14 | Bash is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
| 20 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
| 21 | */ |
| 22 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 23 | #include "config.h" |
| 24 | |
| 25 | #include "bashtypes.h" |
| 26 | #include "posixstat.h" |
| 27 | #include "filecntl.h" |
| 28 | |
| 29 | #if defined (HAVE_UNISTD_H) |
| 30 | # include <unistd.h> |
| 31 | #endif |
| 32 | |
| 33 | #include "bashansi.h" |
| 34 | |
| 35 | #include <stdio.h> |
| 36 | #include <errno.h> |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 37 | #include "chartypes.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 38 | |
| 39 | #include "builtins.h" |
| 40 | #include "shell.h" |
| 41 | #include "bashgetopt.h" |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 42 | #include "common.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 43 | |
| 44 | #if !defined (errno) |
Ricardo Cerqueira | a02fbff | 2013-07-25 22:35:34 +0100 | [diff] [blame] | 45 | #include <errno.h> |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 46 | #endif |
| 47 | |
| 48 | static void |
| 49 | munge_list (list) |
| 50 | WORD_LIST *list; |
| 51 | { |
| 52 | WORD_LIST *l, *nl; |
| 53 | WORD_DESC *wd; |
| 54 | char *arg; |
| 55 | |
| 56 | for (l = list; l; l = l->next) |
| 57 | { |
| 58 | arg = l->word->word; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 59 | if (arg[0] != '-' || arg[1] == '-' || (DIGIT(arg[1]) == 0)) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 60 | return; |
| 61 | /* We have -[0-9]* */ |
| 62 | wd = make_bare_word (arg+1); |
| 63 | nl = make_word_list (wd, l->next); |
| 64 | l->word->word[1] = 'n'; |
| 65 | l->word->word[2] = '\0'; |
| 66 | l->next = nl; |
| 67 | l = nl; /* skip over new argument */ |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | static int |
| 72 | file_head (fp, cnt) |
| 73 | FILE *fp; |
| 74 | int cnt; |
| 75 | { |
| 76 | int ch; |
| 77 | |
| 78 | while (cnt--) |
| 79 | { |
| 80 | while ((ch = getc (fp)) != EOF) |
| 81 | { |
| 82 | if (putchar (ch) == EOF) |
| 83 | { |
| 84 | builtin_error ("write error: %s", strerror (errno)); |
| 85 | return EXECUTION_FAILURE; |
| 86 | } |
| 87 | if (ch == '\n') |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | head_builtin (list) |
| 94 | WORD_LIST *list; |
| 95 | { |
| 96 | int nline, opt, rval; |
| 97 | WORD_LIST *l; |
| 98 | FILE *fp; |
| 99 | |
| 100 | char *t; |
| 101 | |
| 102 | munge_list (list); /* change -num into -n num */ |
| 103 | |
| 104 | reset_internal_getopt (); |
| 105 | nline = 10; |
| 106 | while ((opt = internal_getopt (list, "n:")) != -1) |
| 107 | { |
| 108 | switch (opt) |
| 109 | { |
| 110 | case 'n': |
| 111 | nline = atoi (list_optarg); |
| 112 | if (nline <= 0) |
| 113 | { |
| 114 | builtin_error ("bad line count: %s", list_optarg); |
| 115 | return (EX_USAGE); |
| 116 | } |
| 117 | break; |
| 118 | default: |
| 119 | builtin_usage (); |
| 120 | return (EX_USAGE); |
| 121 | } |
| 122 | } |
| 123 | list = loptend; |
| 124 | |
| 125 | if (list == 0) |
| 126 | return (file_head (stdin, nline)); |
| 127 | |
| 128 | for (rval = EXECUTION_SUCCESS, opt = 1, l = list; l; l = l->next) |
| 129 | { |
| 130 | fp = fopen (l->word->word, "r"); |
| 131 | if (fp == NULL) |
| 132 | { |
| 133 | builtin_error ("%s: %s", l->word->word, strerror (errno)); |
| 134 | continue; |
| 135 | } |
| 136 | if (list->next) /* more than one file */ |
| 137 | { |
| 138 | printf ("%s==> %s <==\n", opt ? "" : "\n", l->word->word); |
| 139 | opt = 0; |
| 140 | } |
| 141 | rval = file_head (fp, nline); |
| 142 | fclose (fp); |
| 143 | } |
| 144 | |
| 145 | return (rval); |
| 146 | } |
| 147 | |
| 148 | char *head_doc[] = { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 149 | "Display lines from beginning of file.", |
| 150 | "", |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 151 | "Copy the first N lines from the input files to the standard output.", |
| 152 | "N is supplied as an argument to the `-n' option. If N is not given,", |
| 153 | "the first ten lines are copied.", |
| 154 | (char *)NULL |
| 155 | }; |
| 156 | |
| 157 | struct builtin head_struct = { |
| 158 | "head", /* builtin name */ |
| 159 | head_builtin, /* function implementing the builtin */ |
| 160 | BUILTIN_ENABLED, /* initial flags for builtin */ |
| 161 | head_doc, /* array of long documentation strings. */ |
| 162 | "head [-n num] [file ...]", /* usage synopsis; becomes short_doc */ |
| 163 | 0 /* reserved for internal use */ |
| 164 | }; |