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. */
27 #include <sys/types.h>
29 #include <sys/ioctl.h>
33 #define max(h,i) ((h) > (i) ? (h) : (i))
40 static void next_line_num ();
41 static void simple_cat ();
43 /* Name under which this program was invoked. */
46 /* Name of input file. May be "-". */
49 /* Descriptor on which input file is open. */
50 static int input_desc;
52 /* Descriptor on which output file is open. Always is 1. */
53 static int output_desc;
55 /* Buffer for line numbers. */
56 static char line_buf[13] =
57 {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '0', '\t', '\0'};
59 /* Position in `line_buf' where printing starts. This will not change
60 unless the number of lines are more than 999999. */
61 static char *line_num_print = line_buf + 5;
63 /* Position of the first digit in `line_buf'. */
64 static char *line_num_start = line_buf + 10;
66 /* Position of the last digit in `line_buf'. */
67 static char *line_num_end = line_buf + 10;
69 /* Preserves the `cat' function's local `newlines' between invocations. */
70 static int newlines2 = 0;
72 /* Count of non-fatal error conditions. */
73 static int exit_stat = 0;
80 fprintf (stderr, "%s: %s\n", program_name, reason);
83 Usage: %s [-benstuvAET] [--number] [--number-nonblank] [--squeeze-blank]\n\
84 [--show-nonprinting] [--show-ends] [--show-tabs] [--show-all]\n\
97 /* Optimal size of i/o operations of output. */
100 /* Optimal size of i/o operations of input. */
103 /* Pointer to the input buffer. */
104 unsigned char *inbuf;
106 /* Pointer to the output buffer. */
107 unsigned char *outbuf;
111 /* Index in argv to processed argument. */
114 /* Device number of the output (file or whatever). */
117 /* I-node number of the output. */
120 /* Nonzero if the output file should not be the same as any input file. */
121 int check_redirection = 1;
123 /* Nonzero if we have ever read standard input. */
124 int have_read_stdin = 0;
126 struct stat stat_buf;
128 /* Variables that are set according to the specified options. */
130 int numbers_at_empty_lines = 1;
131 int squeeze_empty_lines = 0;
132 int mark_line_ends = 0;
137 static struct option const long_options[] =
139 {"number-nonblank", no_argument, NULL, 'b'},
140 {"number", no_argument, NULL, 'n'},
141 {"squeeze-blank", no_argument, NULL, 's'},
142 {"show-nonprinting", no_argument, NULL, 'v'},
143 {"show-ends", no_argument, NULL, 'E'},
144 {"show-tabs", no_argument, NULL, 'T'},
145 {"show-all", no_argument, NULL, 'A'},
149 program_name = argv[0];
151 /* Parse command line options. */
153 while ((c = getopt_long (argc, argv, "benstuvAET", long_options, (int *) 0))
161 numbers_at_empty_lines = 0;
174 squeeze_empty_lines = 1;
183 /* We provide the -u feature unconditionally. */
212 /* Get device, i-node number, and optimal blocksize of output. */
214 if (fstat (output_desc, &stat_buf) < 0)
215 error (1, errno, "standard output");
217 outsize = ST_BLKSIZE (stat_buf);
218 /* Input file can be output file for non-regular files.
219 fstat on pipes returns S_IFSOCK on some systems, S_IFIFO
220 on others, so the checking should not be done for those types,
221 and to allow things like cat < /dev/tty > /dev/tty, checking
222 is not done for device files either. */
224 if (S_ISREG (stat_buf.st_mode))
226 out_dev = stat_buf.st_dev;
227 out_ino = stat_buf.st_ino;
230 check_redirection = 0;
232 /* Check if any of the input files are the same as the output file. */
242 infile = argv[argind];
244 if (infile[0] == '-' && infile[1] == 0)
251 input_desc = open (infile, O_RDONLY);
254 error (0, errno, "%s", infile);
260 if (fstat (input_desc, &stat_buf) < 0)
262 error (0, errno, "%s", infile);
266 insize = ST_BLKSIZE (stat_buf);
268 /* Compare the device and i-node numbers of this input file with
269 the corresponding values of the (output file associated with)
270 stdout, and skip this input file if they coincide. Input
271 files cannot be redirected to themselves. */
273 if (check_redirection
274 && stat_buf.st_dev == out_dev && stat_buf.st_ino == out_ino)
276 error (0, 0, "%s: input file is output file", infile);
281 /* Select which version of `cat' to use. If any options (more than -u)
282 were specified, use `cat', otherwise use `simple_cat'. */
286 insize = max (insize, outsize);
287 inbuf = (unsigned char *) xmalloc (insize);
289 simple_cat (inbuf, insize);
293 inbuf = (unsigned char *) xmalloc (insize + 1);
295 /* Why are (OUTSIZE - 1 + INSIZE * 4 + 13) bytes allocated for
298 A test whether output needs to be written is done when the input
299 buffer empties or when a newline appears in the input. After
300 output is written, at most (OUTSIZE - 1) bytes will remain in the
301 buffer. Now INSIZE bytes of input is read. Each input character
302 may grow by a factor of 4 (by the prepending of M-^). If all
303 characters do, and no newlines appear in this block of input, we
304 will have at most (OUTSIZE - 1 + INSIZE) bytes in the buffer. If
305 the last character in the preceeding block of input was a
306 newline, a line number may be written (according to the given
307 options) as the first thing in the output buffer. (Done after the
308 new input is read, but before processing of the input begins.) A
309 line number requires seldom more than 13 positions. */
311 outbuf = (unsigned char *) xmalloc (outsize - 1 + insize * 4 + 13);
313 cat (inbuf, insize, outbuf, outsize, quote,
314 output_tabs, numbers, numbers_at_empty_lines, mark_line_ends,
315 squeeze_empty_lines);
323 if (strcmp (infile, "-") && close (input_desc) < 0)
325 error (0, errno, "%s", infile);
329 while (++argind < argc);
331 if (have_read_stdin && close (0) < 0)
332 error (1, errno, "-");
334 error (1, errno, "write error");
339 /* Plain cat. Copies the file behind `input_desc' to the file behind
343 simple_cat (buf, bufsize)
344 /* Pointer to the buffer, used by reads and writes. */
347 /* Number of characters preferably read or written by each read and write
351 /* Actual number of characters read, and therefore written. */
354 /* Loop until the end of the file. */
358 /* Read a block of input. */
360 n_read = read (input_desc, buf, bufsize);
363 error (0, errno, "%s", infile);
368 /* End of this file? */
373 /* Write this block out. */
375 if (write (output_desc, buf, n_read) != n_read)
376 error (1, errno, "write error");
380 /* Cat the file behind INPUT_DESC to the file behind OUTPUT_DESC.
381 Called if any option more than -u was specified.
383 A newline character is always put at the end of the buffer, to make
384 an explicit test for buffer end unnecessary. */
387 cat (inbuf, insize, outbuf, outsize, quote,
388 output_tabs, numbers, numbers_at_empty_lines,
389 mark_line_ends, squeeze_empty_lines)
391 /* Pointer to the beginning of the input buffer. */
392 unsigned char *inbuf;
394 /* Number of characters read in each read call. */
397 /* Pointer to the beginning of the output buffer. */
398 unsigned char *outbuf;
400 /* Number of characters written by each write call. */
403 /* Variables that have values according to the specified options. */
407 int numbers_at_empty_lines;
409 int squeeze_empty_lines;
411 /* Last character read from the input buffer. */
414 /* Pointer to the next character in the input buffer. */
417 /* Pointer to the first non-valid byte in the input buffer, i.e. the
418 current end of the buffer. */
421 /* Pointer to the position where the next character shall be written. */
422 unsigned char *bpout;
424 /* Number of characters read by the last read call. */
427 /* Determines how many consequtive newlines there have been in the
428 input. 0 newlines makes NEWLINES -1, 1 newline makes NEWLINES 1,
429 etc. Initially 0 to indicate that we are at the beginning of a
430 new line. The "state" of the procedure is determined by
432 int newlines = newlines2;
435 /* If nonzero, use the FIONREAD ioctl, as an optimization.
436 (On Ultrix, it is not supported on NFS filesystems.) */
437 int use_fionread = 1;
440 /* The inbuf pointers are initialized so that BPIN > EOB, and thereby input
441 is read immediately. */
452 /* Write if there are at least OUTSIZE bytes in OUTBUF. */
454 if (bpout - outbuf >= outsize)
456 unsigned char *wp = outbuf;
459 if (write (output_desc, wp, outsize) != outsize)
460 error (1, errno, "write error");
463 while (bpout - wp >= outsize);
465 /* Move the remaining bytes to the beginning of the
468 bcopy (wp, outbuf, bpout - wp);
469 bpout = outbuf + (bpout - wp);
472 /* Is INBUF empty? */
479 /* Is there any input to read immediately?
480 If not, we are about to wait,
481 so write all buffered output before waiting. */
484 && ioctl (input_desc, FIONREAD, &n_to_read) < 0)
486 /* Ultrix returns EOPNOTSUPP on NFS;
487 HP-UX returns ENOTTY on pipes. */
488 if (errno == EOPNOTSUPP || errno == ENOTTY)
492 error (0, errno, "cannot do ioctl on `%s'", infile);
494 newlines2 = newlines;
501 int n_write = bpout - outbuf;
503 if (write (output_desc, outbuf, n_write) != n_write)
504 error (1, errno, "write error");
508 /* Read more input into INBUF. */
510 n_read = read (input_desc, inbuf, insize);
513 error (0, errno, "%s", infile);
515 newlines2 = newlines;
520 newlines2 = newlines;
524 /* Update the pointers and insert a sentinel at the buffer
533 /* It was a real (not a sentinel) newline. */
535 /* Was the last line empty?
536 (i.e. have two or more consecutive newlines been read?) */
540 /* Are multiple adjacent empty lines to be substituted by
541 single ditto (-s), and this was the second empty line? */
543 if (squeeze_empty_lines && newlines >= 2)
549 /* Are line numbers to be written at empty lines (-n)? */
551 if (numbers && numbers_at_empty_lines)
554 bpout = (unsigned char *) stpcpy (bpout, line_num_print);
558 /* Output a currency symbol if requested (-e). */
563 /* Output the newline. */
571 /* Are we at the beginning of a line, and line numbers are requested? */
573 if (newlines >= 0 && numbers)
576 bpout = (unsigned char *) stpcpy (bpout, line_num_print);
579 /* Here CH cannot contain a newline character. */
581 /* The loops below continue until a newline character is found,
582 which means that the buffer is empty or that a proper newline
585 /* If quoting, i.e. at least one of -v, -e, or -t specified,
586 scan for chars that need conversion. */
609 *bpout++ = ch - 128 + 64;
612 else if (ch == '\t' && output_tabs)
626 /* Not quoting, neither of -v, -e, or -t specified. */
629 if (ch == '\t' && !output_tabs)
645 /* Compute the next line number. */
650 char *endp = line_num_end;
657 while (endp >= line_num_start);
658 *--line_num_start = '1';
659 if (line_num_start < line_num_print)