tests: exercise tests new "==" operator
[coreutils.git] / src / cat.c
blob6df3eb08f9e5efde4837dfc76f99dbf60a43dd45
1 /* cat -- concatenate files and print on the standard output.
2 Copyright (C) 1988, 1990-1991, 1995-2011 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 3 of the License, or
7 (at your option) any later version.
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, see <http://www.gnu.org/licenses/>. */
17 /* Differences from the Unix cat:
18 * Always unbuffered, -u is ignored.
19 * Usually much faster than other versions of cat, the difference
20 is especially apparent when using the -v option.
22 By tege@sics.se, Torbjorn Granlund, advised by rms, Richard Stallman. */
24 #include <config.h>
26 #include <stdio.h>
27 #include <getopt.h>
28 #include <sys/types.h>
30 #if HAVE_STROPTS_H
31 # include <stropts.h>
32 #endif
33 #include <sys/ioctl.h>
35 #include "system.h"
36 #include "error.h"
37 #include "fadvise.h"
38 #include "full-write.h"
39 #include "quote.h"
40 #include "safe-read.h"
41 #include "xfreopen.h"
43 /* The official name of this program (e.g., no `g' prefix). */
44 #define PROGRAM_NAME "cat"
46 #define AUTHORS \
47 proper_name_utf8 ("Torbjorn Granlund", "Torbj\303\266rn Granlund"), \
48 proper_name ("Richard M. Stallman")
50 /* Name of input file. May be "-". */
51 static char const *infile;
53 /* Descriptor on which input file is open. */
54 static int input_desc;
56 /* Buffer for line numbers.
57 An 11 digit counter may overflow within an hour on a P2/466,
58 an 18 digit counter needs about 1000y */
59 #define LINE_COUNTER_BUF_LEN 20
60 static char line_buf[LINE_COUNTER_BUF_LEN] =
62 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
63 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '0',
64 '\t', '\0'
67 /* Position in `line_buf' where printing starts. This will not change
68 unless the number of lines is larger than 999999. */
69 static char *line_num_print = line_buf + LINE_COUNTER_BUF_LEN - 8;
71 /* Position of the first digit in `line_buf'. */
72 static char *line_num_start = line_buf + LINE_COUNTER_BUF_LEN - 3;
74 /* Position of the last digit in `line_buf'. */
75 static char *line_num_end = line_buf + LINE_COUNTER_BUF_LEN - 3;
77 /* Preserves the `cat' function's local `newlines' between invocations. */
78 static int newlines2 = 0;
80 void
81 usage (int status)
83 if (status != EXIT_SUCCESS)
84 fprintf (stderr, _("Try `%s --help' for more information.\n"),
85 program_name);
86 else
88 printf (_("\
89 Usage: %s [OPTION]... [FILE]...\n\
90 "),
91 program_name);
92 fputs (_("\
93 Concatenate FILE(s), or standard input, to standard output.\n\
94 \n\
95 -A, --show-all equivalent to -vET\n\
96 -b, --number-nonblank number nonempty output lines, overrides -n\n\
97 -e equivalent to -vE\n\
98 -E, --show-ends display $ at end of each line\n\
99 -n, --number number all output lines\n\
100 -s, --squeeze-blank suppress repeated empty output lines\n\
101 "), stdout);
102 fputs (_("\
103 -t equivalent to -vT\n\
104 -T, --show-tabs display TAB characters as ^I\n\
105 -u (ignored)\n\
106 -v, --show-nonprinting use ^ and M- notation, except for LFD and TAB\n\
107 "), stdout);
108 fputs (HELP_OPTION_DESCRIPTION, stdout);
109 fputs (VERSION_OPTION_DESCRIPTION, stdout);
110 fputs (_("\
112 With no FILE, or when FILE is -, read standard input.\n\
113 "), stdout);
114 printf (_("\
116 Examples:\n\
117 %s f - g Output f's contents, then standard input, then g's contents.\n\
118 %s Copy standard input to standard output.\n\
120 program_name, program_name);
121 emit_ancillary_info ();
123 exit (status);
126 /* Compute the next line number. */
128 static void
129 next_line_num (void)
131 char *endp = line_num_end;
134 if ((*endp)++ < '9')
135 return;
136 *endp-- = '0';
138 while (endp >= line_num_start);
139 if (line_num_start > line_buf)
140 *--line_num_start = '1';
141 else
142 *line_buf = '>';
143 if (line_num_start < line_num_print)
144 line_num_print--;
147 /* Plain cat. Copies the file behind `input_desc' to STDOUT_FILENO.
148 Return true if successful. */
150 static bool
151 simple_cat (
152 /* Pointer to the buffer, used by reads and writes. */
153 char *buf,
155 /* Number of characters preferably read or written by each read and write
156 call. */
157 size_t bufsize)
159 /* Actual number of characters read, and therefore written. */
160 size_t n_read;
162 /* Loop until the end of the file. */
164 while (true)
166 /* Read a block of input. */
168 n_read = safe_read (input_desc, buf, bufsize);
169 if (n_read == SAFE_READ_ERROR)
171 error (0, errno, "%s", infile);
172 return false;
175 /* End of this file? */
177 if (n_read == 0)
178 return true;
180 /* Write this block out. */
183 /* The following is ok, since we know that 0 < n_read. */
184 size_t n = n_read;
185 if (full_write (STDOUT_FILENO, buf, n) != n)
186 error (EXIT_FAILURE, errno, _("write error"));
191 /* Write any pending output to STDOUT_FILENO.
192 Pending is defined to be the *BPOUT - OUTBUF bytes starting at OUTBUF.
193 Then set *BPOUT to OUTPUT if it's not already that value. */
195 static inline void
196 write_pending (char *outbuf, char **bpout)
198 size_t n_write = *bpout - outbuf;
199 if (0 < n_write)
201 if (full_write (STDOUT_FILENO, outbuf, n_write) != n_write)
202 error (EXIT_FAILURE, errno, _("write error"));
203 *bpout = outbuf;
207 /* Cat the file behind INPUT_DESC to the file behind OUTPUT_DESC.
208 Return true if successful.
209 Called if any option more than -u was specified.
211 A newline character is always put at the end of the buffer, to make
212 an explicit test for buffer end unnecessary. */
214 static bool
215 cat (
216 /* Pointer to the beginning of the input buffer. */
217 char *inbuf,
219 /* Number of characters read in each read call. */
220 size_t insize,
222 /* Pointer to the beginning of the output buffer. */
223 char *outbuf,
225 /* Number of characters written by each write call. */
226 size_t outsize,
228 /* Variables that have values according to the specified options. */
229 bool show_nonprinting,
230 bool show_tabs,
231 bool number,
232 bool number_nonblank,
233 bool show_ends,
234 bool squeeze_blank)
236 /* Last character read from the input buffer. */
237 unsigned char ch;
239 /* Pointer to the next character in the input buffer. */
240 char *bpin;
242 /* Pointer to the first non-valid byte in the input buffer, i.e. the
243 current end of the buffer. */
244 char *eob;
246 /* Pointer to the position where the next character shall be written. */
247 char *bpout;
249 /* Number of characters read by the last read call. */
250 size_t n_read;
252 /* Determines how many consecutive newlines there have been in the
253 input. 0 newlines makes NEWLINES -1, 1 newline makes NEWLINES 1,
254 etc. Initially 0 to indicate that we are at the beginning of a
255 new line. The "state" of the procedure is determined by
256 NEWLINES. */
257 int newlines = newlines2;
259 #ifdef FIONREAD
260 /* If nonzero, use the FIONREAD ioctl, as an optimization.
261 (On Ultrix, it is not supported on NFS file systems.) */
262 bool use_fionread = true;
263 #endif
265 /* The inbuf pointers are initialized so that BPIN > EOB, and thereby input
266 is read immediately. */
268 eob = inbuf;
269 bpin = eob + 1;
271 bpout = outbuf;
273 while (true)
277 /* Write if there are at least OUTSIZE bytes in OUTBUF. */
279 if (outbuf + outsize <= bpout)
281 char *wp = outbuf;
282 size_t remaining_bytes;
285 if (full_write (STDOUT_FILENO, wp, outsize) != outsize)
286 error (EXIT_FAILURE, errno, _("write error"));
287 wp += outsize;
288 remaining_bytes = bpout - wp;
290 while (outsize <= remaining_bytes);
292 /* Move the remaining bytes to the beginning of the
293 buffer. */
295 memmove (outbuf, wp, remaining_bytes);
296 bpout = outbuf + remaining_bytes;
299 /* Is INBUF empty? */
301 if (bpin > eob)
303 bool input_pending = false;
304 #ifdef FIONREAD
305 int n_to_read = 0;
307 /* Is there any input to read immediately?
308 If not, we are about to wait,
309 so write all buffered output before waiting. */
311 if (use_fionread
312 && ioctl (input_desc, FIONREAD, &n_to_read) < 0)
314 /* Ultrix returns EOPNOTSUPP on NFS;
315 HP-UX returns ENOTTY on pipes.
316 SunOS returns EINVAL and
317 More/BSD returns ENODEV on special files
318 like /dev/null.
319 Irix-5 returns ENOSYS on pipes. */
320 if (errno == EOPNOTSUPP || errno == ENOTTY
321 || errno == EINVAL || errno == ENODEV
322 || errno == ENOSYS)
323 use_fionread = false;
324 else
326 error (0, errno, _("cannot do ioctl on %s"),
327 quote (infile));
328 newlines2 = newlines;
329 return false;
332 if (n_to_read != 0)
333 input_pending = true;
334 #endif
336 if (!input_pending)
337 write_pending (outbuf, &bpout);
339 /* Read more input into INBUF. */
341 n_read = safe_read (input_desc, inbuf, insize);
342 if (n_read == SAFE_READ_ERROR)
344 error (0, errno, "%s", infile);
345 write_pending (outbuf, &bpout);
346 newlines2 = newlines;
347 return false;
349 if (n_read == 0)
351 write_pending (outbuf, &bpout);
352 newlines2 = newlines;
353 return true;
356 /* Update the pointers and insert a sentinel at the buffer
357 end. */
359 bpin = inbuf;
360 eob = bpin + n_read;
361 *eob = '\n';
363 else
365 /* It was a real (not a sentinel) newline. */
367 /* Was the last line empty?
368 (i.e. have two or more consecutive newlines been read?) */
370 if (++newlines > 0)
372 if (newlines >= 2)
374 /* Limit this to 2 here. Otherwise, with lots of
375 consecutive newlines, the counter could wrap
376 around at INT_MAX. */
377 newlines = 2;
379 /* Are multiple adjacent empty lines to be substituted
380 by single ditto (-s), and this was the second empty
381 line? */
382 if (squeeze_blank)
384 ch = *bpin++;
385 continue;
389 /* Are line numbers to be written at empty lines (-n)? */
391 if (number && !number_nonblank)
393 next_line_num ();
394 bpout = stpcpy (bpout, line_num_print);
398 /* Output a currency symbol if requested (-e). */
400 if (show_ends)
401 *bpout++ = '$';
403 /* Output the newline. */
405 *bpout++ = '\n';
407 ch = *bpin++;
409 while (ch == '\n');
411 /* Are we at the beginning of a line, and line numbers are requested? */
413 if (newlines >= 0 && number)
415 next_line_num ();
416 bpout = stpcpy (bpout, line_num_print);
419 /* Here CH cannot contain a newline character. */
421 /* The loops below continue until a newline character is found,
422 which means that the buffer is empty or that a proper newline
423 has been found. */
425 /* If quoting, i.e. at least one of -v, -e, or -t specified,
426 scan for chars that need conversion. */
427 if (show_nonprinting)
429 while (true)
431 if (ch >= 32)
433 if (ch < 127)
434 *bpout++ = ch;
435 else if (ch == 127)
437 *bpout++ = '^';
438 *bpout++ = '?';
440 else
442 *bpout++ = 'M';
443 *bpout++ = '-';
444 if (ch >= 128 + 32)
446 if (ch < 128 + 127)
447 *bpout++ = ch - 128;
448 else
450 *bpout++ = '^';
451 *bpout++ = '?';
454 else
456 *bpout++ = '^';
457 *bpout++ = ch - 128 + 64;
461 else if (ch == '\t' && !show_tabs)
462 *bpout++ = '\t';
463 else if (ch == '\n')
465 newlines = -1;
466 break;
468 else
470 *bpout++ = '^';
471 *bpout++ = ch + 64;
474 ch = *bpin++;
477 else
479 /* Not quoting, neither of -v, -e, or -t specified. */
480 while (true)
482 if (ch == '\t' && show_tabs)
484 *bpout++ = '^';
485 *bpout++ = ch + 64;
487 else if (ch != '\n')
488 *bpout++ = ch;
489 else
491 newlines = -1;
492 break;
495 ch = *bpin++;
502 main (int argc, char **argv)
504 /* Optimal size of i/o operations of output. */
505 size_t outsize;
507 /* Optimal size of i/o operations of input. */
508 size_t insize;
510 size_t page_size = getpagesize ();
512 /* Pointer to the input buffer. */
513 char *inbuf;
515 /* Pointer to the output buffer. */
516 char *outbuf;
518 bool ok = true;
519 int c;
521 /* Index in argv to processed argument. */
522 int argind;
524 /* Device number of the output (file or whatever). */
525 dev_t out_dev;
527 /* I-node number of the output. */
528 ino_t out_ino;
530 /* True if the output file should not be the same as any input file. */
531 bool check_redirection = true;
533 /* Nonzero if we have ever read standard input. */
534 bool have_read_stdin = false;
536 struct stat stat_buf;
538 /* Variables that are set according to the specified options. */
539 bool number = false;
540 bool number_nonblank = false;
541 bool squeeze_blank = false;
542 bool show_ends = false;
543 bool show_nonprinting = false;
544 bool show_tabs = false;
545 int file_open_mode = O_RDONLY;
547 static struct option const long_options[] =
549 {"number-nonblank", no_argument, NULL, 'b'},
550 {"number", no_argument, NULL, 'n'},
551 {"squeeze-blank", no_argument, NULL, 's'},
552 {"show-nonprinting", no_argument, NULL, 'v'},
553 {"show-ends", no_argument, NULL, 'E'},
554 {"show-tabs", no_argument, NULL, 'T'},
555 {"show-all", no_argument, NULL, 'A'},
556 {GETOPT_HELP_OPTION_DECL},
557 {GETOPT_VERSION_OPTION_DECL},
558 {NULL, 0, NULL, 0}
561 initialize_main (&argc, &argv);
562 set_program_name (argv[0]);
563 setlocale (LC_ALL, "");
564 bindtextdomain (PACKAGE, LOCALEDIR);
565 textdomain (PACKAGE);
567 /* Arrange to close stdout if we exit via the
568 case_GETOPT_HELP_CHAR or case_GETOPT_VERSION_CHAR code.
569 Normally STDOUT_FILENO is used rather than stdout, so
570 close_stdout does nothing. */
571 atexit (close_stdout);
573 /* Parse command line options. */
575 while ((c = getopt_long (argc, argv, "benstuvAET", long_options, NULL))
576 != -1)
578 switch (c)
580 case 'b':
581 number = true;
582 number_nonblank = true;
583 break;
585 case 'e':
586 show_ends = true;
587 show_nonprinting = true;
588 break;
590 case 'n':
591 number = true;
592 break;
594 case 's':
595 squeeze_blank = true;
596 break;
598 case 't':
599 show_tabs = true;
600 show_nonprinting = true;
601 break;
603 case 'u':
604 /* We provide the -u feature unconditionally. */
605 break;
607 case 'v':
608 show_nonprinting = true;
609 break;
611 case 'A':
612 show_nonprinting = true;
613 show_ends = true;
614 show_tabs = true;
615 break;
617 case 'E':
618 show_ends = true;
619 break;
621 case 'T':
622 show_tabs = true;
623 break;
625 case_GETOPT_HELP_CHAR;
627 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
629 default:
630 usage (EXIT_FAILURE);
634 /* Get device, i-node number, and optimal blocksize of output. */
636 if (fstat (STDOUT_FILENO, &stat_buf) < 0)
637 error (EXIT_FAILURE, errno, _("standard output"));
639 outsize = io_blksize (stat_buf);
640 /* Input file can be output file for non-regular files.
641 fstat on pipes returns S_IFSOCK on some systems, S_IFIFO
642 on others, so the checking should not be done for those types,
643 and to allow things like cat < /dev/tty > /dev/tty, checking
644 is not done for device files either. */
646 if (S_ISREG (stat_buf.st_mode))
648 out_dev = stat_buf.st_dev;
649 out_ino = stat_buf.st_ino;
651 else
653 check_redirection = false;
654 #ifdef lint /* Suppress `used before initialized' warning. */
655 out_dev = 0;
656 out_ino = 0;
657 #endif
660 if (! (number || show_ends || squeeze_blank))
662 file_open_mode |= O_BINARY;
663 if (O_BINARY && ! isatty (STDOUT_FILENO))
664 xfreopen (NULL, "wb", stdout);
667 /* Check if any of the input files are the same as the output file. */
669 /* Main loop. */
671 infile = "-";
672 argind = optind;
676 if (argind < argc)
677 infile = argv[argind];
679 if (STREQ (infile, "-"))
681 have_read_stdin = true;
682 input_desc = STDIN_FILENO;
683 if ((file_open_mode & O_BINARY) && ! isatty (STDIN_FILENO))
684 xfreopen (NULL, "rb", stdin);
686 else
688 input_desc = open (infile, file_open_mode);
689 if (input_desc < 0)
691 error (0, errno, "%s", infile);
692 ok = false;
693 continue;
697 if (fstat (input_desc, &stat_buf) < 0)
699 error (0, errno, "%s", infile);
700 ok = false;
701 goto contin;
703 insize = io_blksize (stat_buf);
705 fdadvise (input_desc, 0, 0, FADVISE_SEQUENTIAL);
707 /* Compare the device and i-node numbers of this input file with
708 the corresponding values of the (output file associated with)
709 stdout, and skip this input file if they coincide. Input
710 files cannot be redirected to themselves. */
712 if (check_redirection
713 && stat_buf.st_dev == out_dev && stat_buf.st_ino == out_ino
714 && (input_desc != STDIN_FILENO))
716 error (0, 0, _("%s: input file is output file"), infile);
717 ok = false;
718 goto contin;
721 /* Select which version of `cat' to use. If any format-oriented
722 options were given use `cat'; otherwise use `simple_cat'. */
724 if (! (number || show_ends || show_nonprinting
725 || show_tabs || squeeze_blank))
727 insize = MAX (insize, outsize);
728 inbuf = xmalloc (insize + page_size - 1);
730 ok &= simple_cat (ptr_align (inbuf, page_size), insize);
732 else
734 inbuf = xmalloc (insize + 1 + page_size - 1);
736 /* Why are
737 (OUTSIZE - 1 + INSIZE * 4 + LINE_COUNTER_BUF_LEN + PAGE_SIZE - 1)
738 bytes allocated for the output buffer?
740 A test whether output needs to be written is done when the input
741 buffer empties or when a newline appears in the input. After
742 output is written, at most (OUTSIZE - 1) bytes will remain in the
743 buffer. Now INSIZE bytes of input is read. Each input character
744 may grow by a factor of 4 (by the prepending of M-^). If all
745 characters do, and no newlines appear in this block of input, we
746 will have at most (OUTSIZE - 1 + INSIZE * 4) bytes in the buffer.
747 If the last character in the preceding block of input was a
748 newline, a line number may be written (according to the given
749 options) as the first thing in the output buffer. (Done after the
750 new input is read, but before processing of the input begins.)
751 A line number requires seldom more than LINE_COUNTER_BUF_LEN
752 positions.
754 Align the output buffer to a page size boundary, for efficency on
755 some paging implementations, so add PAGE_SIZE - 1 bytes to the
756 request to make room for the alignment. */
758 outbuf = xmalloc (outsize - 1 + insize * 4 + LINE_COUNTER_BUF_LEN
759 + page_size - 1);
761 ok &= cat (ptr_align (inbuf, page_size), insize,
762 ptr_align (outbuf, page_size), outsize, show_nonprinting,
763 show_tabs, number, number_nonblank, show_ends,
764 squeeze_blank);
766 free (outbuf);
769 free (inbuf);
771 contin:
772 if (!STREQ (infile, "-") && close (input_desc) < 0)
774 error (0, errno, "%s", infile);
775 ok = false;
778 while (++argind < argc);
780 if (have_read_stdin && close (STDIN_FILENO) < 0)
781 error (EXIT_FAILURE, errno, _("closing standard input"));
783 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);