1 /* tac - concatenate and print files in reverse
2 Copyright (C) 1988, 1989, 1990, 1991, 1995 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Jay Lepreau (lepreau@cs.utah.edu).
19 GNU enhancements by David MacKenzie (djm@gnu.ai.mit.edu). */
21 /* Copy each FILE, or the standard input if none are given or when a
22 FILE name of "-" is encountered, to the standard output with the
23 order of the records reversed. The records are separated by
24 instances of a string, or a newline if none is given. By default, the
25 separator string is attached to the end of the record that it
29 -b, --before The separator is attached to the beginning
30 of the record that it precedes in the file.
31 -r, --regex The separator is a regular expression.
32 -s, --separator=separator Use SEPARATOR as the record separator.
34 To reverse a file byte by byte, use (in bash, ksh, or sh):
42 #include <sys/types.h>
54 #ifndef DEFAULT_TMPDIR
55 #define DEFAULT_TMPDIR "/tmp"
58 /* The number of bytes per atomic read. */
59 #define INITIAL_READSIZE 8192
61 /* The number of bytes per atomic write. */
62 #define WRITESIZE 8192
66 static char *xmalloc ();
67 static char *xrealloc ();
68 static void xwrite ();
73 /* The name this program was run with. */
76 /* The string that separates the records of the file. */
77 static char *separator;
79 /* If nonzero, print `separator' along with the record preceding it
80 in the file; otherwise with the record following it. */
81 static int separator_ends_record;
83 /* 0 if `separator' is to be matched as a regular expression;
84 otherwise, the length of `separator', used as a sentinel to
86 static int sentinel_length;
88 /* The length of a match with `separator'. If `sentinel_length' is 0,
89 `match_length' is computed every time a match succeeds;
90 otherwise, it is simply the length of `separator'. */
91 static int match_length;
93 /* The input buffer. */
96 /* The number of bytes to read at once into `buffer'. */
97 static unsigned read_size;
99 /* The size of `buffer'. This is read_size * 2 + sentinel_length + 2.
100 The extra 2 bytes allow `past_end' to have a value beyond the
101 end of `buffer' and `match_start' to run off the front of `buffer'. */
102 static unsigned buffer_size;
104 /* The compiled regular expression representing `separator'. */
105 static struct re_pattern_buffer compiled_separator;
107 /* The name of a temporary file containing a copy of pipe input. */
108 static char *tempfile;
110 /* If non-zero, display usage information and exit. */
111 static int show_help;
113 /* If non-zero, print the version on standard output then exit. */
114 static int show_version;
116 static struct option const longopts[] =
118 {"before", no_argument, &separator_ends_record, 0},
119 {"regex", no_argument, &sentinel_length, 0},
120 {"separator", required_argument, NULL, 's'},
121 {"help", no_argument, &show_help, 1},
122 {"version", no_argument, &show_version, 1},
131 fprintf (stderr, _("Try `%s --help' for more information.\n"),
136 Usage: %s [OPTION]... [FILE]...\n\
140 Write each FILE to standard output, last line first.\n\
141 With no FILE, or when FILE is -, read standard input.\n\
143 -b, --before attach the separator before instead of after\n\
144 -r, --regex interpret the separator as a regular expression\n\
145 -s, --separator=STRING use STRING as the separator instead of newline\n\
146 --help display this help and exit\n\
147 --version output version information and exit\n\
160 /* Allocate N bytes of memory dynamically, with error checking. */
171 error (0, 0, _("virtual memory exhausted"));
177 /* Change the size of memory area P to N bytes, with error checking. */
187 error (0, 0, _("virtual memory exhausted"));
194 xwrite (desc, buffer, size)
199 if (full_write (desc, buffer, size) < 0)
201 error (0, errno, _("write error"));
206 /* Print the characters from START to PAST_END - 1.
207 If START is NULL, just flush the buffer. */
210 output (start, past_end)
214 static char buffer[WRITESIZE];
215 static int bytes_in_buffer = 0;
216 int bytes_to_add = past_end - start;
217 int bytes_available = WRITESIZE - bytes_in_buffer;
221 xwrite (STDOUT_FILENO, buffer, bytes_in_buffer);
226 /* Write out as many full buffers as possible. */
227 while (bytes_to_add >= bytes_available)
229 memcpy (buffer + bytes_in_buffer, start, bytes_available);
230 bytes_to_add -= bytes_available;
231 start += bytes_available;
232 xwrite (STDOUT_FILENO, buffer, WRITESIZE);
234 bytes_available = WRITESIZE;
237 memcpy (buffer + bytes_in_buffer, start, bytes_to_add);
238 bytes_in_buffer += bytes_to_add;
241 /* Print in reverse the file open on descriptor FD for reading FILE.
242 Return 0 if ok, 1 if an error occurs. */
249 /* Pointer to the location in `buffer' where the search for
250 the next separator will begin. */
252 /* Pointer to one past the rightmost character in `buffer' that
253 has not been printed yet. */
255 unsigned saved_record_size; /* Length of the record growing in `buffer'. */
256 off_t file_pos; /* Offset in the file of the next read. */
257 /* Nonzero if `output' has not been called yet for any file.
258 Only used when the separator is attached to the preceding record. */
260 char first_char = *separator; /* Speed optimization, non-regexp. */
261 char *separator1 = separator + 1; /* Speed optimization, non-regexp. */
262 int match_length1 = match_length - 1; /* Speed optimization, non-regexp. */
263 struct re_registers regs;
265 /* Find the size of the input file. */
266 file_pos = lseek (fd, (off_t) 0, SEEK_END);
268 return 0; /* It's an empty file. */
270 /* Arrange for the first read to lop off enough to leave the rest of the
271 file a multiple of `read_size'. Since `read_size' can change, this may
272 not always hold during the program run, but since it usually will, leave
273 it here for i/o efficiency (page/sector boundaries and all that).
274 Note: the efficiency gain has not been verified. */
275 saved_record_size = file_pos % read_size;
276 if (saved_record_size == 0)
277 saved_record_size = read_size;
278 file_pos -= saved_record_size;
279 /* `file_pos' now points to the start of the last (probably partial) block
280 in the input file. */
282 lseek (fd, file_pos, SEEK_SET);
283 if (safe_read (fd, buffer, saved_record_size) != saved_record_size)
285 error (0, errno, "%s", file);
289 match_start = past_end = buffer + saved_record_size;
290 /* For non-regexp search, move past impossible positions for a match. */
292 match_start -= match_length1;
296 /* Search backward from `match_start' - 1 to `buffer' for a match
297 with `separator'; for speed, use strncmp if `separator' contains no
299 If the match succeeds, set `match_start' to point to the start of
300 the match and `match_length' to the length of the match.
301 Otherwise, make `match_start' < `buffer'. */
302 if (sentinel_length == 0)
304 int i = match_start - buffer;
307 ret = re_search (&compiled_separator, buffer, i, i - 1, -i, ®s);
309 match_start = buffer - 1;
312 error (0, 0, _("error in regular expression search"));
317 match_start = buffer + regs.start[0];
318 match_length = regs.end[0] - regs.start[0];
323 /* `match_length' is constant for non-regexp boundaries. */
324 while (*--match_start != first_char
325 || (match_length1 && strncmp (match_start + 1, separator1,
330 /* Check whether we backed off the front of `buffer' without finding
331 a match for `separator'. */
332 if (match_start < buffer)
336 /* Hit the beginning of the file; print the remaining record. */
337 output (buffer, past_end);
341 saved_record_size = past_end - buffer;
342 if (saved_record_size > read_size)
344 /* `buffer_size' is about twice `read_size', so since
345 we want to read in another `read_size' bytes before
346 the data already in `buffer', we need to increase
349 int offset = sentinel_length ? sentinel_length : 1;
352 buffer_size = read_size * 2 + sentinel_length + 2;
353 newbuffer = xrealloc (buffer - offset, buffer_size) + offset;
354 /* Adjust the pointers for the new buffer location. */
355 match_start += newbuffer - buffer;
356 past_end += newbuffer - buffer;
360 /* Back up to the start of the next bufferfull of the file. */
361 if (file_pos >= read_size)
362 file_pos -= read_size;
365 read_size = file_pos;
368 lseek (fd, file_pos, SEEK_SET);
370 /* Shift the pending record data right to make room for the new.
371 The source and destination regions probably overlap. */
372 memmove (buffer + read_size, buffer, saved_record_size);
373 past_end = buffer + read_size + saved_record_size;
374 /* For non-regexp searches, avoid unneccessary scanning. */
376 match_start = buffer + read_size;
378 match_start = past_end;
380 if (safe_read (fd, buffer, read_size) != read_size)
382 error (0, errno, "%s", file);
388 /* Found a match of `separator'. */
389 if (separator_ends_record)
391 char *match_end = match_start + match_length;
393 /* If this match of `separator' isn't at the end of the
394 file, print the record. */
395 if (first_time == 0 || match_end != past_end)
396 output (match_end, past_end);
397 past_end = match_end;
402 output (match_start, past_end);
403 past_end = match_start;
405 match_start -= match_length - 1;
410 /* Print FILE in reverse.
411 Return 0 if ok, 1 if an error occurs. */
419 fd = open (file, O_RDONLY);
422 error (0, errno, "%s", file);
425 errors = tac (fd, file);
428 error (0, errno, "%s", file);
434 /* Make a copy of the standard input in `tempfile'. */
439 static char *template = NULL;
440 static char *tempdir;
444 if (template == NULL)
446 tempdir = getenv ("TMPDIR");
448 tempdir = DEFAULT_TMPDIR;
449 template = xmalloc (strlen (tempdir) + 11);
451 sprintf (template, "%s/tacXXXXXX", tempdir);
452 tempfile = mktemp (template);
454 fd = creat (tempfile, 0600);
457 error (0, errno, "%s", tempfile);
460 while ((bytes_read = safe_read (0, buffer, read_size)) > 0)
461 if (full_write (fd, buffer, bytes_read) < 0)
463 error (0, errno, "%s", tempfile);
468 error (0, errno, "%s", tempfile);
471 if (bytes_read == -1)
473 error (0, errno, _("read error"));
478 /* Print the standard input in reverse, saving it to temporary
479 file `tempfile' first if it is a pipe.
480 Return 0 if ok, 1 if an error occurs. */
485 /* Previous values of signal handlers. */
486 RETSIGTYPE (*sigint) (), (*sighup) (), (*sigpipe) (), (*sigterm) ();
490 struct sigaction oldact, newact;
491 #endif /* SA_INTERRUPT */
493 /* No tempfile is needed for "tac < file".
494 Use fstat instead of checking for errno == ESPIPE because
495 lseek doesn't work on some special files but doesn't return an
497 if (fstat (0, &stats))
499 error (0, errno, _("standard input"));
502 if (S_ISREG (stats.st_mode))
503 return tac (0, _("standard input"));
506 newact.sa_handler = cleanup;
507 sigemptyset (&newact.sa_mask);
510 sigaction (SIGINT, NULL, &oldact);
511 sigint = oldact.sa_handler;
512 if (sigint != SIG_IGN)
513 sigaction (SIGINT, &newact, NULL);
515 sigaction (SIGHUP, NULL, &oldact);
516 sighup = oldact.sa_handler;
517 if (sighup != SIG_IGN)
518 sigaction (SIGHUP, &newact, NULL);
520 sigaction (SIGPIPE, NULL, &oldact);
521 sigpipe = oldact.sa_handler;
522 if (sigpipe != SIG_IGN)
523 sigaction (SIGPIPE, &newact, NULL);
525 sigaction (SIGTERM, NULL, &oldact);
526 sigterm = oldact.sa_handler;
527 if (sigterm != SIG_IGN)
528 sigaction (SIGTERM, &newact, NULL);
529 #else /* !SA_INTERRUPT */
530 sigint = signal (SIGINT, SIG_IGN);
531 if (sigint != SIG_IGN)
532 signal (SIGINT, cleanup);
534 sighup = signal (SIGHUP, SIG_IGN);
535 if (sighup != SIG_IGN)
536 signal (SIGHUP, cleanup);
538 sigpipe = signal (SIGPIPE, SIG_IGN);
539 if (sigpipe != SIG_IGN)
540 signal (SIGPIPE, cleanup);
542 sigterm = signal (SIGTERM, SIG_IGN);
543 if (sigterm != SIG_IGN)
544 signal (SIGTERM, cleanup);
545 #endif /* SA_INTERRUPT */
549 errors = tac_file (tempfile);
554 newact.sa_handler = sigint;
555 sigaction (SIGINT, &newact, NULL);
556 newact.sa_handler = sighup;
557 sigaction (SIGHUP, &newact, NULL);
558 newact.sa_handler = sigterm;
559 sigaction (SIGTERM, &newact, NULL);
560 newact.sa_handler = sigpipe;
561 sigaction (SIGPIPE, &newact, NULL);
562 #else /* !SA_INTERRUPT */
563 signal (SIGINT, sigint);
564 signal (SIGHUP, sighup);
565 signal (SIGTERM, sigterm);
566 signal (SIGPIPE, sigpipe);
567 #endif /* SA_INTERRUPT */
577 const char *error_message; /* Return value from re_compile_pattern. */
579 int have_read_stdin = 0;
581 program_name = argv[0];
585 separator_ends_record = 1;
587 while ((optc = getopt_long (argc, argv, "brs:", longopts, (int *) 0))
595 separator_ends_record = 0;
603 error (1, 0, _("separator cannot be empty"));
612 printf ("tac - %s\n", version_string);
619 if (sentinel_length == 0)
621 compiled_separator.allocated = 100;
622 compiled_separator.buffer = (unsigned char *)
623 xmalloc (compiled_separator.allocated);
624 compiled_separator.fastmap = xmalloc (256);
625 compiled_separator.translate = 0;
626 error_message = re_compile_pattern (separator, strlen (separator),
627 &compiled_separator);
629 error (1, 0, "%s", error_message);
632 match_length = sentinel_length = strlen (separator);
634 read_size = INITIAL_READSIZE;
635 /* A precaution that will probably never be needed. */
636 while (sentinel_length * 2 >= read_size)
638 buffer_size = read_size * 2 + sentinel_length + 2;
639 buffer = xmalloc (buffer_size);
642 strcpy (buffer, separator);
643 buffer += sentinel_length;
651 errors = tac_stdin ();
654 for (; optind < argc; ++optind)
656 if (strcmp (argv[optind], "-") == 0)
659 errors |= tac_stdin ();
662 errors |= tac_file (argv[optind]);
665 /* Flush the output buffer. */
666 output ((char *) NULL, (char *) NULL);
668 if (have_read_stdin && close (0) < 0)
669 error (1, errno, "-");
671 error (1, errno, _("write error"));