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 RETSIGTYPE
cleanup ();
68 static int tac_file ();
69 static int tac_stdin ();
70 static char *xmalloc ();
71 static char *xrealloc ();
72 static void output ();
73 static void save_stdin ();
74 static void xwrite ();
79 /* The name this program was run with. */
82 /* The string that separates the records of the file. */
83 static char *separator
;
85 /* If nonzero, print `separator' along with the record preceding it
86 in the file; otherwise with the record following it. */
87 static int separator_ends_record
;
89 /* 0 if `separator' is to be matched as a regular expression;
90 otherwise, the length of `separator', used as a sentinel to
92 static int sentinel_length
;
94 /* The length of a match with `separator'. If `sentinel_length' is 0,
95 `match_length' is computed every time a match succeeds;
96 otherwise, it is simply the length of `separator'. */
97 static int match_length
;
99 /* The input buffer. */
102 /* The number of bytes to read at once into `buffer'. */
103 static unsigned read_size
;
105 /* The size of `buffer'. This is read_size * 2 + sentinel_length + 2.
106 The extra 2 bytes allow `past_end' to have a value beyond the
107 end of `buffer' and `match_start' to run off the front of `buffer'. */
108 static unsigned buffer_size
;
110 /* The compiled regular expression representing `separator'. */
111 static struct re_pattern_buffer compiled_separator
;
113 /* If non-zero, display usage information and exit. */
114 static int show_help
;
116 /* If non-zero, print the version on standard output then exit. */
117 static int show_version
;
119 static struct option
const longopts
[] =
121 {"before", no_argument
, &separator_ends_record
, 0},
122 {"regex", no_argument
, &sentinel_length
, 0},
123 {"separator", required_argument
, NULL
, 's'},
124 {"help", no_argument
, &show_help
, 1},
125 {"version", no_argument
, &show_version
, 1},
134 fprintf (stderr
, "Try `%s --help' for more information.\n",
139 Usage: %s [OPTION]... [FILE]...\n\
143 Write each FILE to standard output, last line first.\n\
144 With no FILE, or when FILE is -, read standard input.\n\
146 -b, --before attach the separator before instead of after\n\
147 -r, --regex interpret the separator as a regular expression\n\
148 -s, --separator=STRING use STRING as the separator instead of newline\n\
149 --help display this help and exit\n\
150 --version output version information and exit\n\
161 const char *error_message
; /* Return value from re_compile_pattern. */
163 int have_read_stdin
= 0;
165 program_name
= argv
[0];
169 separator_ends_record
= 1;
171 while ((optc
= getopt_long (argc
, argv
, "brs:", longopts
, (int *) 0))
179 separator_ends_record
= 0;
187 error (1, 0, "separator cannot be empty");
196 printf ("tac - %s\n", version_string
);
203 if (sentinel_length
== 0)
205 compiled_separator
.allocated
= 100;
206 compiled_separator
.buffer
= (unsigned char *)
207 xmalloc (compiled_separator
.allocated
);
208 compiled_separator
.fastmap
= xmalloc (256);
209 compiled_separator
.translate
= 0;
210 error_message
= re_compile_pattern (separator
, strlen (separator
),
211 &compiled_separator
);
213 error (1, 0, "%s", error_message
);
216 match_length
= sentinel_length
= strlen (separator
);
218 read_size
= INITIAL_READSIZE
;
219 /* A precaution that will probably never be needed. */
220 while (sentinel_length
* 2 >= read_size
)
222 buffer_size
= read_size
* 2 + sentinel_length
+ 2;
223 buffer
= xmalloc (buffer_size
);
226 strcpy (buffer
, separator
);
227 buffer
+= sentinel_length
;
235 errors
= tac_stdin ();
238 for (; optind
< argc
; ++optind
)
240 if (strcmp (argv
[optind
], "-") == 0)
243 errors
|= tac_stdin ();
246 errors
|= tac_file (argv
[optind
]);
249 /* Flush the output buffer. */
250 output ((char *) NULL
, (char *) NULL
);
252 if (have_read_stdin
&& close (0) < 0)
253 error (1, errno
, "-");
255 error (1, errno
, "write error");
259 /* The name of a temporary file containing a copy of pipe input. */
262 /* Print the standard input in reverse, saving it to temporary
263 file `tempfile' first if it is a pipe.
264 Return 0 if ok, 1 if an error occurs. */
269 /* Previous values of signal handlers. */
270 RETSIGTYPE (*sigint
) (), (*sighup
) (), (*sigpipe
) (), (*sigterm
) ();
273 #ifdef _POSIX_VERSION
274 struct sigaction oldact
, newact
;
275 #endif /* _POSIX_VERSION */
277 /* No tempfile is needed for "tac < file".
278 Use fstat instead of checking for errno == ESPIPE because
279 lseek doesn't work on some special files but doesn't return an
281 if (fstat (0, &stats
))
283 error (0, errno
, "standard input");
286 if (S_ISREG (stats
.st_mode
))
287 return tac (0, "standard input");
289 #ifdef _POSIX_VERSION
290 newact
.sa_handler
= cleanup
;
291 sigemptyset (&newact
.sa_mask
);
294 sigaction (SIGINT
, NULL
, &oldact
);
295 sigint
= oldact
.sa_handler
;
296 if (sigint
!= SIG_IGN
)
297 sigaction (SIGINT
, &newact
, NULL
);
299 sigaction (SIGHUP
, NULL
, &oldact
);
300 sighup
= oldact
.sa_handler
;
301 if (sighup
!= SIG_IGN
)
302 sigaction (SIGHUP
, &newact
, NULL
);
304 sigaction (SIGPIPE
, NULL
, &oldact
);
305 sigpipe
= oldact
.sa_handler
;
306 if (sigpipe
!= SIG_IGN
)
307 sigaction (SIGPIPE
, &newact
, NULL
);
309 sigaction (SIGTERM
, NULL
, &oldact
);
310 sigterm
= oldact
.sa_handler
;
311 if (sigterm
!= SIG_IGN
)
312 sigaction (SIGTERM
, &newact
, NULL
);
313 #else /* !_POSIX_VERSION */
314 sigint
= signal (SIGINT
, SIG_IGN
);
315 if (sigint
!= SIG_IGN
)
316 signal (SIGINT
, cleanup
);
318 sighup
= signal (SIGHUP
, SIG_IGN
);
319 if (sighup
!= SIG_IGN
)
320 signal (SIGHUP
, cleanup
);
322 sigpipe
= signal (SIGPIPE
, SIG_IGN
);
323 if (sigpipe
!= SIG_IGN
)
324 signal (SIGPIPE
, cleanup
);
326 sigterm
= signal (SIGTERM
, SIG_IGN
);
327 if (sigterm
!= SIG_IGN
)
328 signal (SIGTERM
, cleanup
);
329 #endif /* _POSIX_VERSION */
333 errors
= tac_file (tempfile
);
337 #ifdef _POSIX_VERSION
338 newact
.sa_handler
= sigint
;
339 sigaction (SIGINT
, &newact
, NULL
);
340 newact
.sa_handler
= sighup
;
341 sigaction (SIGHUP
, &newact
, NULL
);
342 newact
.sa_handler
= sigterm
;
343 sigaction (SIGTERM
, &newact
, NULL
);
344 newact
.sa_handler
= sigpipe
;
345 sigaction (SIGPIPE
, &newact
, NULL
);
346 #else /* !_POSIX_VERSION */
347 signal (SIGINT
, sigint
);
348 signal (SIGHUP
, sighup
);
349 signal (SIGTERM
, sigterm
);
350 signal (SIGPIPE
, sigpipe
);
351 #endif /* _POSIX_VERSION */
356 /* Make a copy of the standard input in `tempfile'. */
361 static char *template = NULL
;
362 static char *tempdir
;
366 if (template == NULL
)
368 tempdir
= getenv ("TMPDIR");
370 tempdir
= DEFAULT_TMPDIR
;
371 template = xmalloc (strlen (tempdir
) + 11);
373 sprintf (template, "%s/tacXXXXXX", tempdir
);
374 tempfile
= mktemp (template);
376 fd
= creat (tempfile
, 0600);
379 error (0, errno
, "%s", tempfile
);
382 while ((bytes_read
= safe_read (0, buffer
, read_size
)) > 0)
383 if (full_write (fd
, buffer
, bytes_read
) < 0)
385 error (0, errno
, "%s", tempfile
);
390 error (0, errno
, "%s", tempfile
);
393 if (bytes_read
== -1)
395 error (0, errno
, "read error");
400 /* Print FILE in reverse.
401 Return 0 if ok, 1 if an error occurs. */
409 fd
= open (file
, O_RDONLY
);
412 error (0, errno
, "%s", file
);
415 errors
= tac (fd
, file
);
418 error (0, errno
, "%s", file
);
424 /* Print in reverse the file open on descriptor FD for reading FILE.
425 Return 0 if ok, 1 if an error occurs. */
432 /* Pointer to the location in `buffer' where the search for
433 the next separator will begin. */
435 /* Pointer to one past the rightmost character in `buffer' that
436 has not been printed yet. */
438 unsigned saved_record_size
; /* Length of the record growing in `buffer'. */
439 off_t file_pos
; /* Offset in the file of the next read. */
440 /* Nonzero if `output' has not been called yet for any file.
441 Only used when the separator is attached to the preceding record. */
443 char first_char
= *separator
; /* Speed optimization, non-regexp. */
444 char *separator1
= separator
+ 1; /* Speed optimization, non-regexp. */
445 int match_length1
= match_length
- 1; /* Speed optimization, non-regexp. */
446 struct re_registers regs
;
448 /* Find the size of the input file. */
449 file_pos
= lseek (fd
, (off_t
) 0, SEEK_END
);
451 return 0; /* It's an empty file. */
453 /* Arrange for the first read to lop off enough to leave the rest of the
454 file a multiple of `read_size'. Since `read_size' can change, this may
455 not always hold during the program run, but since it usually will, leave
456 it here for i/o efficiency (page/sector boundaries and all that).
457 Note: the efficiency gain has not been verified. */
458 saved_record_size
= file_pos
% read_size
;
459 if (saved_record_size
== 0)
460 saved_record_size
= read_size
;
461 file_pos
-= saved_record_size
;
462 /* `file_pos' now points to the start of the last (probably partial) block
463 in the input file. */
465 lseek (fd
, file_pos
, SEEK_SET
);
466 if (safe_read (fd
, buffer
, saved_record_size
) != saved_record_size
)
468 error (0, errno
, "%s", file
);
472 match_start
= past_end
= buffer
+ saved_record_size
;
473 /* For non-regexp search, move past impossible positions for a match. */
475 match_start
-= match_length1
;
479 /* Search backward from `match_start' - 1 to `buffer' for a match
480 with `separator'; for speed, use strncmp if `separator' contains no
482 If the match succeeds, set `match_start' to point to the start of
483 the match and `match_length' to the length of the match.
484 Otherwise, make `match_start' < `buffer'. */
485 if (sentinel_length
== 0)
487 int i
= match_start
- buffer
;
490 ret
= re_search (&compiled_separator
, buffer
, i
, i
- 1, -i
, ®s
);
492 match_start
= buffer
- 1;
495 error (0, 0, "error in regular expression search");
500 match_start
= buffer
+ regs
.start
[0];
501 match_length
= regs
.end
[0] - regs
.start
[0];
506 /* `match_length' is constant for non-regexp boundaries. */
507 while (*--match_start
!= first_char
508 || (match_length1
&& strncmp (match_start
+ 1, separator1
,
513 /* Check whether we backed off the front of `buffer' without finding
514 a match for `separator'. */
515 if (match_start
< buffer
)
519 /* Hit the beginning of the file; print the remaining record. */
520 output (buffer
, past_end
);
524 saved_record_size
= past_end
- buffer
;
525 if (saved_record_size
> read_size
)
527 /* `buffer_size' is about twice `read_size', so since
528 we want to read in another `read_size' bytes before
529 the data already in `buffer', we need to increase
532 int offset
= sentinel_length
? sentinel_length
: 1;
535 buffer_size
= read_size
* 2 + sentinel_length
+ 2;
536 newbuffer
= xrealloc (buffer
- offset
, buffer_size
) + offset
;
537 /* Adjust the pointers for the new buffer location. */
538 match_start
+= newbuffer
- buffer
;
539 past_end
+= newbuffer
- buffer
;
543 /* Back up to the start of the next bufferfull of the file. */
544 if (file_pos
>= read_size
)
545 file_pos
-= read_size
;
548 read_size
= file_pos
;
551 lseek (fd
, file_pos
, SEEK_SET
);
553 /* Shift the pending record data right to make room for the new.
554 The source and destination regions probably overlap. */
555 memmove (buffer
+ read_size
, buffer
, saved_record_size
);
556 past_end
= buffer
+ read_size
+ saved_record_size
;
557 /* For non-regexp searches, avoid unneccessary scanning. */
559 match_start
= buffer
+ read_size
;
561 match_start
= past_end
;
563 if (safe_read (fd
, buffer
, read_size
) != read_size
)
565 error (0, errno
, "%s", file
);
571 /* Found a match of `separator'. */
572 if (separator_ends_record
)
574 char *match_end
= match_start
+ match_length
;
576 /* If this match of `separator' isn't at the end of the
577 file, print the record. */
578 if (first_time
== 0 || match_end
!= past_end
)
579 output (match_end
, past_end
);
580 past_end
= match_end
;
585 output (match_start
, past_end
);
586 past_end
= match_start
;
588 match_start
-= match_length
- 1;
593 /* Print the characters from START to PAST_END - 1.
594 If START is NULL, just flush the buffer. */
597 output (start
, past_end
)
601 static char buffer
[WRITESIZE
];
602 static int bytes_in_buffer
= 0;
603 int bytes_to_add
= past_end
- start
;
604 int bytes_available
= WRITESIZE
- bytes_in_buffer
;
608 xwrite (STDOUT_FILENO
, buffer
, bytes_in_buffer
);
613 /* Write out as many full buffers as possible. */
614 while (bytes_to_add
>= bytes_available
)
616 memcpy (buffer
+ bytes_in_buffer
, start
, bytes_available
);
617 bytes_to_add
-= bytes_available
;
618 start
+= bytes_available
;
619 xwrite (STDOUT_FILENO
, buffer
, WRITESIZE
);
621 bytes_available
= WRITESIZE
;
624 memcpy (buffer
+ bytes_in_buffer
, start
, bytes_to_add
);
625 bytes_in_buffer
+= bytes_to_add
;
636 xwrite (desc
, buffer
, size
)
641 if (full_write (desc
, buffer
, size
) < 0)
643 error (0, errno
, "write error");
648 /* Allocate N bytes of memory dynamically, with error checking. */
659 error (0, 0, "virtual memory exhausted");
665 /* Change the size of memory area P to N bytes, with error checking. */
675 error (0, 0, "virtual memory exhausted");