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 tarball for details.
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 ATTRIBUTE_UNUSED, char **argv)
33 struct lstring *line = NULL;
35 int retval = EXIT_SUCCESS;
39 *--argv = (char *)"-";
40 /* We will read from last file to first */
49 f = fopen_or_warn_stdin(*name);
51 retval = EXIT_FAILURE;
60 /* Grow on every 128th char */
61 line = xrealloc(line, i + 0x7f + sizeof(int) + 1);
64 if ((ch == '\n' || ch == EOF) && i) {
65 line = xrealloc(line, i + sizeof(int));
67 llist_add_to(&list, line);
72 /* fgetc sets errno to ENOENT on EOF, but */
73 /* fopen_or_warn_stdin would catch this error */
74 /* so we can filter it out here. */
75 if (errno && errno != ENOENT) {
76 bb_simple_perror_msg(*name);
77 retval = EXIT_FAILURE;
79 } while (name != argv);
82 line = (struct lstring *)list->data;
83 xwrite(STDOUT_FILENO, line->buf, line->size);
84 if (ENABLE_FEATURE_CLEAN_UP) {
85 free(llist_pop(&list));