Mercurial > hg > toybox
annotate toys/other/tac.c @ 1429:8c78a7e5486d draft
Disable xopen's default O_CLOEXEC for oneit.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 09 Aug 2014 23:49:48 -0500 |
parents | 54d248b907ed |
children |
rev | line source |
---|---|
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
1 /* tac.c - output lines in reverse order |
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
|
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 * 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
|
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 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
|
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 config TAC |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
8 bool "tac" |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
9 default y |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
10 help |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
11 usage: tac [FILE...] |
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
|
12 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
13 Output lines in reverse order. |
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
|
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 |
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 #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
|
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 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
|
19 { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
20 struct arg_list *list = NULL; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
21 char *c; |
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
|
22 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
23 // Read in lines |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
24 for (;;) { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
25 struct arg_list *temp; |
758
54d248b907ed
Fix tac to handle the "abc\ndef" case properly
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
751
diff
changeset
|
26 long len; |
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
|
27 |
758
54d248b907ed
Fix tac to handle the "abc\ndef" case properly
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
751
diff
changeset
|
28 if (!(c = get_rawline(fd, &len, '\n'))) break; |
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
|
29 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
30 temp = xmalloc(sizeof(struct arg_list)); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
31 temp->next = list; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
32 temp->arg = c; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
33 list = temp; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
34 } |
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
|
35 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
36 // Play them back. |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
37 while (list) { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
38 struct arg_list *temp = list->next; |
758
54d248b907ed
Fix tac to handle the "abc\ndef" case properly
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
751
diff
changeset
|
39 xprintf("%s", list->arg); |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
40 free(list->arg); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
41 free(list); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
42 list = temp; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
43 } |
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
|
44 } |
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 |
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 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
|
47 { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
48 loopfiles(toys.optargs, do_tac); |
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
|
49 } |