1 /* head - copy first part of files. */
3 /* See Makefile for compilation details. */
6 Copyright (C) 1999-2009 Free Software Foundation, Inc.
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.
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.
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/>.
25 #include "bashtypes.h"
26 #include "posixstat.h"
29 #if defined (HAVE_UNISTD_H)
37 #include "chartypes.h"
41 #include "bashgetopt.h"
56 for (l
= list
; l
; l
= l
->next
)
59 if (arg
[0] != '-' || arg
[1] == '-' || (DIGIT(arg
[1]) == 0))
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';
67 l
= nl
; /* skip over new argument */
80 while ((ch
= getc (fp
)) != EOF
)
82 if (putchar (ch
) == EOF
)
84 builtin_error ("write error: %s", strerror (errno
));
85 return EXECUTION_FAILURE
;
102 munge_list (list
); /* change -num into -n num */
104 reset_internal_getopt ();
106 while ((opt
= internal_getopt (list
, "n:")) != -1)
111 nline
= atoi (list_optarg
);
114 builtin_error ("bad line count: %s", list_optarg
);
126 return (file_head (stdin
, nline
));
128 for (rval
= EXECUTION_SUCCESS
, opt
= 1, l
= list
; l
; l
= l
->next
)
130 fp
= fopen (l
->word
->word
, "r");
133 builtin_error ("%s: %s", l
->word
->word
, strerror (errno
));
136 if (list
->next
) /* more than one file */
138 printf ("%s==> %s <==\n", opt
? "" : "\n", l
->word
->word
);
141 rval
= file_head (fp
, nline
);
149 "Display lines from beginning of file.",
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.",
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 */