1 /* cat -- concatenate files and print on the standard output.
2 Copyright (C) 1988, 1990, 1991, 1995 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 Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Differences from the Unix cat:
19 * Always unbuffered, -u is ignored.
20 * Usually much faster than other versions of cat, the difference
21 is especially apparent when using the -v option.
23 By tege@sics.se, Torbjorn Granlund, advised by rms, Richard Stallman. */
29 #include <sys/types.h>
31 #include <sys/ioctl.h>
37 /* Undefine, to avoid warning about redefinition on some systems. */
39 #define max(h,i) ((h) > (i) ? (h) : (i))
46 /* Name under which this program was invoked. */
49 /* Name of input file. May be "-". */
52 /* Descriptor on which input file is open. */
53 static int input_desc;
55 /* Descriptor on which output file is open. Always is 1. */
56 static int output_desc;
58 /* Buffer for line numbers. */
59 static char line_buf[13] =
60 {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '0', '\t', '\0'};
62 /* Position in `line_buf' where printing starts. This will not change
63 unless the number of lines is larger than 999999. */
64 static char *line_num_print = line_buf + 5;
66 /* Position of the first digit in `line_buf'. */
67 static char *line_num_start = line_buf + 10;
69 /* Position of the last digit in `line_buf'. */
70 static char *line_num_end = line_buf + 10;
72 /* Preserves the `cat' function's local `newlines' between invocations. */
73 static int newlines2 = 0;
75 /* Count of non-fatal error conditions. */
76 static int exit_stat = 0;
83 fprintf (stderr, _("Try `%s --help' for more information.\n"),
88 Usage: %s [OPTION] [FILE]...\n\
92 Concatenate FILE(s), or standard input, to standard output.\n\
94 -b, --number-nonblank number nonblank output lines\n\
95 -e equivalent to -vE\n\
96 -n, --number number all output lines\n\
97 -s, --squeeze-blank never more than one single blank line\n\
98 -t equivalent to -vT\n\
100 -v, --show-nonprinting use ^ and M- notation, save for LFD and TAB\n\
101 -A, --show-all equivalent to -vET\n\
102 -E, --show-ends display $ at end of each line\n\
103 -T, --show-tabs display TAB characters as ^I\n\
104 --help display this help and exit\n\
105 --version output version information and exit\n\
107 With no FILE, or when FILE is -, read standard input.\n\
113 /* Compute the next line number. */
118 char *endp = line_num_end;
125 while (endp >= line_num_start);
126 *--line_num_start = '1';
127 if (line_num_start < line_num_print)
131 /* Plain cat. Copies the file behind `input_desc' to the file behind
136 /* Pointer to the buffer, used by reads and writes. */
139 /* Number of characters preferably read or written by each read and write
143 /* Actual number of characters read, and therefore written. */
146 /* Loop until the end of the file. */
150 /* Read a block of input. */
152 n_read = safe_read (input_desc, buf, bufsize);
155 error (0, errno, "%s", infile);
160 /* End of this file? */
165 /* Write this block out. */
167 if (full_write (output_desc, buf, n_read) < 0)
168 error (1, errno, _("write error"));
172 /* Cat the file behind INPUT_DESC to the file behind OUTPUT_DESC.
173 Called if any option more than -u was specified.
175 A newline character is always put at the end of the buffer, to make
176 an explicit test for buffer end unnecessary. */
180 /* Pointer to the beginning of the input buffer. */
181 unsigned char *inbuf,
183 /* Number of characters read in each read call. */
186 /* Pointer to the beginning of the output buffer. */
187 unsigned char *outbuf,
189 /* Number of characters written by each write call. */
192 /* Variables that have values according to the specified options. */
196 int numbers_at_empty_lines,
198 int squeeze_empty_lines)
200 /* Last character read from the input buffer. */
203 /* Pointer to the next character in the input buffer. */
206 /* Pointer to the first non-valid byte in the input buffer, i.e. the
207 current end of the buffer. */
210 /* Pointer to the position where the next character shall be written. */
211 unsigned char *bpout;
213 /* Number of characters read by the last read call. */
216 /* Determines how many consecutive newlines there have been in the
217 input. 0 newlines makes NEWLINES -1, 1 newline makes NEWLINES 1,
218 etc. Initially 0 to indicate that we are at the beginning of a
219 new line. The "state" of the procedure is determined by
221 int newlines = newlines2;
224 /* If nonzero, use the FIONREAD ioctl, as an optimization.
225 (On Ultrix, it is not supported on NFS filesystems.) */
226 int use_fionread = 1;
229 /* The inbuf pointers are initialized so that BPIN > EOB, and thereby input
230 is read immediately. */
241 /* Write if there are at least OUTSIZE bytes in OUTBUF. */
243 if (bpout - outbuf >= outsize)
245 unsigned char *wp = outbuf;
248 if (full_write (output_desc, wp, outsize) < 0)
249 error (1, errno, _("write error"));
252 while (bpout - wp >= outsize);
254 /* Move the remaining bytes to the beginning of the
257 memmove (outbuf, wp, bpout - wp);
258 bpout = outbuf + (bpout - wp);
261 /* Is INBUF empty? */
268 /* Is there any input to read immediately?
269 If not, we are about to wait,
270 so write all buffered output before waiting. */
273 && ioctl (input_desc, FIONREAD, &n_to_read) < 0)
275 /* Ultrix returns EOPNOTSUPP on NFS;
276 HP-UX returns ENOTTY on pipes.
277 SunOS returns EINVAL and
278 More/BSD returns ENODEV on special files
280 Irix-5 returns ENOSYS on pipes. */
281 if (errno == EOPNOTSUPP || errno == ENOTTY
282 || errno == EINVAL || errno == ENODEV
290 error (0, errno, _("cannot do ioctl on `%s'"), infile);
292 newlines2 = newlines;
299 int n_write = bpout - outbuf;
301 if (full_write (output_desc, outbuf, n_write) < 0)
302 error (1, errno, _("write error"));
306 /* Read more input into INBUF. */
308 n_read = safe_read (input_desc, inbuf, insize);
311 error (0, errno, "%s", infile);
313 newlines2 = newlines;
318 newlines2 = newlines;
322 /* Update the pointers and insert a sentinel at the buffer
331 /* It was a real (not a sentinel) newline. */
333 /* Was the last line empty?
334 (i.e. have two or more consecutive newlines been read?) */
338 /* Are multiple adjacent empty lines to be substituted by
339 single ditto (-s), and this was the second empty line? */
341 if (squeeze_empty_lines && newlines >= 2)
347 /* Are line numbers to be written at empty lines (-n)? */
349 if (numbers && numbers_at_empty_lines)
352 bpout = (unsigned char *) stpcpy (bpout, line_num_print);
356 /* Output a currency symbol if requested (-e). */
361 /* Output the newline. */
369 /* Are we at the beginning of a line, and line numbers are requested? */
371 if (newlines >= 0 && numbers)
374 bpout = (unsigned char *) stpcpy (bpout, line_num_print);
377 /* Here CH cannot contain a newline character. */
379 /* The loops below continue until a newline character is found,
380 which means that the buffer is empty or that a proper newline
383 /* If quoting, i.e. at least one of -v, -e, or -t specified,
384 scan for chars that need conversion. */
407 *bpout++ = ch - 128 + 64;
410 else if (ch == '\t' && output_tabs)
424 /* Not quoting, neither of -v, -e, or -t specified. */
427 if (ch == '\t' && !output_tabs)
448 /* Optimal size of i/o operations of output. */
451 /* Optimal size of i/o operations of input. */
454 /* Pointer to the input buffer. */
455 unsigned char *inbuf;
457 /* Pointer to the output buffer. */
458 unsigned char *outbuf;
462 /* Index in argv to processed argument. */
465 /* Device number of the output (file or whatever). */
468 /* I-node number of the output. */
471 /* Nonzero if the output file should not be the same as any input file. */
472 int check_redirection = 1;
474 /* Nonzero if we have ever read standard input. */
475 int have_read_stdin = 0;
477 struct stat stat_buf;
479 /* Variables that are set according to the specified options. */
481 int numbers_at_empty_lines = 1;
482 int squeeze_empty_lines = 0;
483 int mark_line_ends = 0;
487 /* If non-zero, call cat, otherwise call simple_cat to do the actual work. */
490 /* If non-zero, display usage information and exit. */
491 static int show_help;
493 /* If non-zero, print the version on standard output then exit. */
494 static int show_version;
496 static struct option const long_options[] =
498 {"number-nonblank", no_argument, NULL, 'b'},
499 {"number", no_argument, NULL, 'n'},
500 {"squeeze-blank", no_argument, NULL, 's'},
501 {"show-nonprinting", no_argument, NULL, 'v'},
502 {"show-ends", no_argument, NULL, 'E'},
503 {"show-tabs", no_argument, NULL, 'T'},
504 {"show-all", no_argument, NULL, 'A'},
505 {"help", no_argument, &show_help, 1},
506 {"version", no_argument, &show_version, 1},
510 program_name = argv[0];
512 /* Parse command line options. */
514 while ((c = getopt_long (argc, argv, "benstuvAET", long_options, (int *) 0))
525 numbers_at_empty_lines = 0;
541 squeeze_empty_lines = 1;
551 /* We provide the -u feature unconditionally. */
583 printf ("cat - %s\n", version_string);
592 /* Get device, i-node number, and optimal blocksize of output. */
594 if (fstat (output_desc, &stat_buf) < 0)
595 error (1, errno, _("standard output"));
597 outsize = ST_BLKSIZE (stat_buf);
598 /* Input file can be output file for non-regular files.
599 fstat on pipes returns S_IFSOCK on some systems, S_IFIFO
600 on others, so the checking should not be done for those types,
601 and to allow things like cat < /dev/tty > /dev/tty, checking
602 is not done for device files either. */
604 if (S_ISREG (stat_buf.st_mode))
606 out_dev = stat_buf.st_dev;
607 out_ino = stat_buf.st_ino;
611 check_redirection = 0;
612 #ifdef lint /* Suppress `used before initialized' warning. */
618 /* Check if any of the input files are the same as the output file. */
628 infile = argv[argind];
630 if (infile[0] == '-' && infile[1] == 0)
637 input_desc = open (infile, O_RDONLY);
640 error (0, errno, "%s", infile);
646 if (fstat (input_desc, &stat_buf) < 0)
648 error (0, errno, "%s", infile);
652 insize = ST_BLKSIZE (stat_buf);
654 /* Compare the device and i-node numbers of this input file with
655 the corresponding values of the (output file associated with)
656 stdout, and skip this input file if they coincide. Input
657 files cannot be redirected to themselves. */
659 if (check_redirection
660 && stat_buf.st_dev == out_dev && stat_buf.st_ino == out_ino)
662 error (0, 0, _("%s: input file is output file"), infile);
667 /* Select which version of `cat' to use. If any options (more than -u,
668 --version, or --help) were specified, use `cat', otherwise use
673 insize = max (insize, outsize);
674 inbuf = (unsigned char *) xmalloc (insize);
676 simple_cat (inbuf, insize);
680 inbuf = (unsigned char *) xmalloc (insize + 1);
682 /* Why are (OUTSIZE - 1 + INSIZE * 4 + 13) bytes allocated for
685 A test whether output needs to be written is done when the input
686 buffer empties or when a newline appears in the input. After
687 output is written, at most (OUTSIZE - 1) bytes will remain in the
688 buffer. Now INSIZE bytes of input is read. Each input character
689 may grow by a factor of 4 (by the prepending of M-^). If all
690 characters do, and no newlines appear in this block of input, we
691 will have at most (OUTSIZE - 1 + INSIZE) bytes in the buffer. If
692 the last character in the preceding block of input was a
693 newline, a line number may be written (according to the given
694 options) as the first thing in the output buffer. (Done after the
695 new input is read, but before processing of the input begins.) A
696 line number requires seldom more than 13 positions. */
698 outbuf = (unsigned char *) xmalloc (outsize - 1 + insize * 4 + 13);
700 cat (inbuf, insize, outbuf, outsize, quote,
701 output_tabs, numbers, numbers_at_empty_lines, mark_line_ends,
702 squeeze_empty_lines);
710 if (strcmp (infile, "-") && close (input_desc) < 0)
712 error (0, errno, "%s", infile);
716 while (++argind < argc);
718 if (have_read_stdin && close (0) < 0)
719 error (1, errno, "-");
721 error (1, errno, _("write error"));