Denis Vlasenko | 482f2b3 | 2008-01-07 16:14:14 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * tac implementation for busybox |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 4 | * tac - concatenate and print files in reverse |
Denis Vlasenko | 482f2b3 | 2008-01-07 16:14:14 +0000 | [diff] [blame] | 5 | * |
| 6 | * Copyright (C) 2003 Yang Xiaopeng <yxp at hanwang.com.cn> |
| 7 | * Copyright (C) 2007 Natanael Copa <natanael.copa@gmail.com> |
| 8 | * Copyright (C) 2007 Tito Ragusa <farmatito@tiscali.it> |
| 9 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 10 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | 482f2b3 | 2008-01-07 16:14:14 +0000 | [diff] [blame] | 11 | */ |
Denis Vlasenko | 482f2b3 | 2008-01-07 16:14:14 +0000 | [diff] [blame] | 12 | /* Based on Yang Xiaopeng's (yxp at hanwang.com.cn) patch |
| 13 | * http://www.uclibc.org/lists/busybox/2003-July/008813.html |
| 14 | */ |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 15 | //config:config TAC |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 16 | //config: bool "tac (3.9 kb)" |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 17 | //config: default y |
| 18 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 19 | //config: tac is used to concatenate and print files in reverse. |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 20 | |
| 21 | //applet:IF_TAC(APPLET_NOEXEC(tac, tac, BB_DIR_USR_BIN, BB_SUID_DROP, tac)) |
| 22 | |
| 23 | //kbuild:lib-$(CONFIG_TAC) += tac.o |
Denis Vlasenko | 482f2b3 | 2008-01-07 16:14:14 +0000 | [diff] [blame] | 24 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 25 | //usage:#define tac_trivial_usage |
| 26 | //usage: "[FILE]..." |
| 27 | //usage:#define tac_full_usage "\n\n" |
| 28 | //usage: "Concatenate FILEs and print them in reverse" |
| 29 | |
Denis Vlasenko | 482f2b3 | 2008-01-07 16:14:14 +0000 | [diff] [blame] | 30 | #include "libbb.h" |
| 31 | |
| 32 | /* This is a NOEXEC applet. Be very careful! */ |
| 33 | |
Denis Vlasenko | de24bd9 | 2008-01-09 23:00:00 +0000 | [diff] [blame] | 34 | struct lstring { |
| 35 | int size; |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 36 | char buf[1]; |
Denis Vlasenko | de24bd9 | 2008-01-09 23:00:00 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
Denis Vlasenko | 482f2b3 | 2008-01-07 16:14:14 +0000 | [diff] [blame] | 39 | int tac_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 40 | int tac_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 482f2b3 | 2008-01-07 16:14:14 +0000 | [diff] [blame] | 41 | { |
| 42 | char **name; |
| 43 | FILE *f; |
Denis Vlasenko | de24bd9 | 2008-01-09 23:00:00 +0000 | [diff] [blame] | 44 | struct lstring *line = NULL; |
Denis Vlasenko | 482f2b3 | 2008-01-07 16:14:14 +0000 | [diff] [blame] | 45 | llist_t *list = NULL; |
| 46 | int retval = EXIT_SUCCESS; |
Denis Vlasenko | 474d1c5 | 2008-01-07 19:06:47 +0000 | [diff] [blame] | 47 | |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 48 | #if ENABLE_DESKTOP |
| 49 | /* tac from coreutils 6.9 supports: |
| 50 | -b, --before |
| 51 | attach the separator before instead of after |
| 52 | -r, --regex |
| 53 | interpret the separator as a regular expression |
| 54 | -s, --separator=STRING |
| 55 | use STRING as the separator instead of newline |
| 56 | We support none, but at least we will complain or handle "--": |
| 57 | */ |
| 58 | getopt32(argv, ""); |
| 59 | argv += optind; |
| 60 | #else |
Denis Vlasenko | 482f2b3 | 2008-01-07 16:14:14 +0000 | [diff] [blame] | 61 | argv++; |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 62 | #endif |
Denis Vlasenko | 482f2b3 | 2008-01-07 16:14:14 +0000 | [diff] [blame] | 63 | if (!*argv) |
| 64 | *--argv = (char *)"-"; |
| 65 | /* We will read from last file to first */ |
| 66 | name = argv; |
| 67 | while (*name) |
| 68 | name++; |
| 69 | |
| 70 | do { |
Denis Vlasenko | de24bd9 | 2008-01-09 23:00:00 +0000 | [diff] [blame] | 71 | int ch, i; |
| 72 | |
Denis Vlasenko | 482f2b3 | 2008-01-07 16:14:14 +0000 | [diff] [blame] | 73 | name--; |
| 74 | f = fopen_or_warn_stdin(*name); |
| 75 | if (f == NULL) { |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 76 | /* error message is printed by fopen_or_warn_stdin */ |
Denis Vlasenko | 482f2b3 | 2008-01-07 16:14:14 +0000 | [diff] [blame] | 77 | retval = EXIT_FAILURE; |
| 78 | continue; |
| 79 | } |
| 80 | |
Denis Vlasenko | de24bd9 | 2008-01-09 23:00:00 +0000 | [diff] [blame] | 81 | errno = i = 0; |
| 82 | do { |
| 83 | ch = fgetc(f); |
| 84 | if (ch != EOF) { |
| 85 | if (!(i & 0x7f)) |
| 86 | /* Grow on every 128th char */ |
| 87 | line = xrealloc(line, i + 0x7f + sizeof(int) + 1); |
| 88 | line->buf[i++] = ch; |
| 89 | } |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 90 | if (ch == '\n' || (ch == EOF && i != 0)) { |
Denis Vlasenko | de24bd9 | 2008-01-09 23:00:00 +0000 | [diff] [blame] | 91 | line = xrealloc(line, i + sizeof(int)); |
| 92 | line->size = i; |
| 93 | llist_add_to(&list, line); |
| 94 | line = NULL; |
| 95 | i = 0; |
| 96 | } |
| 97 | } while (ch != EOF); |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 98 | /* fgetc sets errno to ENOENT on EOF, we don't want |
| 99 | * to warn on this non-error! */ |
Denis Vlasenko | 482f2b3 | 2008-01-07 16:14:14 +0000 | [diff] [blame] | 100 | if (errno && errno != ENOENT) { |
| 101 | bb_simple_perror_msg(*name); |
| 102 | retval = EXIT_FAILURE; |
| 103 | } |
| 104 | } while (name != argv); |
| 105 | |
| 106 | while (list) { |
Denis Vlasenko | de24bd9 | 2008-01-09 23:00:00 +0000 | [diff] [blame] | 107 | line = (struct lstring *)list->data; |
| 108 | xwrite(STDOUT_FILENO, line->buf, line->size); |
| 109 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 110 | free(llist_pop(&list)); |
| 111 | } else { |
| 112 | list = list->link; |
| 113 | } |
Denis Vlasenko | 482f2b3 | 2008-01-07 16:14:14 +0000 | [diff] [blame] | 114 | } |
Denis Vlasenko | 474d1c5 | 2008-01-07 19:06:47 +0000 | [diff] [blame] | 115 | |
Denis Vlasenko | 482f2b3 | 2008-01-07 16:14:14 +0000 | [diff] [blame] | 116 | return retval; |
| 117 | } |