8740e0e4ffe7a9af383cc0dfdf2b90f5b031b834
[platform/upstream/coreutils.git] / src / cat.c
1 /* cat -- concatenate files and print on the standard output.
2    Copyright (C) 1988, 1990-1991, 1995-2010 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.  */
16
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.
21
22    By tege@sics.se, Torbjorn Granlund, advised by rms, Richard Stallman.  */
23
24 #include <config.h>
25
26 #include <stdio.h>
27 #include <getopt.h>
28 #include <sys/types.h>
29
30 #if HAVE_STROPTS_H
31 # include <stropts.h>
32 #endif
33 #include <sys/ioctl.h>
34
35 #include "system.h"
36 #include "error.h"
37 #include "full-write.h"
38 #include "quote.h"
39 #include "safe-read.h"
40 #include "xfreopen.h"
41
42 /* The official name of this program (e.g., no `g' prefix).  */
43 #define PROGRAM_NAME "cat"
44
45 #define AUTHORS \
46   proper_name_utf8 ("Torbjorn Granlund", "Torbj\303\266rn Granlund"), \
47   proper_name ("Richard M. Stallman")
48
49 /* Name of input file.  May be "-".  */
50 static char const *infile;
51
52 /* Descriptor on which input file is open.  */
53 static int input_desc;
54
55 /* Buffer for line numbers.
56    An 11 digit counter may overflow within an hour on a P2/466,
57    an 18 digit counter needs about 1000y */
58 #define LINE_COUNTER_BUF_LEN 20
59 static char line_buf[LINE_COUNTER_BUF_LEN] =
60   {
61     ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
62     ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '0',
63     '\t', '\0'
64   };
65
66 /* Position in `line_buf' where printing starts.  This will not change
67    unless the number of lines is larger than 999999.  */
68 static char *line_num_print = line_buf + LINE_COUNTER_BUF_LEN - 8;
69
70 /* Position of the first digit in `line_buf'.  */
71 static char *line_num_start = line_buf + LINE_COUNTER_BUF_LEN - 3;
72
73 /* Position of the last digit in `line_buf'.  */
74 static char *line_num_end = line_buf + LINE_COUNTER_BUF_LEN - 3;
75
76 /* Preserves the `cat' function's local `newlines' between invocations.  */
77 static int newlines2 = 0;
78
79 void
80 usage (int status)
81 {
82   if (status != EXIT_SUCCESS)
83     fprintf (stderr, _("Try `%s --help' for more information.\n"),
84              program_name);
85   else
86     {
87       printf (_("\
88 Usage: %s [OPTION]... [FILE]...\n\
89 "),
90               program_name);
91       fputs (_("\
92 Concatenate FILE(s), or standard input, to standard output.\n\
93 \n\
94   -A, --show-all           equivalent to -vET\n\
95   -b, --number-nonblank    number nonempty output lines\n\
96   -e                       equivalent to -vE\n\
97   -E, --show-ends          display $ at end of each line\n\
98   -n, --number             number all output lines\n\
99   -s, --squeeze-blank      suppress repeated empty output lines\n\
100 "), stdout);
101       fputs (_("\
102   -t                       equivalent to -vT\n\
103   -T, --show-tabs          display TAB characters as ^I\n\
104   -u                       (ignored)\n\
105   -v, --show-nonprinting   use ^ and M- notation, except for LFD and TAB\n\
106 "), stdout);
107       fputs (HELP_OPTION_DESCRIPTION, stdout);
108       fputs (VERSION_OPTION_DESCRIPTION, stdout);
109       fputs (_("\
110 \n\
111 With no FILE, or when FILE is -, read standard input.\n\
112 "), stdout);
113       printf (_("\
114 \n\
115 Examples:\n\
116   %s f - g  Output f's contents, then standard input, then g's contents.\n\
117   %s        Copy standard input to standard output.\n\
118 "),
119               program_name, program_name);
120       emit_ancillary_info ();
121     }
122   exit (status);
123 }
124
125 /* Compute the next line number.  */
126
127 static void
128 next_line_num (void)
129 {
130   char *endp = line_num_end;
131   do
132     {
133       if ((*endp)++ < '9')
134         return;
135       *endp-- = '0';
136     }
137   while (endp >= line_num_start);
138   if (line_num_start > line_buf)
139     *--line_num_start = '1';
140   else
141     *line_buf = '>';
142   if (line_num_start < line_num_print)
143     line_num_print--;
144 }
145
146 /* Plain cat.  Copies the file behind `input_desc' to STDOUT_FILENO.
147    Return true if successful.  */
148
149 static bool
150 simple_cat (
151      /* Pointer to the buffer, used by reads and writes.  */
152      char *buf,
153
154      /* Number of characters preferably read or written by each read and write
155         call.  */
156      size_t bufsize)
157 {
158   /* Actual number of characters read, and therefore written.  */
159   size_t n_read;
160
161   /* Loop until the end of the file.  */
162
163   for (;;)
164     {
165       /* Read a block of input.  */
166
167       n_read = safe_read (input_desc, buf, bufsize);
168       if (n_read == SAFE_READ_ERROR)
169         {
170           error (0, errno, "%s", infile);
171           return false;
172         }
173
174       /* End of this file?  */
175
176       if (n_read == 0)
177         return true;
178
179       /* Write this block out.  */
180
181       {
182         /* The following is ok, since we know that 0 < n_read.  */
183         size_t n = n_read;
184         if (full_write (STDOUT_FILENO, buf, n) != n)
185           error (EXIT_FAILURE, errno, _("write error"));
186       }
187     }
188 }
189
190 /* Write any pending output to STDOUT_FILENO.
191    Pending is defined to be the *BPOUT - OUTBUF bytes starting at OUTBUF.
192    Then set *BPOUT to OUTPUT if it's not already that value.  */
193
194 static inline void
195 write_pending (char *outbuf, char **bpout)
196 {
197   size_t n_write = *bpout - outbuf;
198   if (0 < n_write)
199     {
200       if (full_write (STDOUT_FILENO, outbuf, n_write) != n_write)
201         error (EXIT_FAILURE, errno, _("write error"));
202       *bpout = outbuf;
203     }
204 }
205
206 /* Cat the file behind INPUT_DESC to the file behind OUTPUT_DESC.
207    Return true if successful.
208    Called if any option more than -u was specified.
209
210    A newline character is always put at the end of the buffer, to make
211    an explicit test for buffer end unnecessary.  */
212
213 static bool
214 cat (
215      /* Pointer to the beginning of the input buffer.  */
216      char *inbuf,
217
218      /* Number of characters read in each read call.  */
219      size_t insize,
220
221      /* Pointer to the beginning of the output buffer.  */
222      char *outbuf,
223
224      /* Number of characters written by each write call.  */
225      size_t outsize,
226
227      /* Variables that have values according to the specified options.  */
228      bool show_nonprinting,
229      bool show_tabs,
230      bool number,
231      bool number_nonblank,
232      bool show_ends,
233      bool squeeze_blank)
234 {
235   /* Last character read from the input buffer.  */
236   unsigned char ch;
237
238   /* Pointer to the next character in the input buffer.  */
239   char *bpin;
240
241   /* Pointer to the first non-valid byte in the input buffer, i.e. the
242      current end of the buffer.  */
243   char *eob;
244
245   /* Pointer to the position where the next character shall be written.  */
246   char *bpout;
247
248   /* Number of characters read by the last read call.  */
249   size_t n_read;
250
251   /* Determines how many consecutive newlines there have been in the
252      input.  0 newlines makes NEWLINES -1, 1 newline makes NEWLINES 1,
253      etc.  Initially 0 to indicate that we are at the beginning of a
254      new line.  The "state" of the procedure is determined by
255      NEWLINES.  */
256   int newlines = newlines2;
257
258 #ifdef FIONREAD
259   /* If nonzero, use the FIONREAD ioctl, as an optimization.
260      (On Ultrix, it is not supported on NFS file systems.)  */
261   bool use_fionread = true;
262 #endif
263
264   /* The inbuf pointers are initialized so that BPIN > EOB, and thereby input
265      is read immediately.  */
266
267   eob = inbuf;
268   bpin = eob + 1;
269
270   bpout = outbuf;
271
272   for (;;)
273     {
274       do
275         {
276           /* Write if there are at least OUTSIZE bytes in OUTBUF.  */
277
278           if (outbuf + outsize <= bpout)
279             {
280               char *wp = outbuf;
281               size_t remaining_bytes;
282               do
283                 {
284                   if (full_write (STDOUT_FILENO, wp, outsize) != outsize)
285                     error (EXIT_FAILURE, errno, _("write error"));
286                   wp += outsize;
287                   remaining_bytes = bpout - wp;
288                 }
289               while (outsize <= remaining_bytes);
290
291               /* Move the remaining bytes to the beginning of the
292                  buffer.  */
293
294               memmove (outbuf, wp, remaining_bytes);
295               bpout = outbuf + remaining_bytes;
296             }
297
298           /* Is INBUF empty?  */
299
300           if (bpin > eob)
301             {
302               bool input_pending = false;
303 #ifdef FIONREAD
304               int n_to_read = 0;
305
306               /* Is there any input to read immediately?
307                  If not, we are about to wait,
308                  so write all buffered output before waiting.  */
309
310               if (use_fionread
311                   && ioctl (input_desc, FIONREAD, &n_to_read) < 0)
312                 {
313                   /* Ultrix returns EOPNOTSUPP on NFS;
314                      HP-UX returns ENOTTY on pipes.
315                      SunOS returns EINVAL and
316                      More/BSD returns ENODEV on special files
317                      like /dev/null.
318                      Irix-5 returns ENOSYS on pipes.  */
319                   if (errno == EOPNOTSUPP || errno == ENOTTY
320                       || errno == EINVAL || errno == ENODEV
321                       || errno == ENOSYS)
322                     use_fionread = false;
323                   else
324                     {
325                       error (0, errno, _("cannot do ioctl on %s"), quote (infile));
326                       newlines2 = newlines;
327                       return false;
328                     }
329                 }
330               if (n_to_read != 0)
331                 input_pending = true;
332 #endif
333
334               if (!input_pending)
335                 write_pending (outbuf, &bpout);
336
337               /* Read more input into INBUF.  */
338
339               n_read = safe_read (input_desc, inbuf, insize);
340               if (n_read == SAFE_READ_ERROR)
341                 {
342                   error (0, errno, "%s", infile);
343                   write_pending (outbuf, &bpout);
344                   newlines2 = newlines;
345                   return false;
346                 }
347               if (n_read == 0)
348                 {
349                   write_pending (outbuf, &bpout);
350                   newlines2 = newlines;
351                   return true;
352                 }
353
354               /* Update the pointers and insert a sentinel at the buffer
355                  end.  */
356
357               bpin = inbuf;
358               eob = bpin + n_read;
359               *eob = '\n';
360             }
361           else
362             {
363               /* It was a real (not a sentinel) newline.  */
364
365               /* Was the last line empty?
366                  (i.e. have two or more consecutive newlines been read?)  */
367
368               if (++newlines > 0)
369                 {
370                   if (newlines >= 2)
371                     {
372                       /* Limit this to 2 here.  Otherwise, with lots of
373                          consecutive newlines, the counter could wrap
374                          around at INT_MAX.  */
375                       newlines = 2;
376
377                       /* Are multiple adjacent empty lines to be substituted
378                          by single ditto (-s), and this was the second empty
379                          line?  */
380                       if (squeeze_blank)
381                         {
382                           ch = *bpin++;
383                           continue;
384                         }
385                     }
386
387                   /* Are line numbers to be written at empty lines (-n)?  */
388
389                   if (number && !number_nonblank)
390                     {
391                       next_line_num ();
392                       bpout = stpcpy (bpout, line_num_print);
393                     }
394                 }
395
396               /* Output a currency symbol if requested (-e).  */
397
398               if (show_ends)
399                 *bpout++ = '$';
400
401               /* Output the newline.  */
402
403               *bpout++ = '\n';
404             }
405           ch = *bpin++;
406         }
407       while (ch == '\n');
408
409       /* Are we at the beginning of a line, and line numbers are requested?  */
410
411       if (newlines >= 0 && number)
412         {
413           next_line_num ();
414           bpout = stpcpy (bpout, line_num_print);
415         }
416
417       /* Here CH cannot contain a newline character.  */
418
419       /* The loops below continue until a newline character is found,
420          which means that the buffer is empty or that a proper newline
421          has been found.  */
422
423       /* If quoting, i.e. at least one of -v, -e, or -t specified,
424          scan for chars that need conversion.  */
425       if (show_nonprinting)
426         {
427           for (;;)
428             {
429               if (ch >= 32)
430                 {
431                   if (ch < 127)
432                     *bpout++ = ch;
433                   else if (ch == 127)
434                     {
435                       *bpout++ = '^';
436                       *bpout++ = '?';
437                     }
438                   else
439                     {
440                       *bpout++ = 'M';
441                       *bpout++ = '-';
442                       if (ch >= 128 + 32)
443                         {
444                           if (ch < 128 + 127)
445                             *bpout++ = ch - 128;
446                           else
447                             {
448                               *bpout++ = '^';
449                               *bpout++ = '?';
450                             }
451                         }
452                       else
453                         {
454                           *bpout++ = '^';
455                           *bpout++ = ch - 128 + 64;
456                         }
457                     }
458                 }
459               else if (ch == '\t' && !show_tabs)
460                 *bpout++ = '\t';
461               else if (ch == '\n')
462                 {
463                   newlines = -1;
464                   break;
465                 }
466               else
467                 {
468                   *bpout++ = '^';
469                   *bpout++ = ch + 64;
470                 }
471
472               ch = *bpin++;
473             }
474         }
475       else
476         {
477           /* Not quoting, neither of -v, -e, or -t specified.  */
478           for (;;)
479             {
480               if (ch == '\t' && show_tabs)
481                 {
482                   *bpout++ = '^';
483                   *bpout++ = ch + 64;
484                 }
485               else if (ch != '\n')
486                 *bpout++ = ch;
487               else
488                 {
489                   newlines = -1;
490                   break;
491                 }
492
493               ch = *bpin++;
494             }
495         }
496     }
497 }
498
499 int
500 main (int argc, char **argv)
501 {
502   /* Optimal size of i/o operations of output.  */
503   size_t outsize;
504
505   /* Optimal size of i/o operations of input.  */
506   size_t insize;
507
508   size_t page_size = getpagesize ();
509
510   /* Pointer to the input buffer.  */
511   char *inbuf;
512
513   /* Pointer to the output buffer.  */
514   char *outbuf;
515
516   bool ok = true;
517   int c;
518
519   /* Index in argv to processed argument.  */
520   int argind;
521
522   /* Device number of the output (file or whatever).  */
523   dev_t out_dev;
524
525   /* I-node number of the output.  */
526   ino_t out_ino;
527
528   /* True if the output file should not be the same as any input file.  */
529   bool check_redirection = true;
530
531   /* Nonzero if we have ever read standard input.  */
532   bool have_read_stdin = false;
533
534   struct stat stat_buf;
535
536   /* Variables that are set according to the specified options.  */
537   bool number = false;
538   bool number_nonblank = false;
539   bool squeeze_blank = false;
540   bool show_ends = false;
541   bool show_nonprinting = false;
542   bool show_tabs = false;
543   int file_open_mode = O_RDONLY;
544
545   static struct option const long_options[] =
546   {
547     {"number-nonblank", no_argument, NULL, 'b'},
548     {"number", no_argument, NULL, 'n'},
549     {"squeeze-blank", no_argument, NULL, 's'},
550     {"show-nonprinting", no_argument, NULL, 'v'},
551     {"show-ends", no_argument, NULL, 'E'},
552     {"show-tabs", no_argument, NULL, 'T'},
553     {"show-all", no_argument, NULL, 'A'},
554     {GETOPT_HELP_OPTION_DECL},
555     {GETOPT_VERSION_OPTION_DECL},
556     {NULL, 0, NULL, 0}
557   };
558
559   initialize_main (&argc, &argv);
560   set_program_name (argv[0]);
561   setlocale (LC_ALL, "");
562   bindtextdomain (PACKAGE, LOCALEDIR);
563   textdomain (PACKAGE);
564
565   /* Arrange to close stdout if we exit via the
566      case_GETOPT_HELP_CHAR or case_GETOPT_VERSION_CHAR code.
567      Normally STDOUT_FILENO is used rather than stdout, so
568      close_stdout does nothing.  */
569   atexit (close_stdout);
570
571   /* Parse command line options.  */
572
573   while ((c = getopt_long (argc, argv, "benstuvAET", long_options, NULL))
574          != -1)
575     {
576       switch (c)
577         {
578         case 'b':
579           number = true;
580           number_nonblank = true;
581           break;
582
583         case 'e':
584           show_ends = true;
585           show_nonprinting = true;
586           break;
587
588         case 'n':
589           number = true;
590           break;
591
592         case 's':
593           squeeze_blank = true;
594           break;
595
596         case 't':
597           show_tabs = true;
598           show_nonprinting = true;
599           break;
600
601         case 'u':
602           /* We provide the -u feature unconditionally.  */
603           break;
604
605         case 'v':
606           show_nonprinting = true;
607           break;
608
609         case 'A':
610           show_nonprinting = true;
611           show_ends = true;
612           show_tabs = true;
613           break;
614
615         case 'E':
616           show_ends = true;
617           break;
618
619         case 'T':
620           show_tabs = true;
621           break;
622
623         case_GETOPT_HELP_CHAR;
624
625         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
626
627         default:
628           usage (EXIT_FAILURE);
629         }
630     }
631
632   /* Get device, i-node number, and optimal blocksize of output.  */
633
634   if (fstat (STDOUT_FILENO, &stat_buf) < 0)
635     error (EXIT_FAILURE, errno, _("standard output"));
636
637   outsize = io_blksize (stat_buf);
638   /* Input file can be output file for non-regular files.
639      fstat on pipes returns S_IFSOCK on some systems, S_IFIFO
640      on others, so the checking should not be done for those types,
641      and to allow things like cat < /dev/tty > /dev/tty, checking
642      is not done for device files either.  */
643
644   if (S_ISREG (stat_buf.st_mode))
645     {
646       out_dev = stat_buf.st_dev;
647       out_ino = stat_buf.st_ino;
648     }
649   else
650     {
651       check_redirection = false;
652 #ifdef lint  /* Suppress `used before initialized' warning.  */
653       out_dev = 0;
654       out_ino = 0;
655 #endif
656     }
657
658   if (! (number || show_ends || squeeze_blank))
659     {
660       file_open_mode |= O_BINARY;
661       if (O_BINARY && ! isatty (STDOUT_FILENO))
662         xfreopen (NULL, "wb", stdout);
663     }
664
665   /* Check if any of the input files are the same as the output file.  */
666
667   /* Main loop.  */
668
669   infile = "-";
670   argind = optind;
671
672   do
673     {
674       if (argind < argc)
675         infile = argv[argind];
676
677       if (STREQ (infile, "-"))
678         {
679           have_read_stdin = true;
680           input_desc = STDIN_FILENO;
681           if ((file_open_mode & O_BINARY) && ! isatty (STDIN_FILENO))
682             xfreopen (NULL, "rb", stdin);
683         }
684       else
685         {
686           input_desc = open (infile, file_open_mode);
687           if (input_desc < 0)
688             {
689               error (0, errno, "%s", infile);
690               ok = false;
691               continue;
692             }
693         }
694
695       if (fstat (input_desc, &stat_buf) < 0)
696         {
697           error (0, errno, "%s", infile);
698           ok = false;
699           goto contin;
700         }
701       insize = io_blksize (stat_buf);
702
703       /* Compare the device and i-node numbers of this input file with
704          the corresponding values of the (output file associated with)
705          stdout, and skip this input file if they coincide.  Input
706          files cannot be redirected to themselves.  */
707
708       if (check_redirection
709           && stat_buf.st_dev == out_dev && stat_buf.st_ino == out_ino
710           && (input_desc != STDIN_FILENO))
711         {
712           error (0, 0, _("%s: input file is output file"), infile);
713           ok = false;
714           goto contin;
715         }
716
717       /* Select which version of `cat' to use.  If any format-oriented
718          options were given use `cat'; otherwise use `simple_cat'.  */
719
720       if (! (number || show_ends || show_nonprinting
721              || show_tabs || squeeze_blank))
722         {
723           insize = MAX (insize, outsize);
724           inbuf = xmalloc (insize + page_size - 1);
725
726           ok &= simple_cat (ptr_align (inbuf, page_size), insize);
727         }
728       else
729         {
730           inbuf = xmalloc (insize + 1 + page_size - 1);
731
732           /* Why are
733              (OUTSIZE - 1 + INSIZE * 4 + LINE_COUNTER_BUF_LEN + PAGE_SIZE - 1)
734              bytes allocated for the output buffer?
735
736              A test whether output needs to be written is done when the input
737              buffer empties or when a newline appears in the input.  After
738              output is written, at most (OUTSIZE - 1) bytes will remain in the
739              buffer.  Now INSIZE bytes of input is read.  Each input character
740              may grow by a factor of 4 (by the prepending of M-^).  If all
741              characters do, and no newlines appear in this block of input, we
742              will have at most (OUTSIZE - 1 + INSIZE * 4) bytes in the buffer.
743              If the last character in the preceding block of input was a
744              newline, a line number may be written (according to the given
745              options) as the first thing in the output buffer. (Done after the
746              new input is read, but before processing of the input begins.)
747              A line number requires seldom more than LINE_COUNTER_BUF_LEN
748              positions.
749
750              Align the output buffer to a page size boundary, for efficency on
751              some paging implementations, so add PAGE_SIZE - 1 bytes to the
752              request to make room for the alignment.  */
753
754           outbuf = xmalloc (outsize - 1 + insize * 4 + LINE_COUNTER_BUF_LEN
755                             + page_size - 1);
756
757           ok &= cat (ptr_align (inbuf, page_size), insize,
758                      ptr_align (outbuf, page_size), outsize, show_nonprinting,
759                      show_tabs, number, number_nonblank, show_ends,
760                      squeeze_blank);
761
762           free (outbuf);
763         }
764
765       free (inbuf);
766
767     contin:
768       if (!STREQ (infile, "-") && close (input_desc) < 0)
769         {
770           error (0, errno, "%s", infile);
771           ok = false;
772         }
773     }
774   while (++argind < argc);
775
776   if (have_read_stdin && close (STDIN_FILENO) < 0)
777     error (EXIT_FAILURE, errno, _("closing standard input"));
778
779   exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
780 }