]> git.ipfire.org Git - thirdparty/bash.git/blob - examples/loadables/head.c
bash-4.4 beta release
[thirdparty/bash.git] / examples / loadables / head.c
1 /* head - copy first part of files. */
2
3 /* See Makefile for compilation details. */
4
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
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>
37 #include "chartypes.h"
38
39 #include "builtins.h"
40 #include "shell.h"
41 #include "bashgetopt.h"
42 #include "common.h"
43
44 #if !defined (errno)
45 extern int errno;
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;
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 */
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 return (EXECUTION_SUCCESS);
92 }
93
94 int
95 head_builtin (list)
96 WORD_LIST *list;
97 {
98 int nline, opt, rval;
99 WORD_LIST *l;
100 FILE *fp;
101
102 char *t;
103
104 munge_list (list); /* change -num into -n num */
105
106 reset_internal_getopt ();
107 nline = 10;
108 while ((opt = internal_getopt (list, "n:")) != -1)
109 {
110 switch (opt)
111 {
112 case 'n':
113 nline = atoi (list_optarg);
114 if (nline <= 0)
115 {
116 builtin_error ("bad line count: %s", list_optarg);
117 return (EX_USAGE);
118 }
119 break;
120 default:
121 builtin_usage ();
122 return (EX_USAGE);
123 }
124 }
125 list = loptend;
126
127 if (list == 0)
128 return (file_head (stdin, nline));
129
130 for (rval = EXECUTION_SUCCESS, opt = 1, l = list; l; l = l->next)
131 {
132 fp = fopen (l->word->word, "r");
133 if (fp == NULL)
134 {
135 builtin_error ("%s: %s", l->word->word, strerror (errno));
136 continue;
137 }
138 if (list->next) /* more than one file */
139 {
140 printf ("%s==> %s <==\n", opt ? "" : "\n", l->word->word);
141 opt = 0;
142 }
143 rval = file_head (fp, nline);
144 fclose (fp);
145 }
146
147 return (rval);
148 }
149
150 char *head_doc[] = {
151 "Display lines from beginning of file.",
152 "",
153 "Copy the first N lines from the input files to the standard output.",
154 "N is supplied as an argument to the `-n' option. If N is not given,",
155 "the first ten lines are copied.",
156 (char *)NULL
157 };
158
159 struct builtin head_struct = {
160 "head", /* builtin name */
161 head_builtin, /* function implementing the builtin */
162 BUILTIN_ENABLED, /* initial flags for builtin */
163 head_doc, /* array of long documentation strings. */
164 "head [-n num] [file ...]", /* usage synopsis; becomes short_doc */
165 0 /* reserved for internal use */
166 };