Mercurial > hg > toybox
annotate toys/tac.c @ 562:4d802d438983
Match uint64_t with PRIu64 to avoid warnings on 64 bit builds.
author  Rob Landley <rob@landley.net> 

date  Sat, 14 Apr 2012 21:27:00 0500 
parents  33ce0407ea47 
children 
rev  line source 

522
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone 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 allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

2 * 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone 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 allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

4 * 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone 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 allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

6 * 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

7 * Not in SUSv4. 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

8 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

9 USE_TAC(NEWTOY(tac, NULL, TOYFLAG_USRTOYFLAG_BIN)) 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

10 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

11 config TAC 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

12 bool "tac" 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

13 default y 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

14 help 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

15 usage: tac [FILE...] 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

16 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

17 Output lines in reverse order. 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

18 */ 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

19 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

20 #include "toys.h" 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

21 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

22 void do_tac(int fd, char *name) 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

23 { 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

24 struct arg_list *list = NULL; 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

25 char *c; 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

26 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

27 // Read in lines 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

28 for (;;) { 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

29 struct arg_list *temp; 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

30 int len; 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

31 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

32 if (!(c = get_line(fd))) break; 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

33 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

34 len = strlen(c); 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

35 if (len && c[len1]=='\n') c[len1] = 0; 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

36 temp = xmalloc(sizeof(struct arg_list)); 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

37 temp>next = list; 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

38 temp>arg = c; 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

39 list = temp; 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

40 } 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

41 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

42 // Play them back. 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

43 while (list) { 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

44 struct arg_list *temp = list>next; 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

45 xputs(list>arg); 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

46 free(list>arg); 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

47 free(list); 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

48 list = temp; 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

49 } 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

50 } 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

51 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

52 void tac_main(void) 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

53 { 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

54 loopfiles(toys.optargs, do_tac); 
33ce0407ea47
Add tac. (Shell wrapper is smaller, but allinone static binary is compelling use case.)
Rob Landley <rob@landley.net>
parents:
diff
changeset

55 } 