Mercurial > hg > toybox
annotate toys/other/tac.c @ 656:6df4ccc0acbe
Regularize command headers, update links to standards documents.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 25 Aug 2012 18:08:51 -0500 |
parents | 2986aa63a021 |
children | 786841fdb1e0 |
rev | line source |
---|---|
522
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
1 /* vi: set sw=4 ts=4: |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
2 * |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
3 * tac.c - output lines in reverse order |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
4 * |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
5 * Copyright 2012 Rob Landley <rob@landley.net> |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
6 |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
7 USE_TAC(NEWTOY(tac, NULL, TOYFLAG_USR|TOYFLAG_BIN)) |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
8 |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
9 config TAC |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
10 bool "tac" |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
11 default y |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
12 help |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
13 usage: tac [FILE...] |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
14 |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
15 Output lines in reverse order. |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
16 */ |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
17 |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
18 #include "toys.h" |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
19 |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
20 void do_tac(int fd, char *name) |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
21 { |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
22 struct arg_list *list = NULL; |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
23 char *c; |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
24 |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
25 // Read in lines |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
26 for (;;) { |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
27 struct arg_list *temp; |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
28 int len; |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
29 |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
30 if (!(c = get_line(fd))) break; |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
31 |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
32 len = strlen(c); |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
33 if (len && c[len-1]=='\n') c[len-1] = 0; |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
34 temp = xmalloc(sizeof(struct arg_list)); |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
35 temp->next = list; |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
36 temp->arg = c; |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
37 list = temp; |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
38 } |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
39 |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
40 // Play them back. |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
41 while (list) { |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
42 struct arg_list *temp = list->next; |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
43 xputs(list->arg); |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
44 free(list->arg); |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
45 free(list); |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
46 list = temp; |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
47 } |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
48 } |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
49 |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
50 void tac_main(void) |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
51 { |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
52 loopfiles(toys.optargs, do_tac); |
33ce0407ea47
Add tac. (Shell wrapper is smaller, but all-in-one static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
53 } |