tac.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * tac implementation for busybox
  4. *
  5. * Copyright (C) 2003 Yang Xiaopeng <yxp at hanwang.com.cn>
  6. * Copyright (C) 2007 Natanael Copa <natanael.copa@gmail.com>
  7. * Copyright (C) 2007 Tito Ragusa <farmatito@tiscali.it>
  8. *
  9. * Licensed under GPLv2, see file LICENSE in this source tree.
  10. *
  11. */
  12. /* tac - concatenate and print files in reverse */
  13. /* Based on Yang Xiaopeng's (yxp at hanwang.com.cn) patch
  14. * http://www.uclibc.org/lists/busybox/2003-July/008813.html
  15. */
  16. //usage:#define tac_trivial_usage
  17. //usage: "[FILE]..."
  18. //usage:#define tac_full_usage "\n\n"
  19. //usage: "Concatenate FILEs and print them in reverse"
  20. #include "libbb.h"
  21. /* This is a NOEXEC applet. Be very careful! */
  22. struct lstring {
  23. int size;
  24. char buf[1];
  25. };
  26. int tac_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  27. int tac_main(int argc UNUSED_PARAM, char **argv)
  28. {
  29. char **name;
  30. FILE *f;
  31. struct lstring *line = NULL;
  32. llist_t *list = NULL;
  33. int retval = EXIT_SUCCESS;
  34. #if ENABLE_DESKTOP
  35. /* tac from coreutils 6.9 supports:
  36. -b, --before
  37. attach the separator before instead of after
  38. -r, --regex
  39. interpret the separator as a regular expression
  40. -s, --separator=STRING
  41. use STRING as the separator instead of newline
  42. We support none, but at least we will complain or handle "--":
  43. */
  44. getopt32(argv, "");
  45. argv += optind;
  46. #else
  47. argv++;
  48. #endif
  49. if (!*argv)
  50. *--argv = (char *)"-";
  51. /* We will read from last file to first */
  52. name = argv;
  53. while (*name)
  54. name++;
  55. do {
  56. int ch, i;
  57. name--;
  58. f = fopen_or_warn_stdin(*name);
  59. if (f == NULL) {
  60. /* error message is printed by fopen_or_warn_stdin */
  61. retval = EXIT_FAILURE;
  62. continue;
  63. }
  64. errno = i = 0;
  65. do {
  66. ch = fgetc(f);
  67. if (ch != EOF) {
  68. if (!(i & 0x7f))
  69. /* Grow on every 128th char */
  70. line = xrealloc(line, i + 0x7f + sizeof(int) + 1);
  71. line->buf[i++] = ch;
  72. }
  73. if (ch == '\n' || (ch == EOF && i != 0)) {
  74. line = xrealloc(line, i + sizeof(int));
  75. line->size = i;
  76. llist_add_to(&list, line);
  77. line = NULL;
  78. i = 0;
  79. }
  80. } while (ch != EOF);
  81. /* fgetc sets errno to ENOENT on EOF, we don't want
  82. * to warn on this non-error! */
  83. if (errno && errno != ENOENT) {
  84. bb_simple_perror_msg(*name);
  85. retval = EXIT_FAILURE;
  86. }
  87. } while (name != argv);
  88. while (list) {
  89. line = (struct lstring *)list->data;
  90. xwrite(STDOUT_FILENO, line->buf, line->size);
  91. if (ENABLE_FEATURE_CLEAN_UP) {
  92. free(llist_pop(&list));
  93. } else {
  94. list = list->link;
  95. }
  96. }
  97. return retval;
  98. }