diff options
author | Rob Landley <rob@landley.net> | 2012-02-28 00:29:52 -0600 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2012-02-28 00:29:52 -0600 |
commit | b73d8e3c2d3b606295453940e602d173924cd380 (patch) | |
tree | e27f1493aaf74291e83e9f7bfe146c57959855d7 /toys/tail.c | |
parent | f793d5347f08bdb742e8ad8a2cc5a7d30e6bd623 (diff) | |
download | toybox-b73d8e3c2d3b606295453940e602d173924cd380.tar.gz |
Add "-" type to optargs and teach tail.c to use it. Tighten up help text, use xzalloc() and xputc() as appropriate.
Diffstat (limited to 'toys/tail.c')
-rw-r--r-- | toys/tail.c | 46 |
1 files changed, 11 insertions, 35 deletions
diff --git a/toys/tail.c b/toys/tail.c index 5ae295b8..b992cd16 100644 --- a/toys/tail.c +++ b/toys/tail.c @@ -6,38 +6,28 @@ * * See http://www.opengroup.org/onlinepubs/009695399/utilities/tail.html -USE_TAIL(NEWTOY(tail, "c:fn:", TOYFLAG_BIN)) +USE_TAIL(NEWTOY(tail, "c-|fn-|", TOYFLAG_BIN)) config TAIL bool "tail" default n help - usage: tail [-n number] [-c number] [-f] [file...] + usage: tail [-n|c number] [-f] [file...] Copy last lines from files to stdout. If no files listed, copy from stdin. Filename "-" is a synonym for stdin. - -n Line offset to start copying from. The number may be signed. - When negative, the line offset is counted from the end of the - file. When positive, the offset is counted from the beginning. - When the sign is omitted, the offset is counted from the end of - the file. Defaults to -10. - - -c Byte offset to start copying from. As above, may be a positive - or negative signed number. - - -f Continue reading input after reaching the last line of input. - This option is ignored if the input file is not a regular file - or the given file is a FIFO. + -n output the last X lines (default 10), +X counts from start. + -c output the last X bytes, +X counts from start + -f follow file, waiting for more data to be appended */ #include "toys.h" DEFINE_GLOBALS( - char *lines_str; - char *bytes_str; long lines; long bytes; + int file_no; ) @@ -128,8 +118,7 @@ static void print_last_lines(int fd, long lines) for (;;) { // read from input and append to buffer list - cur = xmalloc(sizeof(struct line_list)); - memset(cur, 0, sizeof(struct line_list)); + cur = xzalloc(sizeof(struct line_list)); cur->data = xmalloc(size); cur->len = readall(fd, cur->data, size); @@ -170,32 +159,19 @@ static void do_tail(int fd, char *name) if (toys.optc > 1) { // print an extra newline for all but the first file - if (TT.file_no++) xprintf("\n"); + if (TT.file_no++) xputc('\n'); xprintf("==> %s <==\n", name); - xflush(); } if (lines > 0 || bytes > 0) print_after_offset(fd, bytes, lines); else if (bytes < 0) print_last_bytes(fd, bytes * -1); - else if (lines < 0) print_last_lines(fd, lines * -1); -} - -long atolx_default_negative(char *str) -{ - long val = atolx(str); - return str[0] != '+' && str[0] != '-' ? val * -1 : val; + else print_last_lines(fd, lines * -1); } void tail_main(void) { - // if option -c or -n has no sign then we make it negative - if (toys.optflags & FLAG_c) - TT.bytes = atolx_default_negative(TT.bytes_str); - - if (toys.optflags & FLAG_n) - TT.lines = atolx_default_negative(TT.lines_str); - else - TT.lines = -10; + // if nothing specified, default -n to -10 + if (!(toys.optflags&(FLAG_n|FLAG_c))) TT.lines = -10; loopfiles(toys.optargs, do_tail); } |