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))
39 void next_line_num ();
42 /* Name under which this program was invoked. */
45 /* Name of input file. May be "-". */
48 /* Descriptor on which input file is open. */
51 /* Descriptor on which output file is open. Always is 1. */
54 /* Buffer for line numbers. */
56 {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '0', '\t', '\0'};
58 /* Position in `line_buf' where printing starts. This will not change
59 unless the number of lines are more than 999999. */
60 char *line_num_print = line_buf + 5;
62 /* Position of the first digit in `line_buf'. */
63 char *line_num_start = line_buf + 10;
65 /* Position of the last digit in `line_buf'. */
66 char *line_num_end = line_buf + 10;
68 /* Preserves the `cat' function's local `newlines' between invocations. */
71 /* Count of non-fatal error conditions. */
79 fprintf (stderr, "%s: %s\n", program_name, reason);
82 Usage: %s [-benstuvAET] [--number] [--number-nonblank] [--squeeze-blank]\n\
83 [--show-nonprinting] [--show-ends] [--show-tabs] [--show-all]\n\
96 /* Optimal size of i/o operations of output. */
99 /* Optimal size of i/o operations of input. */
102 /* Pointer to the input buffer. */
103 unsigned char *inbuf;
105 /* Pointer to the output buffer. */
106 unsigned char *outbuf;
110 /* Index in argv to processed argument. */
113 /* Device number of the output (file or whatever). */
116 /* I-node number of the output. */
119 /* Nonzero if the output file should not be the same as any input file. */
120 int check_redirection = 1;
122 /* Nonzero if we have ever read standard input. */
123 int have_read_stdin = 0;
125 struct stat stat_buf;
127 /* Variables that are set according to the specified options. */
129 int numbers_at_empty_lines = 1;
130 int squeeze_empty_lines = 0;
131 int mark_line_ends = 0;
136 static struct option long_options[] =
138 {"number-nonblank", 0, NULL, 'b'},
139 {"number", 0, NULL, 'n'},
140 {"squeeze-blank", 0, NULL, 's'},
141 {"show-nonprinting", 0, NULL, 'v'},
142 {"show-ends", 0, NULL, 'E'},
143 {"show-tabs", 0, NULL, 'T'},
144 {"show-all", 0, NULL, 'A'},
148 program_name = argv[0];
150 /* Parse command line options. */
152 while ((c = getopt_long (argc, argv, "benstuvAET", long_options, (int *) 0))
160 numbers_at_empty_lines = 0;
173 squeeze_empty_lines = 1;
182 /* We provide the -u feature unconditionally. */
211 /* Get device, i-node number, and optimal blocksize of output. */
213 if (fstat (output_desc, &stat_buf) < 0)
214 error (1, errno, "standard output");
216 outsize = ST_BLKSIZE (stat_buf);
217 /* Input file can be output file for non-regular files.
218 fstat on pipes returns S_IFSOCK on some systems, S_IFIFO
219 on others, so the checking should not be done for those types,
220 and to allow things like cat < /dev/tty > /dev/tty, checking
221 is not done for device files either. */
223 if (S_ISREG (stat_buf.st_mode))
225 out_dev = stat_buf.st_dev;
226 out_ino = stat_buf.st_ino;
229 check_redirection = 0;
231 /* Check if any of the input files are the same as the output file. */
241 infile = argv[argind];
243 if (infile[0] == '-' && infile[1] == 0)
250 input_desc = open (infile, O_RDONLY);
253 error (0, errno, "%s", infile);
259 if (fstat (input_desc, &stat_buf) < 0)
261 error (0, errno, "%s", infile);
265 insize = ST_BLKSIZE (stat_buf);
267 /* Compare the device and i-node numbers of this input file with
268 the corresponding values of the (output file associated with)
269 stdout, and skip this input file if they coincide. Input
270 files cannot be redirected to themselves. */
272 if (check_redirection
273 && stat_buf.st_dev == out_dev && stat_buf.st_ino == out_ino)
275 error (0, 0, "%s: input file is output file", infile);
280 /* Select which version of `cat' to use. If any options (more than -u)
281 were specified, use `cat', otherwise use `simple_cat'. */
285 insize = max (insize, outsize);
286 inbuf = (unsigned char *) xmalloc (insize);
288 simple_cat (inbuf, insize);
292 inbuf = (unsigned char *) xmalloc (insize + 1);
294 /* Why are (OUTSIZE - 1 + INSIZE * 4 + 13) bytes allocated for
297 A test whether output needs to be written is done when the input
298 buffer empties or when a newline appears in the input. After
299 output is written, at most (OUTSIZE - 1) bytes will remain in the
300 buffer. Now INSIZE bytes of input is read. Each input character
301 may grow by a factor of 4 (by the prepending of M-^). If all
302 characters do, and no newlines appear in this block of input, we
303 will have at most (OUTSIZE - 1 + INSIZE) bytes in the buffer. If
304 the last character in the preceeding block of input was a
305 newline, a line number may be written (according to the given
306 options) as the first thing in the output buffer. (Done after the
307 new input is read, but before processing of the input begins.) A
308 line number requires seldom more than 13 positions. */
310 outbuf = (unsigned char *) xmalloc (outsize - 1 + insize * 4 + 13);
312 cat (inbuf, insize, outbuf, outsize, quote,
313 output_tabs, numbers, numbers_at_empty_lines, mark_line_ends,
314 squeeze_empty_lines);
322 if (strcmp (infile, "-") && close (input_desc) < 0)
324 error (0, errno, "%s", infile);
328 while (++argind < argc);
330 if (have_read_stdin && close (0) < 0)
331 error (1, errno, "-");
333 error (1, errno, "write error");
338 /* Plain cat. Copies the file behind `input_desc' to the file behind
342 simple_cat (buf, bufsize)
343 /* Pointer to the buffer, used by reads and writes. */
346 /* Number of characters preferably read or written by each read and write
350 /* Actual number of characters read, and therefore written. */
353 /* Loop until the end of the file. */
357 /* Read a block of input. */
359 n_read = read (input_desc, buf, bufsize);
362 error (0, errno, "%s", infile);
367 /* End of this file? */
372 /* Write this block out. */
374 if (write (output_desc, buf, n_read) != n_read)
375 error (1, errno, "write error");
379 /* Cat the file behind INPUT_DESC to the file behind OUTPUT_DESC.
380 Called if any option more than -u was specified.
382 A newline character is always put at the end of the buffer, to make
383 an explicit test for buffer end unnecessary. */
386 cat (inbuf, insize, outbuf, outsize, quote,
387 output_tabs, numbers, numbers_at_empty_lines,
388 mark_line_ends, squeeze_empty_lines)
390 /* Pointer to the beginning of the input buffer. */
391 unsigned char *inbuf;
393 /* Number of characters read in each read call. */
396 /* Pointer to the beginning of the output buffer. */
397 unsigned char *outbuf;
399 /* Number of characters written by each write call. */
402 /* Variables that have values according to the specified options. */
406 int numbers_at_empty_lines;
408 int squeeze_empty_lines;
410 /* Last character read from the input buffer. */
413 /* Pointer to the next character in the input buffer. */
416 /* Pointer to the first non-valid byte in the input buffer, i.e. the
417 current end of the buffer. */
420 /* Pointer to the position where the next character shall be written. */
421 unsigned char *bpout;
423 /* Number of characters read by the last read call. */
426 /* Determines how many consequtive newlines there have been in the
427 input. 0 newlines makes NEWLINES -1, 1 newline makes NEWLINES 1,
428 etc. Initially 0 to indicate that we are at the beginning of a
429 new line. The "state" of the procedure is determined by
431 int newlines = newlines2;
434 /* If nonzero, use the FIONREAD ioctl, as an optimization.
435 (On Ultrix, it is not supported on NFS filesystems.) */
436 int use_fionread = 1;
439 /* The inbuf pointers are initialized so that BPIN > EOB, and thereby input
440 is read immediately. */
451 /* Write if there are at least OUTSIZE bytes in OUTBUF. */
453 if (bpout - outbuf >= outsize)
455 unsigned char *wp = outbuf;
458 if (write (output_desc, wp, outsize) != outsize)
459 error (1, errno, "write error");
462 while (bpout - wp >= outsize);
464 /* Move the remaining bytes to the beginning of the
467 bcopy (wp, outbuf, bpout - wp);
468 bpout = outbuf + (bpout - wp);
471 /* Is INBUF empty? */
478 /* Is there any input to read immediately?
479 If not, we are about to wait,
480 so write all buffered output before waiting. */
483 && ioctl (input_desc, FIONREAD, &n_to_read) < 0)
485 /* Ultrix returns EOPNOTSUPP on NFS;
486 HP-UX returns ENOTTY on pipes. */
487 if (errno == EOPNOTSUPP || errno == ENOTTY)
491 error (0, errno, "cannot do ioctl on `%s'", infile);
493 newlines2 = newlines;
500 int n_write = bpout - outbuf;
502 if (write (output_desc, outbuf, n_write) != n_write)
503 error (1, errno, "write error");
507 /* Read more input into INBUF. */
509 n_read = read (input_desc, inbuf, insize);
512 error (0, errno, "%s", infile);
514 newlines2 = newlines;
519 newlines2 = newlines;
523 /* Update the pointers and insert a sentinel at the buffer
532 /* It was a real (not a sentinel) newline. */
534 /* Was the last line empty?
535 (i.e. have two or more consecutive newlines been read?) */
539 /* Are multiple adjacent empty lines to be substituted by
540 single ditto (-s), and this was the second empty line? */
542 if (squeeze_empty_lines && newlines >= 2)
548 /* Are line numbers to be written at empty lines (-n)? */
550 if (numbers && numbers_at_empty_lines)
553 bpout = (unsigned char *) stpcpy (bpout, line_num_print);
557 /* Output a currency symbol if requested (-e). */
562 /* Output the newline. */
570 /* Are we at the beginning of a line, and line numbers are requested? */
572 if (newlines >= 0 && numbers)
575 bpout = (unsigned char *) stpcpy (bpout, line_num_print);
578 /* Here CH cannot contain a newline character. */
580 /* The loops below continue until a newline character is found,
581 which means that the buffer is empty or that a proper newline
584 /* If quoting, i.e. at least one of -v, -e, or -t specified,
585 scan for chars that need conversion. */
608 *bpout++ = ch - 128 + 64;
611 else if (ch == '\t' && output_tabs)
625 /* Not quoting, neither of -v, -e, or -t specified. */
628 if (ch == '\t' && !output_tabs)
644 /* Compute the next line number. */
649 char *endp = line_num_end;
656 while (endp >= line_num_start);
657 *--line_num_start = '1';
658 if (line_num_start < line_num_print)