Home | History | Annotate | Download | only in other
      1 /* tac.c - output lines in reverse order
      2  *
      3  * Copyright 2012 Rob Landley <rob (at) landley.net>
      4 
      5 USE_TAC(NEWTOY(tac, NULL, TOYFLAG_USR|TOYFLAG_BIN))
      6 
      7 config TAC
      8   bool "tac"
      9   default y
     10   help
     11     usage: tac [FILE...]
     12 
     13     Output lines in reverse order.
     14 */
     15 
     16 #include "toys.h"
     17 
     18 static void do_tac(int fd, char *name)
     19 {
     20   struct arg_list *list = NULL;
     21   char *c;
     22 
     23   // Read in lines
     24   for (;;) {
     25     struct arg_list *temp;
     26     long len;
     27 
     28     if (!(c = get_rawline(fd, &len, '\n'))) break;
     29 
     30     temp = xmalloc(sizeof(struct arg_list));
     31     temp->next = list;
     32     temp->arg = c;
     33     list = temp;
     34   }
     35 
     36   // Play them back.
     37   while (list) {
     38     struct arg_list *temp = list->next;
     39     xprintf("%s", list->arg);
     40     free(list->arg);
     41     free(list);
     42     list = temp;
     43   }
     44 }
     45 
     46 void tac_main(void)
     47 {
     48   loopfiles(toys.optargs, do_tac);
     49 }
     50