1 /* cat -- concatenate files and print on the standard output.
2 Copyright (C) 1988, 1990, 1991 Free Software Foundation, Inc.
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)
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Differences from the Unix cat:
19 * Always unbuffered, -u is ignored.
20 * 100 times faster with -v -u.
21 * 20 times faster with -v.
23 By tege@sics.se, Torbjorn Granlund, advised by rms, Richard Stallman. */
26 #if defined (CONFIG_BROKETS)
27 /* We use <config.h> instead of "config.h" so that a compilation
28 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
29 (which it would do because it found this file in $srcdir). */
38 #include <sys/types.h>
40 #include <sys/ioctl.h>
45 #define max(h,i) ((h) > (i) ? (h) : (i))
52 static void next_line_num ();
53 static void simple_cat ();
55 /* Name under which this program was invoked. */
58 /* Name of input file. May be "-". */
61 /* Descriptor on which input file is open. */
62 static int input_desc;
64 /* Descriptor on which output file is open. Always is 1. */
65 static int output_desc;
67 /* Buffer for line numbers. */
68 static char line_buf[13] =
69 {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '0', '\t', '\0'};
71 /* Position in `line_buf' where printing starts. This will not change
72 unless the number of lines are more than 999999. */
73 static char *line_num_print = line_buf + 5;
75 /* Position of the first digit in `line_buf'. */
76 static char *line_num_start = line_buf + 10;
78 /* Position of the last digit in `line_buf'. */
79 static char *line_num_end = line_buf + 10;
81 /* Preserves the `cat' function's local `newlines' between invocations. */
82 static int newlines2 = 0;
84 /* Count of non-fatal error conditions. */
85 static int exit_stat = 0;
91 Usage: %s [-benstuvAET] [--number] [--number-nonblank] [--squeeze-blank]\n\
92 [--show-nonprinting] [--show-ends] [--show-tabs] [--show-all]\n\
93 [--help] [--version] [file...]\n",
105 /* Optimal size of i/o operations of output. */
108 /* Optimal size of i/o operations of input. */
111 /* Pointer to the input buffer. */
112 unsigned char *inbuf;
114 /* Pointer to the output buffer. */
115 unsigned char *outbuf;
119 /* Index in argv to processed argument. */
122 /* Device number of the output (file or whatever). */
125 /* I-node number of the output. */
128 /* Nonzero if the output file should not be the same as any input file. */
129 int check_redirection = 1;
131 /* Nonzero if we have ever read standard input. */
132 int have_read_stdin = 0;
134 struct stat stat_buf;
136 /* Variables that are set according to the specified options. */
138 int numbers_at_empty_lines = 1;
139 int squeeze_empty_lines = 0;
140 int mark_line_ends = 0;
144 /* If non-zero, call cat, otherwise call simple_cat to do the actual work. */
147 /* If non-zero, display usage information and exit. */
148 static int show_help;
150 /* If non-zero, print the version on standard output then exit. */
151 static int show_version;
153 static struct option const long_options[] =
155 {"number-nonblank", no_argument, NULL, 'b'},
156 {"number", no_argument, NULL, 'n'},
157 {"squeeze-blank", no_argument, NULL, 's'},
158 {"show-nonprinting", no_argument, NULL, 'v'},
159 {"show-ends", no_argument, NULL, 'E'},
160 {"show-tabs", no_argument, NULL, 'T'},
161 {"show-all", no_argument, NULL, 'A'},
162 {"help", no_argument, &show_help, 1},
163 {"version", no_argument, &show_version, 1},
167 program_name = argv[0];
169 /* Parse command line options. */
171 while ((c = getopt_long (argc, argv, "benstuvAET", long_options, (int *) 0))
182 numbers_at_empty_lines = 0;
198 squeeze_empty_lines = 1;
208 /* We provide the -u feature unconditionally. */
240 printf ("%s\n", version_string);
249 /* Get device, i-node number, and optimal blocksize of output. */
251 if (fstat (output_desc, &stat_buf) < 0)
252 error (1, errno, "standard output");
254 outsize = ST_BLKSIZE (stat_buf);
255 /* Input file can be output file for non-regular files.
256 fstat on pipes returns S_IFSOCK on some systems, S_IFIFO
257 on others, so the checking should not be done for those types,
258 and to allow things like cat < /dev/tty > /dev/tty, checking
259 is not done for device files either. */
261 if (S_ISREG (stat_buf.st_mode))
263 out_dev = stat_buf.st_dev;
264 out_ino = stat_buf.st_ino;
267 check_redirection = 0;
269 /* Check if any of the input files are the same as the output file. */
279 infile = argv[argind];
281 if (infile[0] == '-' && infile[1] == 0)
288 input_desc = open (infile, O_RDONLY);
291 error (0, errno, "%s", infile);
297 if (fstat (input_desc, &stat_buf) < 0)
299 error (0, errno, "%s", infile);
303 insize = ST_BLKSIZE (stat_buf);
305 /* Compare the device and i-node numbers of this input file with
306 the corresponding values of the (output file associated with)
307 stdout, and skip this input file if they coincide. Input
308 files cannot be redirected to themselves. */
310 if (check_redirection
311 && stat_buf.st_dev == out_dev && stat_buf.st_ino == out_ino)
313 error (0, 0, "%s: input file is output file", infile);
318 /* Select which version of `cat' to use. If any options (more than -u,
319 --version, or --help) were specified, use `cat', otherwise use
324 insize = max (insize, outsize);
325 inbuf = (unsigned char *) xmalloc (insize);
327 simple_cat (inbuf, insize);
331 inbuf = (unsigned char *) xmalloc (insize + 1);
333 /* Why are (OUTSIZE - 1 + INSIZE * 4 + 13) bytes allocated for
336 A test whether output needs to be written is done when the input
337 buffer empties or when a newline appears in the input. After
338 output is written, at most (OUTSIZE - 1) bytes will remain in the
339 buffer. Now INSIZE bytes of input is read. Each input character
340 may grow by a factor of 4 (by the prepending of M-^). If all
341 characters do, and no newlines appear in this block of input, we
342 will have at most (OUTSIZE - 1 + INSIZE) bytes in the buffer. If
343 the last character in the preceding block of input was a
344 newline, a line number may be written (according to the given
345 options) as the first thing in the output buffer. (Done after the
346 new input is read, but before processing of the input begins.) A
347 line number requires seldom more than 13 positions. */
349 outbuf = (unsigned char *) xmalloc (outsize - 1 + insize * 4 + 13);
351 cat (inbuf, insize, outbuf, outsize, quote,
352 output_tabs, numbers, numbers_at_empty_lines, mark_line_ends,
353 squeeze_empty_lines);
361 if (strcmp (infile, "-") && close (input_desc) < 0)
363 error (0, errno, "%s", infile);
367 while (++argind < argc);
369 if (have_read_stdin && close (0) < 0)
370 error (1, errno, "-");
372 error (1, errno, "write error");
377 /* Plain cat. Copies the file behind `input_desc' to the file behind
381 simple_cat (buf, bufsize)
382 /* Pointer to the buffer, used by reads and writes. */
385 /* Number of characters preferably read or written by each read and write
389 /* Actual number of characters read, and therefore written. */
392 /* Loop until the end of the file. */
396 /* Read a block of input. */
398 n_read = read (input_desc, buf, bufsize);
401 error (0, errno, "%s", infile);
406 /* End of this file? */
411 /* Write this block out. */
413 if (write (output_desc, buf, n_read) != n_read)
414 error (1, errno, "write error");
418 /* Cat the file behind INPUT_DESC to the file behind OUTPUT_DESC.
419 Called if any option more than -u was specified.
421 A newline character is always put at the end of the buffer, to make
422 an explicit test for buffer end unnecessary. */
425 cat (inbuf, insize, outbuf, outsize, quote,
426 output_tabs, numbers, numbers_at_empty_lines,
427 mark_line_ends, squeeze_empty_lines)
429 /* Pointer to the beginning of the input buffer. */
430 unsigned char *inbuf;
432 /* Number of characters read in each read call. */
435 /* Pointer to the beginning of the output buffer. */
436 unsigned char *outbuf;
438 /* Number of characters written by each write call. */
441 /* Variables that have values according to the specified options. */
445 int numbers_at_empty_lines;
447 int squeeze_empty_lines;
449 /* Last character read from the input buffer. */
452 /* Pointer to the next character in the input buffer. */
455 /* Pointer to the first non-valid byte in the input buffer, i.e. the
456 current end of the buffer. */
459 /* Pointer to the position where the next character shall be written. */
460 unsigned char *bpout;
462 /* Number of characters read by the last read call. */
465 /* Determines how many consecutive newlines there have been in the
466 input. 0 newlines makes NEWLINES -1, 1 newline makes NEWLINES 1,
467 etc. Initially 0 to indicate that we are at the beginning of a
468 new line. The "state" of the procedure is determined by
470 int newlines = newlines2;
473 /* If nonzero, use the FIONREAD ioctl, as an optimization.
474 (On Ultrix, it is not supported on NFS filesystems.) */
475 int use_fionread = 1;
478 /* The inbuf pointers are initialized so that BPIN > EOB, and thereby input
479 is read immediately. */
490 /* Write if there are at least OUTSIZE bytes in OUTBUF. */
492 if (bpout - outbuf >= outsize)
494 unsigned char *wp = outbuf;
497 if (write (output_desc, wp, outsize) != outsize)
498 error (1, errno, "write error");
501 while (bpout - wp >= outsize);
503 /* Move the remaining bytes to the beginning of the
506 bcopy (wp, outbuf, bpout - wp);
507 bpout = outbuf + (bpout - wp);
510 /* Is INBUF empty? */
517 /* Is there any input to read immediately?
518 If not, we are about to wait,
519 so write all buffered output before waiting. */
522 && ioctl (input_desc, FIONREAD, &n_to_read) < 0)
524 /* Ultrix returns EOPNOTSUPP on NFS;
525 HP-UX returns ENOTTY on pipes.
526 SunOS returns EINVAL and
527 More/BSD returns ENODEV on special files
529 if (errno == EOPNOTSUPP || errno == ENOTTY
530 || errno == EINVAL || errno == ENODEV)
534 error (0, errno, "cannot do ioctl on `%s'", infile);
536 newlines2 = newlines;
543 int n_write = bpout - outbuf;
545 if (write (output_desc, outbuf, n_write) != n_write)
546 error (1, errno, "write error");
550 /* Read more input into INBUF. */
552 n_read = read (input_desc, inbuf, insize);
555 error (0, errno, "%s", infile);
557 newlines2 = newlines;
562 newlines2 = newlines;
566 /* Update the pointers and insert a sentinel at the buffer
575 /* It was a real (not a sentinel) newline. */
577 /* Was the last line empty?
578 (i.e. have two or more consecutive newlines been read?) */
582 /* Are multiple adjacent empty lines to be substituted by
583 single ditto (-s), and this was the second empty line? */
585 if (squeeze_empty_lines && newlines >= 2)
591 /* Are line numbers to be written at empty lines (-n)? */
593 if (numbers && numbers_at_empty_lines)
596 bpout = (unsigned char *) stpcpy (bpout, line_num_print);
600 /* Output a currency symbol if requested (-e). */
605 /* Output the newline. */
613 /* Are we at the beginning of a line, and line numbers are requested? */
615 if (newlines >= 0 && numbers)
618 bpout = (unsigned char *) stpcpy (bpout, line_num_print);
621 /* Here CH cannot contain a newline character. */
623 /* The loops below continue until a newline character is found,
624 which means that the buffer is empty or that a proper newline
627 /* If quoting, i.e. at least one of -v, -e, or -t specified,
628 scan for chars that need conversion. */
651 *bpout++ = ch - 128 + 64;
654 else if (ch == '\t' && output_tabs)
668 /* Not quoting, neither of -v, -e, or -t specified. */
671 if (ch == '\t' && !output_tabs)
687 /* Compute the next line number. */
692 char *endp = line_num_end;
699 while (endp >= line_num_start);
700 *--line_num_start = '1';
701 if (line_num_start < line_num_print)