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>
34 #define max(h,i) ((h) > (i) ? (h) : (i))
41 static void next_line_num ();
42 static void simple_cat ();
44 /* Name under which this program was invoked. */
47 /* Name of input file. May be "-". */
50 /* Descriptor on which input file is open. */
51 static int input_desc;
53 /* Descriptor on which output file is open. Always is 1. */
54 static int output_desc;
56 /* Buffer for line numbers. */
57 static char line_buf[13] =
58 {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '0', '\t', '\0'};
60 /* Position in `line_buf' where printing starts. This will not change
61 unless the number of lines are more than 999999. */
62 static char *line_num_print = line_buf + 5;
64 /* Position of the first digit in `line_buf'. */
65 static char *line_num_start = line_buf + 10;
67 /* Position of the last digit in `line_buf'. */
68 static char *line_num_end = line_buf + 10;
70 /* Preserves the `cat' function's local `newlines' between invocations. */
71 static int newlines2 = 0;
73 /* Count of non-fatal error conditions. */
74 static int exit_stat = 0;
81 fprintf (stderr, "%s: %s\n", program_name, reason);
84 Usage: %s [-benstuvAET] [--number] [--number-nonblank] [--squeeze-blank]\n\
85 [--show-nonprinting] [--show-ends] [--show-tabs] [--show-all]\n\
86 [--help] [--version] [file...]\n",
98 /* Optimal size of i/o operations of output. */
101 /* Optimal size of i/o operations of input. */
104 /* Pointer to the input buffer. */
105 unsigned char *inbuf;
107 /* Pointer to the output buffer. */
108 unsigned char *outbuf;
112 /* Index in argv to processed argument. */
115 /* Device number of the output (file or whatever). */
118 /* I-node number of the output. */
121 /* Nonzero if the output file should not be the same as any input file. */
122 int check_redirection = 1;
124 /* Nonzero if we have ever read standard input. */
125 int have_read_stdin = 0;
127 struct stat stat_buf;
129 /* Variables that are set according to the specified options. */
131 int numbers_at_empty_lines = 1;
132 int squeeze_empty_lines = 0;
133 int mark_line_ends = 0;
137 /* If non-zero, call cat, otherwise call simple_cat to do the actual work. */
140 /* If non-zero, display usage information and exit. */
141 static int flag_help;
143 /* If non-zero, print the version on standard error. */
144 static int flag_version;
146 static struct option const long_options[] =
148 {"number-nonblank", no_argument, NULL, 'b'},
149 {"number", no_argument, NULL, 'n'},
150 {"squeeze-blank", no_argument, NULL, 's'},
151 {"show-nonprinting", no_argument, NULL, 'v'},
152 {"show-ends", no_argument, NULL, 'E'},
153 {"show-tabs", no_argument, NULL, 'T'},
154 {"show-all", no_argument, NULL, 'A'},
155 {"help", no_argument, &flag_help, 1},
156 {"version", no_argument, &flag_version, 1},
160 program_name = argv[0];
162 /* Parse command line options. */
164 while ((c = getopt_long (argc, argv, "benstuvAET", long_options, (int *) 0))
175 numbers_at_empty_lines = 0;
191 squeeze_empty_lines = 1;
201 /* We provide the -u feature unconditionally. */
232 fprintf (stderr, "%s\n", version_string);
239 /* Get device, i-node number, and optimal blocksize of output. */
241 if (fstat (output_desc, &stat_buf) < 0)
242 error (1, errno, "standard output");
244 outsize = ST_BLKSIZE (stat_buf);
245 /* Input file can be output file for non-regular files.
246 fstat on pipes returns S_IFSOCK on some systems, S_IFIFO
247 on others, so the checking should not be done for those types,
248 and to allow things like cat < /dev/tty > /dev/tty, checking
249 is not done for device files either. */
251 if (S_ISREG (stat_buf.st_mode))
253 out_dev = stat_buf.st_dev;
254 out_ino = stat_buf.st_ino;
257 check_redirection = 0;
259 /* Check if any of the input files are the same as the output file. */
269 infile = argv[argind];
271 if (infile[0] == '-' && infile[1] == 0)
278 input_desc = open (infile, O_RDONLY);
281 error (0, errno, "%s", infile);
287 if (fstat (input_desc, &stat_buf) < 0)
289 error (0, errno, "%s", infile);
293 insize = ST_BLKSIZE (stat_buf);
295 /* Compare the device and i-node numbers of this input file with
296 the corresponding values of the (output file associated with)
297 stdout, and skip this input file if they coincide. Input
298 files cannot be redirected to themselves. */
300 if (check_redirection
301 && stat_buf.st_dev == out_dev && stat_buf.st_ino == out_ino)
303 error (0, 0, "%s: input file is output file", infile);
308 /* Select which version of `cat' to use. If any options (more than -u,
309 --version, or --help) were specified, use `cat', otherwise use
314 insize = max (insize, outsize);
315 inbuf = (unsigned char *) xmalloc (insize);
317 simple_cat (inbuf, insize);
321 inbuf = (unsigned char *) xmalloc (insize + 1);
323 /* Why are (OUTSIZE - 1 + INSIZE * 4 + 13) bytes allocated for
326 A test whether output needs to be written is done when the input
327 buffer empties or when a newline appears in the input. After
328 output is written, at most (OUTSIZE - 1) bytes will remain in the
329 buffer. Now INSIZE bytes of input is read. Each input character
330 may grow by a factor of 4 (by the prepending of M-^). If all
331 characters do, and no newlines appear in this block of input, we
332 will have at most (OUTSIZE - 1 + INSIZE) bytes in the buffer. If
333 the last character in the preceding block of input was a
334 newline, a line number may be written (according to the given
335 options) as the first thing in the output buffer. (Done after the
336 new input is read, but before processing of the input begins.) A
337 line number requires seldom more than 13 positions. */
339 outbuf = (unsigned char *) xmalloc (outsize - 1 + insize * 4 + 13);
341 cat (inbuf, insize, outbuf, outsize, quote,
342 output_tabs, numbers, numbers_at_empty_lines, mark_line_ends,
343 squeeze_empty_lines);
351 if (strcmp (infile, "-") && close (input_desc) < 0)
353 error (0, errno, "%s", infile);
357 while (++argind < argc);
359 if (have_read_stdin && close (0) < 0)
360 error (1, errno, "-");
362 error (1, errno, "write error");
367 /* Plain cat. Copies the file behind `input_desc' to the file behind
371 simple_cat (buf, bufsize)
372 /* Pointer to the buffer, used by reads and writes. */
375 /* Number of characters preferably read or written by each read and write
379 /* Actual number of characters read, and therefore written. */
382 /* Loop until the end of the file. */
386 /* Read a block of input. */
388 n_read = read (input_desc, buf, bufsize);
391 error (0, errno, "%s", infile);
396 /* End of this file? */
401 /* Write this block out. */
403 if (write (output_desc, buf, n_read) != n_read)
404 error (1, errno, "write error");
408 /* Cat the file behind INPUT_DESC to the file behind OUTPUT_DESC.
409 Called if any option more than -u was specified.
411 A newline character is always put at the end of the buffer, to make
412 an explicit test for buffer end unnecessary. */
415 cat (inbuf, insize, outbuf, outsize, quote,
416 output_tabs, numbers, numbers_at_empty_lines,
417 mark_line_ends, squeeze_empty_lines)
419 /* Pointer to the beginning of the input buffer. */
420 unsigned char *inbuf;
422 /* Number of characters read in each read call. */
425 /* Pointer to the beginning of the output buffer. */
426 unsigned char *outbuf;
428 /* Number of characters written by each write call. */
431 /* Variables that have values according to the specified options. */
435 int numbers_at_empty_lines;
437 int squeeze_empty_lines;
439 /* Last character read from the input buffer. */
442 /* Pointer to the next character in the input buffer. */
445 /* Pointer to the first non-valid byte in the input buffer, i.e. the
446 current end of the buffer. */
449 /* Pointer to the position where the next character shall be written. */
450 unsigned char *bpout;
452 /* Number of characters read by the last read call. */
455 /* Determines how many consecutive newlines there have been in the
456 input. 0 newlines makes NEWLINES -1, 1 newline makes NEWLINES 1,
457 etc. Initially 0 to indicate that we are at the beginning of a
458 new line. The "state" of the procedure is determined by
460 int newlines = newlines2;
463 /* If nonzero, use the FIONREAD ioctl, as an optimization.
464 (On Ultrix, it is not supported on NFS filesystems.) */
465 int use_fionread = 1;
468 /* The inbuf pointers are initialized so that BPIN > EOB, and thereby input
469 is read immediately. */
480 /* Write if there are at least OUTSIZE bytes in OUTBUF. */
482 if (bpout - outbuf >= outsize)
484 unsigned char *wp = outbuf;
487 if (write (output_desc, wp, outsize) != outsize)
488 error (1, errno, "write error");
491 while (bpout - wp >= outsize);
493 /* Move the remaining bytes to the beginning of the
496 bcopy (wp, outbuf, bpout - wp);
497 bpout = outbuf + (bpout - wp);
500 /* Is INBUF empty? */
507 /* Is there any input to read immediately?
508 If not, we are about to wait,
509 so write all buffered output before waiting. */
512 && ioctl (input_desc, FIONREAD, &n_to_read) < 0)
514 /* Ultrix returns EOPNOTSUPP on NFS;
515 HP-UX returns ENOTTY on pipes. */
516 if (errno == EOPNOTSUPP || errno == ENOTTY)
520 error (0, errno, "cannot do ioctl on `%s'", infile);
522 newlines2 = newlines;
529 int n_write = bpout - outbuf;
531 if (write (output_desc, outbuf, n_write) != n_write)
532 error (1, errno, "write error");
536 /* Read more input into INBUF. */
538 n_read = read (input_desc, inbuf, insize);
541 error (0, errno, "%s", infile);
543 newlines2 = newlines;
548 newlines2 = newlines;
552 /* Update the pointers and insert a sentinel at the buffer
561 /* It was a real (not a sentinel) newline. */
563 /* Was the last line empty?
564 (i.e. have two or more consecutive newlines been read?) */
568 /* Are multiple adjacent empty lines to be substituted by
569 single ditto (-s), and this was the second empty line? */
571 if (squeeze_empty_lines && newlines >= 2)
577 /* Are line numbers to be written at empty lines (-n)? */
579 if (numbers && numbers_at_empty_lines)
582 bpout = (unsigned char *) stpcpy (bpout, line_num_print);
586 /* Output a currency symbol if requested (-e). */
591 /* Output the newline. */
599 /* Are we at the beginning of a line, and line numbers are requested? */
601 if (newlines >= 0 && numbers)
604 bpout = (unsigned char *) stpcpy (bpout, line_num_print);
607 /* Here CH cannot contain a newline character. */
609 /* The loops below continue until a newline character is found,
610 which means that the buffer is empty or that a proper newline
613 /* If quoting, i.e. at least one of -v, -e, or -t specified,
614 scan for chars that need conversion. */
637 *bpout++ = ch - 128 + 64;
640 else if (ch == '\t' && output_tabs)
654 /* Not quoting, neither of -v, -e, or -t specified. */
657 if (ch == '\t' && !output_tabs)
673 /* Compute the next line number. */
678 char *endp = line_num_end;
685 while (endp >= line_num_start);
686 *--line_num_start = '1';
687 if (line_num_start < line_num_print)