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! */
23 int tac_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
24 int tac_main(int argc, char **argv)
30 int retval = EXIT_SUCCESS;
34 *--argv = (char *)"-";
35 /* We will read from last file to first */
42 f = fopen_or_warn_stdin(*name);
44 retval = EXIT_FAILURE;
49 /* FIXME: NUL bytes are mishandled. */
50 while ((line = xmalloc_fgets(f)) != NULL)
51 llist_add_to(&list, line);
53 /* xmalloc_fgets uses getc and returns NULL on error or EOF. */
54 /* It sets errno to ENOENT on EOF, but fopen_or_warn_stdin would */
55 /* catch this error so we can filter it out here. */
56 if (errno && errno != ENOENT) {
57 bb_simple_perror_msg(*name);
58 retval = EXIT_FAILURE;
60 } while (name != argv);
63 printf("%s", list->data);