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
21 /* This is a NOEXEC applet. Be very careful! */
28 int tac_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
29 int tac_main(int argc UNUSED_PARAM
, char **argv
)
33 struct lstring
*line
= NULL
;
35 int retval
= EXIT_SUCCESS
;
38 /* tac from coreutils 6.9 supports:
40 attach the separator before instead of after
42 interpret the separator as a regular expression
43 -s, --separator=STRING
44 use STRING as the separator instead of newline
45 We support none, but at least we will complain or handle "--":
53 *--argv
= (char *)"-";
54 /* We will read from last file to first */
63 f
= fopen_or_warn_stdin(*name
);
65 /* error message is printed by fopen_or_warn_stdin */
66 retval
= EXIT_FAILURE
;
75 /* Grow on every 128th char */
76 line
= xrealloc(line
, i
+ 0x7f + sizeof(int) + 1);
79 if (ch
== '\n' || (ch
== EOF
&& i
!= 0)) {
80 line
= xrealloc(line
, i
+ sizeof(int));
82 llist_add_to(&list
, line
);
87 /* fgetc sets errno to ENOENT on EOF, we don't want
88 * to warn on this non-error! */
89 if (errno
&& errno
!= ENOENT
) {
90 bb_simple_perror_msg(*name
);
91 retval
= EXIT_FAILURE
;
93 } while (name
!= argv
);
96 line
= (struct lstring
*)list
->data
;
97 xwrite(STDOUT_FILENO
, line
->buf
, line
->size
);
98 if (ENABLE_FEATURE_CLEAN_UP
) {
99 free(llist_pop(&list
));