Added gitignore entries needed to ignore derived objects generated from full build...
[bash.git] / examples / loadables / head.c
blob22cf6ab574db6b1c2807bd1175d001f5b7741c48
1 /* head - copy first part of files. */
3 /* See Makefile for compilation details. */
5 /*
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/>.
23 #include "config.h"
25 #include "bashtypes.h"
26 #include "posixstat.h"
27 #include "filecntl.h"
29 #if defined (HAVE_UNISTD_H)
30 # include <unistd.h>
31 #endif
33 #include "bashansi.h"
35 #include <stdio.h>
36 #include <errno.h>
37 #include "chartypes.h"
39 #include "builtins.h"
40 #include "shell.h"
41 #include "bashgetopt.h"
42 #include "common.h"
44 #if !defined (errno)
45 extern int errno;
46 #endif
48 static void
49 munge_list (list)
50 WORD_LIST *list;
52 WORD_LIST *l, *nl;
53 WORD_DESC *wd;
54 char *arg;
56 for (l = list; l; l = l->next)
58 arg = l->word->word;
59 if (arg[0] != '-' || arg[1] == '-' || (DIGIT(arg[1]) == 0))
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 */
71 static int
72 file_head (fp, cnt)
73 FILE *fp;
74 int cnt;
76 int ch;
78 while (cnt--)
80 while ((ch = getc (fp)) != EOF)
82 if (putchar (ch) == EOF)
84 builtin_error ("write error: %s", strerror (errno));
85 return EXECUTION_FAILURE;
87 if (ch == '\n')
88 break;
93 head_builtin (list)
94 WORD_LIST *list;
96 int nline, opt, rval;
97 WORD_LIST *l;
98 FILE *fp;
100 char *t;
102 munge_list (list); /* change -num into -n num */
104 reset_internal_getopt ();
105 nline = 10;
106 while ((opt = internal_getopt (list, "n:")) != -1)
108 switch (opt)
110 case 'n':
111 nline = atoi (list_optarg);
112 if (nline <= 0)
114 builtin_error ("bad line count: %s", list_optarg);
115 return (EX_USAGE);
117 break;
118 default:
119 builtin_usage ();
120 return (EX_USAGE);
123 list = loptend;
125 if (list == 0)
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");
131 if (fp == NULL)
133 builtin_error ("%s: %s", l->word->word, strerror (errno));
134 continue;
136 if (list->next) /* more than one file */
138 printf ("%s==> %s <==\n", opt ? "" : "\n", l->word->word);
139 opt = 0;
141 rval = file_head (fp, nline);
142 fclose (fp);
145 return (rval);
148 char *head_doc[] = {
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.",
154 (char *)NULL
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 */