1 /* vi: set sw=4 ts=4: */
3 * tac implementation for busybox
4 * tac - concatenate and print files in reverse
6 * Copyright (C) 2003 Yang Xiaopeng <yxp at hanwang.com.cn>
7 * Copyright (C) 2007 Natanael Copa <natanael.copa@gmail.com>
8 * Copyright (C) 2007 Tito Ragusa <farmatito@tiscali.it>
10 * Licensed under GPLv2, see file LICENSE in this source tree.
12 /* Based on Yang Xiaopeng's (yxp at hanwang.com.cn) patch
13 * http://www.uclibc.org/lists/busybox/2003-July/008813.html
19 //config: tac is used to concatenate and print files in reverse.
21 //applet:IF_TAC(APPLET_NOEXEC(tac, tac, BB_DIR_USR_BIN, BB_SUID_DROP, tac))
23 //kbuild:lib-$(CONFIG_TAC) += tac.o
25 //usage:#define tac_trivial_usage
27 //usage:#define tac_full_usage "\n\n"
28 //usage: "Concatenate FILEs and print them in reverse"
32 /* This is a NOEXEC applet. Be very careful! */
39 int tac_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
40 int tac_main(int argc UNUSED_PARAM, char **argv)
44 struct lstring *line = NULL;
46 int retval = EXIT_SUCCESS;
49 /* tac from coreutils 6.9 supports:
51 attach the separator before instead of after
53 interpret the separator as a regular expression
54 -s, --separator=STRING
55 use STRING as the separator instead of newline
56 We support none, but at least we will complain or handle "--":
64 *--argv = (char *)"-";
65 /* We will read from last file to first */
74 f = fopen_or_warn_stdin(*name);
76 /* error message is printed by fopen_or_warn_stdin */
77 retval = EXIT_FAILURE;
86 /* Grow on every 128th char */
87 line = xrealloc(line, i + 0x7f + sizeof(int) + 1);
90 if (ch == '\n' || (ch == EOF && i != 0)) {
91 line = xrealloc(line, i + sizeof(int));
93 llist_add_to(&list, line);
98 /* fgetc sets errno to ENOENT on EOF, we don't want
99 * to warn on this non-error! */
100 if (errno && errno != ENOENT) {
101 bb_simple_perror_msg(*name);
102 retval = EXIT_FAILURE;
104 } while (name != argv);
107 line = (struct lstring *)list->data;
108 xwrite(STDOUT_FILENO, line->buf, line->size);
109 if (ENABLE_FEATURE_CLEAN_UP) {
110 free(llist_pop(&list));