OSDN Git Service

Simplify/unify listfiles recursion: populate directory node (and detect top of tree...
[android-x86/external-toybox.git] / toys / tac.c
1 /* vi: set sw=4 ts=4:
2  *
3  * tac.c - output lines in reverse order
4  *
5  * Copyright 2012 Rob Landley <rob@landley.net>
6  *
7  * Not in SUSv4.
8
9 USE_TAC(NEWTOY(tac, NULL, TOYFLAG_USR|TOYFLAG_BIN))
10
11 config TAC
12         bool "tac"
13         default y
14         help
15           usage: tac [FILE...]
16
17           Output lines in reverse order.
18 */
19
20 #include "toys.h"
21
22 void do_tac(int fd, char *name)
23 {
24         struct arg_list *list = NULL;
25         char *c;
26
27         // Read in lines
28         for (;;) {
29                 struct arg_list *temp;
30                 int len;
31
32                 if (!(c = get_line(fd))) break;
33
34                 len = strlen(c);
35                 if (len && c[len-1]=='\n') c[len-1] = 0;
36                 temp = xmalloc(sizeof(struct arg_list));
37                 temp->next = list;
38                 temp->arg = c;
39                 list = temp;
40         }
41
42         // Play them back.
43         while (list) {
44                 struct arg_list *temp = list->next;
45                 xputs(list->arg);
46                 free(list->arg);
47                 free(list);
48                 list = temp;
49         }
50 }
51
52 void tac_main(void)
53 {
54         loopfiles(toys.optargs, do_tac);
55 }