aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCem Keylan <cem@ckyln.com>2021-07-07 16:10:32 +0300
committerCem Keylan <cem@ckyln.com>2021-07-07 16:10:32 +0300
commit854ddfac002e753a6389b48d3b9296fe32665ad3 (patch)
treeff9a2be83f68dd66239ff3a798222dc2f0afc9b8
parent5bc00f0da395aa4ec779bb802bd78bf893565143 (diff)
parent3fbacb1f5c5eab1727d0bed0bd50d659af854ec7 (diff)
downloadtoybox-854ddfac002e753a6389b48d3b9296fe32665ad3.tar.gz
Merge remote-tracking branch 'refs/remotes/origin/master'
-rw-r--r--lib/lib.c8
-rw-r--r--lib/lib.h3
-rwxr-xr-xtests/split.test18
-rwxr-xr-xtests/tail.test6
-rw-r--r--toys/posix/split.c14
-rw-r--r--toys/posix/tail.c140
6 files changed, 92 insertions, 97 deletions
diff --git a/lib/lib.c b/lib/lib.c
index 9f9b8136..49fd5dd2 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -713,9 +713,9 @@ void poke(void *ptr, long long val, unsigned size)
void loopfiles_rw(char **argv, int flags, int permissions,
void (*function)(int fd, char *name))
{
- int fd, failok = !(flags&WARN_ONLY);
+ int fd, failok = !(flags&WARN_ONLY), anyway = flags & LOOPFILES_ANYWAY;
- flags &= ~WARN_ONLY;
+ flags &= ~(WARN_ONLY|LOOPFILES_ANYWAY);
// If no arguments, read from stdin.
if (!*argv) function((flags & O_ACCMODE) != O_RDONLY ? 1 : 0, "-");
@@ -726,10 +726,10 @@ void loopfiles_rw(char **argv, int flags, int permissions,
if (!strcmp(*argv, "-")) fd = 0;
else if (0>(fd = notstdio(open(*argv, flags, permissions))) && !failok) {
perror_msg_raw(*argv);
- continue;
+ if (!anyway) continue;
}
function(fd, *argv);
- if ((flags & O_CLOEXEC) && fd) close(fd);
+ if ((flags & O_CLOEXEC) && fd>0) close(fd);
} while (*++argv);
}
diff --git a/lib/lib.h b/lib/lib.h
index cf1920f9..7484eb8f 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -112,7 +112,8 @@ void show_help(FILE *out, int full);
// Tell xopen and friends to print warnings but return -1 as necessary
// The largest O_BLAH flag so far is arch/alpha's O_PATH at 0x800000 so
// plenty of headroom.
-#define WARN_ONLY (1<<31)
+#define WARN_ONLY (1<<31) // don't exit, just warn
+#define LOOPFILES_ANYWAY (1<<30) // call function with fd -1
// xwrap.c
void xstrncpy(char *dest, char *src, size_t size);
diff --git a/tests/split.test b/tests/split.test
index 4a522432..57270ffa 100755
--- a/tests/split.test
+++ b/tests/split.test
@@ -5,21 +5,21 @@
#testing "name" "command" "result" "infile" "stdin"
testing "split" "seq 1 12345 | split && ls xa[a-z] | wc -l" "13\n" "" ""
-rm xa[a-z]
+rm -f xa[a-z]
testing "-" "seq 1 12345 | split - && ls xa[a-z] | wc -l" "13\n" "" ""
-rm xa[a-z]
+rm -f xa[a-z]
seq 1 12345 > file
testing "file" "split file && ls xa[a-z] | wc -l" "13\n" "" ""
-rm xa[a-z]
+rm -f xa[a-z]
-testing "-l" "split file -l 10k && wc -l xab" "2105 xab\n" "" ""
-rm xa[ab]
+toyonly testing "-l" "split file -l 10k && wc -l xab" "2105 xab\n" "" ""
+rm -f xa[ab]
testing "suffix exhaustion" \
"split file -l 10 -a 1 walrus 2>/dev/null || ls walrus* | wc -l" "26\n" "" ""
-rm walrus*
+rm -f walrus*
testing "bytes" \
"seq 1 20000 | split -b 100 -a 3 - whang && ls whang* | wc -l && wc -c whangbpw" "1089\n94 whangbpw\n" "" ""
@@ -27,5 +27,9 @@ testing "bytes" \
testing "reassembly" \
'ls whang* | sort | xargs cat > reassembled && seq 1 20000 | diff -u reassembled - && echo yes' \
"yes\n" "" ""
+rm -f file whang* reassembled
+
+testing "-n" "split -n 3 input; md5sum xaa xab xac" \
+ "494bb8fb423bfa1a5fd66dd0b98f866d xaa\n449acfdbc692780de30a2df05c5d32aa xab\n15ab4be57aebe9a1e445195d5094036c xac\n" \
+ "$(seq 1 10000)" ""
-rm file whang* reassembled
diff --git a/tests/tail.test b/tests/tail.test
index 036f433c..94644c8f 100755
--- a/tests/tail.test
+++ b/tests/tail.test
@@ -73,3 +73,9 @@ testing "-f one two three" \
'tail -f one two three & sleep .25 ; echo more >> three ; echo also >> one; sleep .25; kill $! >/dev/null' \
"==> one <==\nuno\n\n==> two <==\ndos\n\n==> three <==\ntres\nmore\n\n==> one <==\nalso\n" "" ""
rm one two three
+
+testing "-F" "tail -s .1 -F walrus 2>/dev/null & sleep .2; echo hello > walrus;
+sleep .2; truncate -s 0 walrus; sleep .2; echo potato >> walrus; sleep .2;
+echo hello >> walrus; sleep .2; rm walrus; sleep .2; echo done > walrus;
+ sleep .5; kill %1" "hello\npotato\nhello\ndone\n" "" ""
+rm -f walrus
diff --git a/toys/posix/split.c b/toys/posix/split.c
index 5161affb..293343bf 100644
--- a/toys/posix/split.c
+++ b/toys/posix/split.c
@@ -22,7 +22,7 @@ config SPLIT
-a Suffix length (default 2)
-b BYTES/file (10, 10k, 10m, 10g...)
-l LINES/file (default 1000)
- -n PARTS
+ -n PARTS many equal length files
*/
#define FOR_split
@@ -42,14 +42,10 @@ static void do_split(int infd, char *in)
// posix doesn't cover permissions on output file, so copy input (or 0777)
st.st_mode = 0777;
+ st.st_size = 0;
fstat(infd, &st);
- if (!TT.b && TT.n) {
- if (lseek(infd, 0, SEEK_CUR) < 0)
- error_exit("cannot determine file size");
- TT.b = st.st_size / TT.n;
- }
-
+ if (TT.n && (TT.b = st.st_size/TT.n)<1) return error_msg("%s: no size", in);
len = pos = filenum = bytesleft = linesleft = 0;
for (;;) {
int i, j;
@@ -70,9 +66,7 @@ static void do_split(int infd, char *in)
j /= 26;
}
if (j) error_exit("bad suffix");
- bytesleft = TT.b;
- if (TT.n && filenum == TT.n && st.st_size % 2)
- ++bytesleft;
+ bytesleft = TT.b + ((filenum == TT.n) ? st.st_size%TT.n : 0);
linesleft = TT.l;
xclose(outfd);
outfd = xcreate(TT.outfile, O_RDWR|O_CREAT|O_TRUNC, st.st_mode & 0777);
diff --git a/toys/posix/tail.c b/toys/posix/tail.c
index 842814c0..0d13ab01 100644
--- a/toys/posix/tail.c
+++ b/toys/posix/tail.c
@@ -6,7 +6,7 @@
*
* Deviations from posix: -f waits for pipe/fifo on stdin (nonblock?).
-USE_TAIL(NEWTOY(tail, "?fFs#=1c-n-[-cn][-fF]", TOYFLAG_USR|TOYFLAG_BIN))
+USE_TAIL(NEWTOY(tail, "?fFs:c-n-[-cn][-fF]", TOYFLAG_USR|TOYFLAG_BIN))
config TAIL
bool "tail"
@@ -27,18 +27,18 @@ config TAIL
#define FOR_tail
#include "toys.h"
-struct follow_file {
- char *path;
- int fd;
- dev_t dev;
- ino_t ino;
-};
-
GLOBALS(
- long n, c, s;
- int file_no, last_fd;
+ long n, c;
+ char *s;
+
+ int file_no, last_fd, ss;
struct xnotify *not;
- struct follow_file *F;
+ struct {
+ char *path;
+ int fd;
+ dev_t dev;
+ ino_t ino;
+ } *F;
)
struct line_list {
@@ -132,66 +132,53 @@ static int try_lseek(int fd, long bytes, long lines)
return 1;
}
-static void show_new(int fd, char *path)
-{
- int len;
-
- while ((len = read(fd, toybuf, sizeof(toybuf)))>0) {
- if (TT.last_fd != fd) {
- TT.last_fd = fd;
- xprintf("\n==> %s <==\n", path);
- }
- xwrite(1, toybuf, len);
- }
-}
-
-static void tail_F()
+// For -f and -F
+static void tail_continue()
{
+ long long pos;
+ char *path;
struct stat sb;
- int i, old_fd;
-
- for (;;) {
- for (i = 0; i<TT.file_no; ++i) {
- old_fd = TT.F[i].fd;
- if (stat(TT.F[i].path, &sb) != 0) {
- if (old_fd >= 0) {
- xprintf("tail: file inaccessible: %s\n", TT.F[i].path);
- close(old_fd);
+ int i = 0, fd, len;
+
+ for (i = 0; ; i++) {
+ if (FLAG(f)) fd = xnotify_wait(TT.not, &path);
+ else {
+ if (i == TT.file_no) {
+ i = 0;
+ msleep(TT.ss);
+ }
+ fd = TT.F[i].fd;
+ path = TT.F[i].path;
+
+ if (stat(TT.F[i].path, &sb)) {
+ if (fd >= 0) {
+ close(fd);
TT.F[i].fd = -1;
+ error_msg("file inaccessible: %s\n", TT.F[i].path);
}
continue;
}
- if (old_fd < 0 || sb.st_dev != TT.F[i].dev || sb.st_ino != TT.F[i].ino) {
- if (old_fd >= 0) close(old_fd);
- TT.F[i].fd = open(TT.F[i].path, O_RDONLY);
- if (TT.F[i].fd == -1) continue;
- else {
- xprintf("tail: following new file: %s\n", TT.F[i].path);
- TT.F[i].dev = sb.st_dev;
- TT.F[i].ino = sb.st_ino;
- }
- } else if (sb.st_size && sb.st_size < lseek(TT.F[i].fd, 0, SEEK_CUR)) {
- // If file was truncated, move to start.
- xprintf("tail: file truncated: %s\n", TT.F[i].path);
- xlseek(TT.F[i].fd, 0, SEEK_SET);
+ if (fd<0 || sb.st_dev!=TT.F[i].dev || sb.st_ino!=TT.F[i].ino) {
+ if (fd>=0) close(fd);
+ if (-1 == (TT.F[i].fd = fd = open(path, O_RDONLY))) continue;
+ error_msg("following new file: %s\n", path);
+ TT.F[i].dev = sb.st_dev;
+ TT.F[i].ino = sb.st_ino;
+ } else if (sb.st_size <= (pos = lseek(fd, 0, SEEK_CUR))) {
+ if (pos == sb.st_size) continue;
+ error_msg("file truncated: %s\n", path);
+ lseek(fd, 0, SEEK_SET);
}
-
- show_new(TT.F[i].fd, TT.F[i].path);
}
- sleep(TT.s);
- }
-}
-
-static void tail_f()
-{
- char *path;
- int fd;
-
- for (;;) {
- fd = xnotify_wait(TT.not, &path);
- show_new(fd, path);
+ while ((len = read(fd, toybuf, sizeof(toybuf)))>0) {
+ if (TT.file_no>1 && TT.last_fd != fd) {
+ TT.last_fd = fd;
+ xprintf("\n==> %s <==\n", path);
+ }
+ xwrite(1, toybuf, len);
+ }
}
}
@@ -201,6 +188,9 @@ static void do_tail(int fd, char *name)
long bytes = TT.c, lines = TT.n;
int linepop = 1;
+ if (FLAG(F)) {
+ if (!fd) perror_exit("no -F with '-'");
+ } else if (fd == -1) return;
if (FLAG(f) || FLAG(F)) {
char *s = name;
struct stat sb;
@@ -209,11 +199,13 @@ static void do_tail(int fd, char *name)
if (FLAG(f)) xnotify_add(TT.not, fd, s);
if (FLAG(F)) {
- if (fstat(fd, &sb) != 0) perror_exit("%s", name);
+ if (fd != -1) {
+ if (fstat(fd, &sb)) perror_exit("%s", name);
+ TT.F[TT.file_no].dev = sb.st_dev;
+ TT.F[TT.file_no].ino = sb.st_ino;
+ }
TT.F[TT.file_no].fd = fd;
TT.F[TT.file_no].path = s;
- TT.F[TT.file_no].dev = sb.st_dev;
- TT.F[TT.file_no].ino = sb.st_ino;
}
}
@@ -298,23 +290,21 @@ void tail_main(void)
if (!FLAG(n) && !FLAG(c)) {
char *arg = *args;
- // handle old "-42" style arguments
+ // handle old "-42" style arguments, else default to last 10 lines
if (arg && *arg == '-' && arg[1]) {
TT.n = atolx(*(args++));
toys.optc--;
- } else {
- // if nothing specified, default -n to -10
- TT.n = -10;
- }
+ } else TT.n = -10;
}
- if (FLAG(F)) TT.F = xmalloc(toys.optc * sizeof(struct follow_file));
+ if (FLAG(F)) TT.F = xzalloc(toys.optc*sizeof(*TT.F));
else if (FLAG(f)) TT.not = xnotify_init(toys.optc);
+ TT.ss = TT.s ? xparsemillitime(TT.s) : 1000;
- loopfiles_rw(args, O_RDONLY|WARN_ONLY|(O_CLOEXEC*!(FLAG(f) || FLAG(F))), 0, do_tail);
+ loopfiles_rw(args,
+ O_RDONLY|WARN_ONLY|LOOPFILES_ANYWAY|(O_CLOEXEC*!(FLAG(f) || FLAG(F))),
+ 0, do_tail);
- if (TT.file_no) {
- if (FLAG(F)) tail_F();
- else if (FLAG(f)) tail_f();
- }
+ // Wait for more data when following files
+ if (TT.file_no && (FLAG(F) || FLAG(f))) tail_continue();
}