toybox/toys/other/tac.c
<<
>>
Prefs
   1/* tac.c - output lines in reverse order
   2 *
   3 * Copyright 2012 Rob Landley <rob@landley.net>
   4
   5USE_TAC(NEWTOY(tac, NULL, TOYFLAG_USR|TOYFLAG_BIN))
   6
   7config TAC
   8  bool "tac"
   9  default y
  10  help
  11    usage: tac [FILE...]
  12
  13    Output lines in reverse order.
  14*/
  15
  16#define FOR_tac
  17#include "toys.h"
  18
  19GLOBALS(
  20  struct double_list *dl;
  21)
  22
  23static void do_tac(char **pline, long len)
  24{
  25  if (pline) {
  26    dlist_add(&TT.dl, *pline);
  27    *pline = 0;
  28  } else while (TT.dl) {
  29    struct double_list *dl = dlist_lpop(&TT.dl);
  30
  31    xprintf("%s", dl->data);
  32    free(dl->data);
  33    free(dl);
  34  }
  35}
  36
  37void tac_main(void)
  38{
  39  loopfiles_lines(toys.optargs, do_tac);
  40}
  41