1 /* vi: set sw=4 ts=4: */
3 * tac implementation for busybox
5 * Copyright (C) 2003 Yang Xiaopeng <yxp at hanwang.com.cn>
6 * Copyright (C) 2007 Natanael Copa <natanael.copa@gmail.com>
7 * Copyright (C) 2007 Tito Ragusa <farmatito@tiscali.it>
9 * Licensed under GPLv2, see file LICENSE in this source tree.
13 /* tac - concatenate and print files in reverse */
15 /* Based on Yang Xiaopeng's (yxp at hanwang.com.cn) patch
16 * http://www.uclibc.org/lists/busybox/2003-July/008813.html
19 //usage:#define tac_trivial_usage
21 //usage:#define tac_full_usage "\n\n"
22 //usage: "Concatenate FILEs and print them in reverse"
26 /* This is a NOEXEC applet. Be very careful! */
33 int tac_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
34 int tac_main(int argc UNUSED_PARAM
, char **argv
)
38 struct lstring
*line
= NULL
;
40 int retval
= EXIT_SUCCESS
;
43 /* tac from coreutils 6.9 supports:
45 attach the separator before instead of after
47 interpret the separator as a regular expression
48 -s, --separator=STRING
49 use STRING as the separator instead of newline
50 We support none, but at least we will complain or handle "--":
58 *--argv
= (char *)"-";
59 /* We will read from last file to first */
68 f
= fopen_or_warn_stdin(*name
);
70 /* error message is printed by fopen_or_warn_stdin */
71 retval
= EXIT_FAILURE
;
80 /* Grow on every 128th char */
81 line
= xrealloc(line
, i
+ 0x7f + sizeof(int) + 1);
84 if (ch
== '\n' || (ch
== EOF
&& i
!= 0)) {
85 line
= xrealloc(line
, i
+ sizeof(int));
87 llist_add_to(&list
, line
);
92 /* fgetc sets errno to ENOENT on EOF, we don't want
93 * to warn on this non-error! */
94 if (errno
&& errno
!= ENOENT
) {
95 bb_simple_perror_msg(*name
);
96 retval
= EXIT_FAILURE
;
98 } while (name
!= argv
);
101 line
= (struct lstring
*)list
->data
;
102 xwrite(STDOUT_FILENO
, line
->buf
, line
->size
);
103 if (ENABLE_FEATURE_CLEAN_UP
) {
104 free(llist_pop(&list
));