Don't include version.h.
[platform/upstream/coreutils.git] / src / head.c
1 /* head -- output first part of file(s)
2    Copyright (C) 1989, 1990, 1991, 1995 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 /* Options: (see usage)
19    Reads from standard input if no files are given or when a filename of
20    ``-'' is encountered.
21    By default, filename headers are printed only if more than one file
22    is given.
23    By default, prints the first 10 lines (head -n 10).
24
25    David MacKenzie <djm@gnu.ai.mit.edu> */
26
27 #include <config.h>
28
29 #include <stdio.h>
30 #include <getopt.h>
31 #include <sys/types.h>
32 #include "system.h"
33 #include "error.h"
34
35 /* Number of lines/chars/blocks to head. */
36 #define DEFAULT_NUMBER 10
37
38 /* Size of atomic reads. */
39 #define BUFSIZE (512 * 8)
40
41 /* Number of bytes per item we are printing.
42    If 0, head in lines. */
43 static int unit_size;
44
45 /* If nonzero, print filename headers. */
46 static int print_headers;
47
48 /* When to print the filename banners. */
49 enum header_mode
50 {
51   multiple_files, always, never
52 };
53
54 int safe_read ();
55
56 /* The name this program was run with. */
57 char *program_name;
58
59 /* Have we ever read standard input?  */
60 static int have_read_stdin;
61
62 /* If nonzero, display usage information and exit.  */
63 static int show_help;
64
65 /* If nonzero, print the version on standard output then exit.  */
66 static int show_version;
67
68 static struct option const long_options[] =
69 {
70   {"bytes", required_argument, NULL, 'c'},
71   {"lines", required_argument, NULL, 'n'},
72   {"quiet", no_argument, NULL, 'q'},
73   {"silent", no_argument, NULL, 'q'},
74   {"verbose", no_argument, NULL, 'v'},
75   {"help", no_argument, &show_help, 1},
76   {"version", no_argument, &show_version, 1},
77   {NULL, 0, NULL, 0}
78 };
79
80 static void
81 usage (int status)
82 {
83   if (status != 0)
84     fprintf (stderr, _("Try `%s --help' for more information.\n"),
85              program_name);
86   else
87     {
88       printf (_("\
89 Usage: %s [OPTION]... [FILE]...\n\
90 "),
91               program_name);
92       printf (_("\
93 Print first 10 lines of each FILE to standard output.\n\
94 With more than one FILE, precede each with a header giving the file name.\n\
95 With no FILE, or when FILE is -, read standard input.\n\
96 \n\
97   -c, --bytes=SIZE         print first SIZE bytes\n\
98   -n, --lines=NUMBER       print first NUMBER lines instead of first 10\n\
99   -q, --quiet, --silent    never print headers giving file names\n\
100   -v, --verbose            always print headers giving file names\n\
101       --help               display this help and exit\n\
102       --version            output version information and exit\n\
103 \n\
104 SIZE may have a multiplier suffix: b for 512, k for 1K, m for 1 Meg.\n\
105 If -VALUE is used as first OPTION, read -c VALUE when one of\n\
106 multipliers bkm follows concatenated, else read -n VALUE.\n\
107 "));
108     }
109   exit (status);
110 }
111
112 /* Convert STR, a string of ASCII digits, into an unsigned integer.
113    Return -1 if STR does not represent a valid unsigned integer. */
114
115 static long
116 atou (const char *str)
117 {
118   int value;
119
120   for (value = 0; ISDIGIT (*str); ++str)
121     value = value * 10 + *str - '0';
122   return *str ? -1 : value;
123 }
124
125 static void
126 parse_unit (char *str)
127 {
128   int arglen = strlen (str);
129
130   if (arglen == 0)
131     return;
132
133   switch (str[arglen - 1])
134     {
135     case 'b':
136       unit_size = 512;
137       str[arglen - 1] = '\0';
138       break;
139     case 'k':
140       unit_size = 1024;
141       str[arglen - 1] = '\0';
142       break;
143     case 'm':
144       unit_size = 1048576;
145       str[arglen - 1] = '\0';
146       break;
147     }
148 }
149
150 static void
151 write_header (const char *filename)
152 {
153   static int first_file = 1;
154
155   printf ("%s==> %s <==\n", (first_file ? "" : "\n"), filename);
156   first_file = 0;
157 }
158
159 static int
160 head_bytes (const char *filename, int fd, long int bytes_to_write)
161 {
162   char buffer[BUFSIZE];
163   int bytes_read;
164
165   while (bytes_to_write)
166     {
167       bytes_read = safe_read (fd, buffer, BUFSIZE);
168       if (bytes_read < 0)
169         {
170           error (0, errno, "%s", filename);
171           return 1;
172         }
173       if (bytes_read == 0)
174         break;
175       if (bytes_read > bytes_to_write)
176         bytes_read = bytes_to_write;
177       if (fwrite (buffer, 1, bytes_read, stdout) == 0)
178         error (1, errno, _("write error"));
179       bytes_to_write -= bytes_read;
180     }
181   return 0;
182 }
183
184 static int
185 head_lines (const char *filename, int fd, long int lines_to_write)
186 {
187   char buffer[BUFSIZE];
188   int bytes_read;
189   int bytes_to_write;
190
191   while (lines_to_write)
192     {
193       bytes_read = safe_read (fd, buffer, BUFSIZE);
194       if (bytes_read < 0)
195         {
196           error (0, errno, "%s", filename);
197           return 1;
198         }
199       if (bytes_read == 0)
200         break;
201       bytes_to_write = 0;
202       while (bytes_to_write < bytes_read)
203         if (buffer[bytes_to_write++] == '\n' && --lines_to_write == 0)
204           break;
205       if (fwrite (buffer, 1, bytes_to_write, stdout) == 0)
206         error (1, errno, _("write error"));
207     }
208   return 0;
209 }
210
211 static int
212 head (const char *filename, int fd, long int number)
213 {
214   if (unit_size)
215     return head_bytes (filename, fd, number);
216   else
217     return head_lines (filename, fd, number);
218 }
219
220 static int
221 head_file (const char *filename, long int number)
222 {
223   int fd;
224
225   if (!strcmp (filename, "-"))
226     {
227       have_read_stdin = 1;
228       filename = _("standard input");
229       if (print_headers)
230         write_header (filename);
231       return head (filename, 0, number);
232     }
233   else
234     {
235       fd = open (filename, O_RDONLY);
236       if (fd >= 0)
237         {
238           int errors;
239
240           if (print_headers)
241             write_header (filename);
242           errors = head (filename, fd, number);
243           if (close (fd) == 0)
244             return errors;
245         }
246       error (0, errno, "%s", filename);
247       return 1;
248     }
249 }
250
251 void
252 main (int argc, char **argv)
253 {
254   enum header_mode header_mode = multiple_files;
255   int exit_status = 0;
256   long number = -1;             /* Number of items to print (-1 if undef.). */
257   int c;                        /* Option character. */
258
259   program_name = argv[0];
260   setlocale (LC_ALL, "");
261   bindtextdomain (PACKAGE, LOCALEDIR);
262   textdomain (PACKAGE);
263
264   have_read_stdin = 0;
265   unit_size = 0;
266   print_headers = 0;
267
268   if (argc > 1 && argv[1][0] == '-' && ISDIGIT (argv[1][1]))
269     {
270       /* Old option syntax; a dash, one or more digits, and one or
271          more option letters.  Move past the number. */
272       for (number = 0, ++argv[1]; ISDIGIT (*argv[1]); ++argv[1])
273         number = number * 10 + *argv[1] - '0';
274       /* Parse any appended option letters. */
275       while (*argv[1])
276         {
277           switch (*argv[1])
278             {
279             case 'b':
280               unit_size = 512;
281               break;
282
283             case 'c':
284               unit_size = 1;
285               break;
286
287             case 'k':
288               unit_size = 1024;
289               break;
290
291             case 'l':
292               unit_size = 0;
293               break;
294
295             case 'm':
296               unit_size = 1048576;
297               break;
298
299             case 'q':
300               header_mode = never;
301               break;
302
303             case 'v':
304               header_mode = always;
305               break;
306
307             default:
308               error (0, 0, _("unrecognized option `-%c'"), *argv[1]);
309               usage (1);
310             }
311           ++argv[1];
312         }
313       /* Make the options we just parsed invisible to getopt. */
314       argv[1] = argv[0];
315       argv++;
316       argc--;
317     }
318
319   while ((c = getopt_long (argc, argv, "c:n:qv", long_options, (int *) 0))
320          != EOF)
321     {
322       switch (c)
323         {
324         case 0:
325           break;
326
327         case 'c':
328           unit_size = 1;
329           parse_unit (optarg);
330           goto getnum;
331         case 'n':
332           unit_size = 0;
333         getnum:
334           /* FIXME: use xstrtoul instead.  */
335           number = atou (optarg);
336           if (number == -1)
337             error (1, 0, _("invalid number `%s'"), optarg);
338           break;
339
340         case 'q':
341           header_mode = never;
342           break;
343
344         case 'v':
345           header_mode = always;
346           break;
347
348         default:
349           usage (1);
350         }
351     }
352
353   if (show_version)
354     {
355       printf ("head - %s\n", PACKAGE_VERSION);
356       exit (0);
357     }
358
359   if (show_help)
360     usage (0);
361
362   if (number == -1)
363     number = DEFAULT_NUMBER;
364
365   if (unit_size > 1)
366     number *= unit_size;
367
368   if (header_mode == always
369       || (header_mode == multiple_files && optind < argc - 1))
370     print_headers = 1;
371
372   if (optind == argc)
373     exit_status |= head_file ("-", number);
374
375   for (; optind < argc; ++optind)
376     exit_status |= head_file (argv[optind], number);
377
378   if (have_read_stdin && close (0) < 0)
379     error (1, errno, "-");
380   if (fclose (stdout) == EOF)
381     error (1, errno, _("write error"));
382
383   exit (exit_status);
384 }