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;
92 fprintf (stderr, "Try `%s --help' for more information.\n",
97 Usage: %s [OPTION] [FILE]...\n\
102 -b, --number-nonblank number nonblank output lines\n\
103 -e equivalent to -vE\n\
104 -n, --number number all output lines\n\
105 -s, --squeeze-blank never more than one single blank line\n\
106 -t equivalent to -vT\n\
108 -v, --show-nonprinting use ^ and M- notation, save for LFD and TAB\n\
109 -A, --show-all equivalent to -vET\n\
110 -E, --show-ends display $ at end of each line\n\
111 -T, --show-tabs display TAB characters as ^I\n\
112 --help display this help and exit\n\
113 --version output version information and exit\n\
115 With no FILE, or when FILE is -, read standard input.\n\
127 /* Optimal size of i/o operations of output. */
130 /* Optimal size of i/o operations of input. */
133 /* Pointer to the input buffer. */
134 unsigned char *inbuf;
136 /* Pointer to the output buffer. */
137 unsigned char *outbuf;
141 /* Index in argv to processed argument. */
144 /* Device number of the output (file or whatever). */
147 /* I-node number of the output. */
150 /* Nonzero if the output file should not be the same as any input file. */
151 int check_redirection = 1;
153 /* Nonzero if we have ever read standard input. */
154 int have_read_stdin = 0;
156 struct stat stat_buf;
158 /* Variables that are set according to the specified options. */
160 int numbers_at_empty_lines = 1;
161 int squeeze_empty_lines = 0;
162 int mark_line_ends = 0;
166 /* If non-zero, call cat, otherwise call simple_cat to do the actual work. */
169 /* If non-zero, display usage information and exit. */
170 static int show_help;
172 /* If non-zero, print the version on standard output then exit. */
173 static int show_version;
175 static struct option const long_options[] =
177 {"number-nonblank", no_argument, NULL, 'b'},
178 {"number", no_argument, NULL, 'n'},
179 {"squeeze-blank", no_argument, NULL, 's'},
180 {"show-nonprinting", no_argument, NULL, 'v'},
181 {"show-ends", no_argument, NULL, 'E'},
182 {"show-tabs", no_argument, NULL, 'T'},
183 {"show-all", no_argument, NULL, 'A'},
184 {"help", no_argument, &show_help, 1},
185 {"version", no_argument, &show_version, 1},
189 program_name = argv[0];
191 /* Parse command line options. */
193 while ((c = getopt_long (argc, argv, "benstuvAET", long_options, (int *) 0))
204 numbers_at_empty_lines = 0;
220 squeeze_empty_lines = 1;
230 /* We provide the -u feature unconditionally. */
262 printf ("%s\n", version_string);
271 /* Get device, i-node number, and optimal blocksize of output. */
273 if (fstat (output_desc, &stat_buf) < 0)
274 error (1, errno, "standard output");
276 outsize = ST_BLKSIZE (stat_buf);
277 /* Input file can be output file for non-regular files.
278 fstat on pipes returns S_IFSOCK on some systems, S_IFIFO
279 on others, so the checking should not be done for those types,
280 and to allow things like cat < /dev/tty > /dev/tty, checking
281 is not done for device files either. */
283 if (S_ISREG (stat_buf.st_mode))
285 out_dev = stat_buf.st_dev;
286 out_ino = stat_buf.st_ino;
289 check_redirection = 0;
291 /* Check if any of the input files are the same as the output file. */
301 infile = argv[argind];
303 if (infile[0] == '-' && infile[1] == 0)
310 input_desc = open (infile, O_RDONLY);
313 error (0, errno, "%s", infile);
319 if (fstat (input_desc, &stat_buf) < 0)
321 error (0, errno, "%s", infile);
325 insize = ST_BLKSIZE (stat_buf);
327 /* Compare the device and i-node numbers of this input file with
328 the corresponding values of the (output file associated with)
329 stdout, and skip this input file if they coincide. Input
330 files cannot be redirected to themselves. */
332 if (check_redirection
333 && stat_buf.st_dev == out_dev && stat_buf.st_ino == out_ino)
335 error (0, 0, "%s: input file is output file", infile);
340 /* Select which version of `cat' to use. If any options (more than -u,
341 --version, or --help) were specified, use `cat', otherwise use
346 insize = max (insize, outsize);
347 inbuf = (unsigned char *) xmalloc (insize);
349 simple_cat (inbuf, insize);
353 inbuf = (unsigned char *) xmalloc (insize + 1);
355 /* Why are (OUTSIZE - 1 + INSIZE * 4 + 13) bytes allocated for
358 A test whether output needs to be written is done when the input
359 buffer empties or when a newline appears in the input. After
360 output is written, at most (OUTSIZE - 1) bytes will remain in the
361 buffer. Now INSIZE bytes of input is read. Each input character
362 may grow by a factor of 4 (by the prepending of M-^). If all
363 characters do, and no newlines appear in this block of input, we
364 will have at most (OUTSIZE - 1 + INSIZE) bytes in the buffer. If
365 the last character in the preceding block of input was a
366 newline, a line number may be written (according to the given
367 options) as the first thing in the output buffer. (Done after the
368 new input is read, but before processing of the input begins.) A
369 line number requires seldom more than 13 positions. */
371 outbuf = (unsigned char *) xmalloc (outsize - 1 + insize * 4 + 13);
373 cat (inbuf, insize, outbuf, outsize, quote,
374 output_tabs, numbers, numbers_at_empty_lines, mark_line_ends,
375 squeeze_empty_lines);
383 if (strcmp (infile, "-") && close (input_desc) < 0)
385 error (0, errno, "%s", infile);
389 while (++argind < argc);
391 if (have_read_stdin && close (0) < 0)
392 error (1, errno, "-");
394 error (1, errno, "write error");
399 /* Plain cat. Copies the file behind `input_desc' to the file behind
403 simple_cat (buf, bufsize)
404 /* Pointer to the buffer, used by reads and writes. */
407 /* Number of characters preferably read or written by each read and write
411 /* Actual number of characters read, and therefore written. */
414 /* Loop until the end of the file. */
418 /* Read a block of input. */
420 n_read = read (input_desc, buf, bufsize);
423 error (0, errno, "%s", infile);
428 /* End of this file? */
433 /* Write this block out. */
435 if (write (output_desc, buf, n_read) != n_read)
436 error (1, errno, "write error");
440 /* Cat the file behind INPUT_DESC to the file behind OUTPUT_DESC.
441 Called if any option more than -u was specified.
443 A newline character is always put at the end of the buffer, to make
444 an explicit test for buffer end unnecessary. */
447 cat (inbuf, insize, outbuf, outsize, quote,
448 output_tabs, numbers, numbers_at_empty_lines,
449 mark_line_ends, squeeze_empty_lines)
451 /* Pointer to the beginning of the input buffer. */
452 unsigned char *inbuf;
454 /* Number of characters read in each read call. */
457 /* Pointer to the beginning of the output buffer. */
458 unsigned char *outbuf;
460 /* Number of characters written by each write call. */
463 /* Variables that have values according to the specified options. */
467 int numbers_at_empty_lines;
469 int squeeze_empty_lines;
471 /* Last character read from the input buffer. */
474 /* Pointer to the next character in the input buffer. */
477 /* Pointer to the first non-valid byte in the input buffer, i.e. the
478 current end of the buffer. */
481 /* Pointer to the position where the next character shall be written. */
482 unsigned char *bpout;
484 /* Number of characters read by the last read call. */
487 /* Determines how many consecutive newlines there have been in the
488 input. 0 newlines makes NEWLINES -1, 1 newline makes NEWLINES 1,
489 etc. Initially 0 to indicate that we are at the beginning of a
490 new line. The "state" of the procedure is determined by
492 int newlines = newlines2;
495 /* If nonzero, use the FIONREAD ioctl, as an optimization.
496 (On Ultrix, it is not supported on NFS filesystems.) */
497 int use_fionread = 1;
500 /* The inbuf pointers are initialized so that BPIN > EOB, and thereby input
501 is read immediately. */
512 /* Write if there are at least OUTSIZE bytes in OUTBUF. */
514 if (bpout - outbuf >= outsize)
516 unsigned char *wp = outbuf;
519 if (write (output_desc, wp, outsize) != outsize)
520 error (1, errno, "write error");
523 while (bpout - wp >= outsize);
525 /* Move the remaining bytes to the beginning of the
528 bcopy (wp, outbuf, bpout - wp);
529 bpout = outbuf + (bpout - wp);
532 /* Is INBUF empty? */
539 /* Is there any input to read immediately?
540 If not, we are about to wait,
541 so write all buffered output before waiting. */
544 && ioctl (input_desc, FIONREAD, &n_to_read) < 0)
546 /* Ultrix returns EOPNOTSUPP on NFS;
547 HP-UX returns ENOTTY on pipes.
548 SunOS returns EINVAL and
549 More/BSD returns ENODEV on special files
551 if (errno == EOPNOTSUPP || errno == ENOTTY
552 || errno == EINVAL || errno == ENODEV)
556 error (0, errno, "cannot do ioctl on `%s'", infile);
558 newlines2 = newlines;
565 int n_write = bpout - outbuf;
567 if (write (output_desc, outbuf, n_write) != n_write)
568 error (1, errno, "write error");
572 /* Read more input into INBUF. */
574 n_read = read (input_desc, inbuf, insize);
577 error (0, errno, "%s", infile);
579 newlines2 = newlines;
584 newlines2 = newlines;
588 /* Update the pointers and insert a sentinel at the buffer
597 /* It was a real (not a sentinel) newline. */
599 /* Was the last line empty?
600 (i.e. have two or more consecutive newlines been read?) */
604 /* Are multiple adjacent empty lines to be substituted by
605 single ditto (-s), and this was the second empty line? */
607 if (squeeze_empty_lines && newlines >= 2)
613 /* Are line numbers to be written at empty lines (-n)? */
615 if (numbers && numbers_at_empty_lines)
618 bpout = (unsigned char *) stpcpy (bpout, line_num_print);
622 /* Output a currency symbol if requested (-e). */
627 /* Output the newline. */
635 /* Are we at the beginning of a line, and line numbers are requested? */
637 if (newlines >= 0 && numbers)
640 bpout = (unsigned char *) stpcpy (bpout, line_num_print);
643 /* Here CH cannot contain a newline character. */
645 /* The loops below continue until a newline character is found,
646 which means that the buffer is empty or that a proper newline
649 /* If quoting, i.e. at least one of -v, -e, or -t specified,
650 scan for chars that need conversion. */
673 *bpout++ = ch - 128 + 64;
676 else if (ch == '\t' && output_tabs)
690 /* Not quoting, neither of -v, -e, or -t specified. */
693 if (ch == '\t' && !output_tabs)
709 /* Compute the next line number. */
714 char *endp = line_num_end;
721 while (endp >= line_num_start);
722 *--line_num_start = '1';
723 if (line_num_start < line_num_print)