*: add -Wunused-parameter; fix resulting breakage
[platform/upstream/busybox.git] / coreutils / tac.c
1 /* 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
23 struct lstring {
24         int size;
25         char buf[];
26 };
27
28 int tac_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
29 int tac_main(int argc ATTRIBUTE_UNUSED, char **argv)
30 {
31         char **name;
32         FILE *f;
33         struct lstring *line = NULL;
34         llist_t *list = NULL;
35         int retval = EXIT_SUCCESS;
36
37         argv++;
38         if (!*argv)
39                 *--argv = (char *)"-";
40         /* We will read from last file to first */
41         name = argv;
42         while (*name)
43                 name++;
44
45         do {
46                 int ch, i;
47
48                 name--;
49                 f = fopen_or_warn_stdin(*name);
50                 if (f == NULL) {
51                         retval = EXIT_FAILURE;
52                         continue;
53                 }
54
55                 errno = i = 0;
56                 do {
57                         ch = fgetc(f);
58                         if (ch != EOF) {
59                                 if (!(i & 0x7f))
60                                         /* Grow on every 128th char */
61                                         line = xrealloc(line, i + 0x7f + sizeof(int) + 1);
62                                 line->buf[i++] = ch;
63                         }
64                         if ((ch == '\n' || ch == EOF) && i) {
65                                 line = xrealloc(line, i + sizeof(int));
66                                 line->size = i;
67                                 llist_add_to(&list, line);
68                                 line = NULL;
69                                 i = 0;
70                         }
71                 } while (ch != EOF);
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;
78                 }
79         } while (name != argv);
80
81         while (list) {
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));
86                 } else {
87                         list = list->link;
88                 }
89         }
90
91         return retval;
92 }