blob: d70e23ad41cecec81c44a02e6ce64c4c1eceefcb [file] [log] [blame]
Denis Vlasenko482f2b32008-01-07 16:14:14 +00001/* vi: set sw=4 ts=4: */
2/*
3 * tac implementation for busybox
4 *
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>
8 *
9 * Licensed under GPLv2, see file License in this tarball for details.
10 *
11 */
12
13/* tac - concatenate and print files in reverse */
14
15/* Based on Yang Xiaopeng's (yxp at hanwang.com.cn) patch
16 * http://www.uclibc.org/lists/busybox/2003-July/008813.html
17 */
18
19#include "libbb.h"
20
21/* This is a NOEXEC applet. Be very careful! */
22
Denis Vlasenkode24bd92008-01-09 23:00:00 +000023struct lstring {
24 int size;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000025 char buf[1];
Denis Vlasenkode24bd92008-01-09 23:00:00 +000026};
27
Denis Vlasenko482f2b32008-01-07 16:14:14 +000028int tac_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000029int tac_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko482f2b32008-01-07 16:14:14 +000030{
31 char **name;
32 FILE *f;
Denis Vlasenkode24bd92008-01-09 23:00:00 +000033 struct lstring *line = NULL;
Denis Vlasenko482f2b32008-01-07 16:14:14 +000034 llist_t *list = NULL;
35 int retval = EXIT_SUCCESS;
Denis Vlasenko474d1c52008-01-07 19:06:47 +000036
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000037#if ENABLE_DESKTOP
38/* tac from coreutils 6.9 supports:
39 -b, --before
40 attach the separator before instead of after
41 -r, --regex
42 interpret the separator as a regular expression
43 -s, --separator=STRING
44 use STRING as the separator instead of newline
45We support none, but at least we will complain or handle "--":
46*/
47 getopt32(argv, "");
48 argv += optind;
49#else
Denis Vlasenko482f2b32008-01-07 16:14:14 +000050 argv++;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000051#endif
Denis Vlasenko482f2b32008-01-07 16:14:14 +000052 if (!*argv)
53 *--argv = (char *)"-";
54 /* We will read from last file to first */
55 name = argv;
56 while (*name)
57 name++;
58
59 do {
Denis Vlasenkode24bd92008-01-09 23:00:00 +000060 int ch, i;
61
Denis Vlasenko482f2b32008-01-07 16:14:14 +000062 name--;
63 f = fopen_or_warn_stdin(*name);
64 if (f == NULL) {
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000065 /* error message is printed by fopen_or_warn_stdin */
Denis Vlasenko482f2b32008-01-07 16:14:14 +000066 retval = EXIT_FAILURE;
67 continue;
68 }
69
Denis Vlasenkode24bd92008-01-09 23:00:00 +000070 errno = i = 0;
71 do {
72 ch = fgetc(f);
73 if (ch != EOF) {
74 if (!(i & 0x7f))
75 /* Grow on every 128th char */
76 line = xrealloc(line, i + 0x7f + sizeof(int) + 1);
77 line->buf[i++] = ch;
78 }
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000079 if (ch == '\n' || (ch == EOF && i != 0)) {
Denis Vlasenkode24bd92008-01-09 23:00:00 +000080 line = xrealloc(line, i + sizeof(int));
81 line->size = i;
82 llist_add_to(&list, line);
83 line = NULL;
84 i = 0;
85 }
86 } while (ch != EOF);
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000087 /* fgetc sets errno to ENOENT on EOF, we don't want
88 * to warn on this non-error! */
Denis Vlasenko482f2b32008-01-07 16:14:14 +000089 if (errno && errno != ENOENT) {
90 bb_simple_perror_msg(*name);
91 retval = EXIT_FAILURE;
92 }
93 } while (name != argv);
94
95 while (list) {
Denis Vlasenkode24bd92008-01-09 23:00:00 +000096 line = (struct lstring *)list->data;
97 xwrite(STDOUT_FILENO, line->buf, line->size);
98 if (ENABLE_FEATURE_CLEAN_UP) {
99 free(llist_pop(&list));
100 } else {
101 list = list->link;
102 }
Denis Vlasenko482f2b32008-01-07 16:14:14 +0000103 }
Denis Vlasenko474d1c52008-01-07 19:06:47 +0000104
Denis Vlasenko482f2b32008-01-07 16:14:14 +0000105 return retval;
106}