1 /* head - copy first part of files. */
3 /* See Makefile for compilation details. */
11 #if defined (HAVE_UNISTD_H)
19 #include "chartypes.h"
23 #include "bashgetopt.h"
37 for (l = list; l; l = l->next)
40 if (arg[0] != '-' || arg[1] == '-' || (DIGIT(arg[1]) == 0))
43 wd = make_bare_word (arg+1);
44 nl = make_word_list (wd, l->next);
45 l->word->word[1] = 'n';
46 l->word->word[2] = '\0';
48 l = nl; /* skip over new argument */
61 while ((ch = getc (fp)) != EOF)
63 if (putchar (ch) == EOF)
65 builtin_error ("write error: %s", strerror (errno));
66 return EXECUTION_FAILURE;
83 munge_list (list); /* change -num into -n num */
85 reset_internal_getopt ();
87 while ((opt = internal_getopt (list, "n:")) != -1)
92 nline = atoi (list_optarg);
95 builtin_error ("bad line count: %s", list_optarg);
107 return (file_head (stdin, nline));
109 for (rval = EXECUTION_SUCCESS, opt = 1, l = list; l; l = l->next)
111 fp = fopen (l->word->word, "r");
114 builtin_error ("%s: %s", l->word->word, strerror (errno));
117 if (list->next) /* more than one file */
119 printf ("%s==> %s <==\n", opt ? "" : "\n", l->word->word);
122 rval = file_head (fp, nline);
130 "Copy the first N lines from the input files to the standard output.",
131 "N is supplied as an argument to the `-n' option. If N is not given,",
132 "the first ten lines are copied.",
136 struct builtin head_struct = {
137 "head", /* builtin name */
138 head_builtin, /* function implementing the builtin */
139 BUILTIN_ENABLED, /* initial flags for builtin */
140 head_doc, /* array of long documentation strings. */
141 "head [-n num] [file ...]", /* usage synopsis; becomes short_doc */
142 0 /* reserved for internal use */