10f66f451Sopenharmony_ci/* tac.c - output lines in reverse order
20f66f451Sopenharmony_ci *
30f66f451Sopenharmony_ci * Copyright 2012 Rob Landley <rob@landley.net>
40f66f451Sopenharmony_ci
50f66f451Sopenharmony_ciUSE_TAC(NEWTOY(tac, NULL, TOYFLAG_USR|TOYFLAG_BIN))
60f66f451Sopenharmony_ci
70f66f451Sopenharmony_ciconfig TAC
80f66f451Sopenharmony_ci  bool "tac"
90f66f451Sopenharmony_ci  default y
100f66f451Sopenharmony_ci  help
110f66f451Sopenharmony_ci    usage: tac [FILE...]
120f66f451Sopenharmony_ci
130f66f451Sopenharmony_ci    Output lines in reverse order.
140f66f451Sopenharmony_ci*/
150f66f451Sopenharmony_ci
160f66f451Sopenharmony_ci#define FOR_tac
170f66f451Sopenharmony_ci#include "toys.h"
180f66f451Sopenharmony_ci
190f66f451Sopenharmony_ciGLOBALS(
200f66f451Sopenharmony_ci  struct double_list *dl;
210f66f451Sopenharmony_ci)
220f66f451Sopenharmony_ci
230f66f451Sopenharmony_cistatic void do_tac(char **pline, long len)
240f66f451Sopenharmony_ci{
250f66f451Sopenharmony_ci  if (pline) {
260f66f451Sopenharmony_ci    dlist_add(&TT.dl, *pline);
270f66f451Sopenharmony_ci    *pline = 0;
280f66f451Sopenharmony_ci  } else while (TT.dl) {
290f66f451Sopenharmony_ci    struct double_list *dl = dlist_lpop(&TT.dl);
300f66f451Sopenharmony_ci
310f66f451Sopenharmony_ci    xprintf("%s", dl->data);
320f66f451Sopenharmony_ci    free(dl->data);
330f66f451Sopenharmony_ci    free(dl);
340f66f451Sopenharmony_ci  }
350f66f451Sopenharmony_ci}
360f66f451Sopenharmony_ci
370f66f451Sopenharmony_civoid tac_main(void)
380f66f451Sopenharmony_ci{
390f66f451Sopenharmony_ci  loopfiles_lines(toys.optargs, do_tac);
400f66f451Sopenharmony_ci}
41