Merge remote-tracking branch 'toybox/master' into HEAD
Change-Id: I69a04951b0d27c7c3fa05ef51278fb2087851488
diff --git a/android/device/generated/flags.h b/android/device/generated/flags.h
index de3425d..fb87274 100644
--- a/android/device/generated/flags.h
+++ b/android/device/generated/flags.h
@@ -2948,9 +2948,9 @@
#undef FOR_tac
#endif
-// tail ?fFs#=1c-n-[-cn][-fF] ?fFs#=1c-n-[-cn][-fF]
+// tail ?fFs:c-n-[-cn][-fF] ?fFs:c-n-[-cn][-fF]
#undef OPTSTR_tail
-#define OPTSTR_tail "?fFs#=1c-n-[-cn][-fF]"
+#define OPTSTR_tail "?fFs:c-n-[-cn][-fF]"
#ifdef CLEANUP_tail
#undef CLEANUP_tail
#undef FOR_tail
diff --git a/android/device/generated/globals.h b/android/device/generated/globals.h
index 73cc23c..6eae6f7 100644
--- a/android/device/generated/globals.h
+++ b/android/device/generated/globals.h
@@ -1472,10 +1472,17 @@
// toys/posix/tail.c
struct tail_data {
- 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;
};
// toys/posix/tar.c
diff --git a/android/device/generated/help.h b/android/device/generated/help.h
index e1e10a6..465475e 100644
--- a/android/device/generated/help.h
+++ b/android/device/generated/help.h
@@ -512,7 +512,7 @@
#define HELP_strings "usage: strings [-fo] [-t oxd] [-n LEN] [FILE...]\n\nDisplay printable strings in a binary file\n\n-f Show filename\n-n At least LEN characters form a string (default 4)\n-o Show offset (ala -t d)\n-t Show offset type (o=octal, d=decimal, x=hexadecimal)"
-#define HELP_split "usage: split [-a SUFFIX_LEN] [-b BYTES] [-l LINES] [-n PARTS] [INPUT [OUTPUT]]\n\nCopy INPUT (or stdin) data to a series of OUTPUT (or \"x\") files with\nalphabetically increasing suffix (aa, ab, ac... az, ba, bb...).\n\n-a Suffix length (default 2)\n-b BYTES/file (10, 10k, 10m, 10g...)\n-l LINES/file (default 1000)\n-n PARTS"
+#define HELP_split "usage: split [-a SUFFIX_LEN] [-b BYTES] [-l LINES] [-n PARTS] [INPUT [OUTPUT]]\n\nCopy INPUT (or stdin) data to a series of OUTPUT (or \"x\") files with\nalphabetically increasing suffix (aa, ab, ac... az, ba, bb...).\n\n-a Suffix length (default 2)\n-b BYTES/file (10, 10k, 10m, 10g...)\n-l LINES/file (default 1000)\n-n PARTS many equal length files"
#define HELP_sort "usage: sort [-Mbcdfginrsuz] [FILE...] [-k#[,#[x]] [-t X]] [-o FILE]\n\nSort all lines of text from input files (or stdin) to stdout.\n-M Month sort (jan, feb, etc)\n-V Version numbers (name-1.234-rc6.5b.tgz)\n-b Ignore leading blanks (or trailing blanks in second part of key)\n-c Check whether input is sorted\n-d Dictionary order (use alphanumeric and whitespace chars only)\n-f Force uppercase (case insensitive sort)\n-g General numeric sort (double precision with nan and inf)\n-i Ignore nonprinting characters\n-k Sort by \"key\" (see below)\n-n Numeric order (instead of alphabetical)\n-o Output to FILE instead of stdout\n-r Reverse\n-s Skip fallback sort (only sort with keys)\n-t Use a key separator other than whitespace\n-u Unique lines only\n-x Hexadecimal numerical sort\n-z Zero (null) terminated lines\n\nSorting by key looks at a subset of the words on each line. -k2 uses the\nsecond word to the end of the line, -k2,2 looks at only the second word,\n-k2,4 looks from the start of the second to the end of the fourth word.\n-k2.4,5 starts from the fourth character of the second word, to the end\nof the fifth word. Specifying multiple keys uses the later keys as tie\nbreakers, in order. A type specifier appended to a sort key (such as -2,2n)\napplies only to sorting that key."
diff --git a/android/device/generated/newtoys.h b/android/device/generated/newtoys.h
index ad171c5..a37ec0c 100644
--- a/android/device/generated/newtoys.h
+++ b/android/device/generated/newtoys.h
@@ -263,7 +263,7 @@
USE_SYSCTL(NEWTOY(sysctl, "^neNqwpaA[!ap][!aq][!aw][+aA]", TOYFLAG_SBIN))
USE_SYSLOGD(NEWTOY(syslogd,">0l#<1>8=8R:b#<0>99=1s#<0=200m#<0>71582787=20O:p:f:a:nSKLD", TOYFLAG_SBIN|TOYFLAG_STAYROOT))
USE_TAC(NEWTOY(tac, NULL, TOYFLAG_USR|TOYFLAG_BIN))
-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))
USE_TAR(NEWTOY(tar, "&(restrict)(full-time)(no-recursion)(numeric-owner)(no-same-permissions)(overwrite)(exclude)*(mode):(mtime):(group):(owner):(to-command):o(no-same-owner)p(same-permissions)k(keep-old)c(create)|h(dereference)x(extract)|t(list)|v(verbose)I(use-compress-program):J(xz)j(bzip2)z(gzip)S(sparse)O(to-stdout)P(absolute-names)m(touch)X(exclude-from)*T(files-from)*C(directory):f(file):a[!txc][!jzJa]", TOYFLAG_USR|TOYFLAG_BIN))
USE_TASKSET(NEWTOY(taskset, "<1^pa", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_STAYROOT))
USE_TCPSVD(NEWTOY(tcpsvd, "^<3c#=30<1C:b#=20<0u:l:hEv", TOYFLAG_USR|TOYFLAG_BIN))
diff --git a/android/linux/generated/flags.h b/android/linux/generated/flags.h
index 15e3f81..8ccf8f0 100644
--- a/android/linux/generated/flags.h
+++ b/android/linux/generated/flags.h
@@ -2948,9 +2948,9 @@
#undef FOR_tac
#endif
-// tail ?fFs#=1c-n-[-cn][-fF] ?fFs#=1c-n-[-cn][-fF]
+// tail ?fFs:c-n-[-cn][-fF] ?fFs:c-n-[-cn][-fF]
#undef OPTSTR_tail
-#define OPTSTR_tail "?fFs#=1c-n-[-cn][-fF]"
+#define OPTSTR_tail "?fFs:c-n-[-cn][-fF]"
#ifdef CLEANUP_tail
#undef CLEANUP_tail
#undef FOR_tail
diff --git a/android/linux/generated/globals.h b/android/linux/generated/globals.h
index 73cc23c..6eae6f7 100644
--- a/android/linux/generated/globals.h
+++ b/android/linux/generated/globals.h
@@ -1472,10 +1472,17 @@
// toys/posix/tail.c
struct tail_data {
- 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;
};
// toys/posix/tar.c
diff --git a/android/linux/generated/help.h b/android/linux/generated/help.h
index 161d57d..7b121e2 100644
--- a/android/linux/generated/help.h
+++ b/android/linux/generated/help.h
@@ -514,7 +514,7 @@
#define HELP_strings "usage: strings [-fo] [-t oxd] [-n LEN] [FILE...]\n\nDisplay printable strings in a binary file\n\n-f Show filename\n-n At least LEN characters form a string (default 4)\n-o Show offset (ala -t d)\n-t Show offset type (o=octal, d=decimal, x=hexadecimal)"
-#define HELP_split "usage: split [-a SUFFIX_LEN] [-b BYTES] [-l LINES] [-n PARTS] [INPUT [OUTPUT]]\n\nCopy INPUT (or stdin) data to a series of OUTPUT (or \"x\") files with\nalphabetically increasing suffix (aa, ab, ac... az, ba, bb...).\n\n-a Suffix length (default 2)\n-b BYTES/file (10, 10k, 10m, 10g...)\n-l LINES/file (default 1000)\n-n PARTS"
+#define HELP_split "usage: split [-a SUFFIX_LEN] [-b BYTES] [-l LINES] [-n PARTS] [INPUT [OUTPUT]]\n\nCopy INPUT (or stdin) data to a series of OUTPUT (or \"x\") files with\nalphabetically increasing suffix (aa, ab, ac... az, ba, bb...).\n\n-a Suffix length (default 2)\n-b BYTES/file (10, 10k, 10m, 10g...)\n-l LINES/file (default 1000)\n-n PARTS many equal length files"
#define HELP_sort "usage: sort [-Mbcdfginrsuz] [FILE...] [-k#[,#[x]] [-t X]] [-o FILE]\n\nSort all lines of text from input files (or stdin) to stdout.\n-M Month sort (jan, feb, etc)\n-V Version numbers (name-1.234-rc6.5b.tgz)\n-b Ignore leading blanks (or trailing blanks in second part of key)\n-c Check whether input is sorted\n-d Dictionary order (use alphanumeric and whitespace chars only)\n-f Force uppercase (case insensitive sort)\n-g General numeric sort (double precision with nan and inf)\n-i Ignore nonprinting characters\n-k Sort by \"key\" (see below)\n-n Numeric order (instead of alphabetical)\n-o Output to FILE instead of stdout\n-r Reverse\n-s Skip fallback sort (only sort with keys)\n-t Use a key separator other than whitespace\n-u Unique lines only\n-x Hexadecimal numerical sort\n-z Zero (null) terminated lines\n\nSorting by key looks at a subset of the words on each line. -k2 uses the\nsecond word to the end of the line, -k2,2 looks at only the second word,\n-k2,4 looks from the start of the second to the end of the fourth word.\n-k2.4,5 starts from the fourth character of the second word, to the end\nof the fifth word. Specifying multiple keys uses the later keys as tie\nbreakers, in order. A type specifier appended to a sort key (such as -2,2n)\napplies only to sorting that key."
diff --git a/android/linux/generated/newtoys.h b/android/linux/generated/newtoys.h
index ad171c5..a37ec0c 100644
--- a/android/linux/generated/newtoys.h
+++ b/android/linux/generated/newtoys.h
@@ -263,7 +263,7 @@
USE_SYSCTL(NEWTOY(sysctl, "^neNqwpaA[!ap][!aq][!aw][+aA]", TOYFLAG_SBIN))
USE_SYSLOGD(NEWTOY(syslogd,">0l#<1>8=8R:b#<0>99=1s#<0=200m#<0>71582787=20O:p:f:a:nSKLD", TOYFLAG_SBIN|TOYFLAG_STAYROOT))
USE_TAC(NEWTOY(tac, NULL, TOYFLAG_USR|TOYFLAG_BIN))
-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))
USE_TAR(NEWTOY(tar, "&(restrict)(full-time)(no-recursion)(numeric-owner)(no-same-permissions)(overwrite)(exclude)*(mode):(mtime):(group):(owner):(to-command):o(no-same-owner)p(same-permissions)k(keep-old)c(create)|h(dereference)x(extract)|t(list)|v(verbose)I(use-compress-program):J(xz)j(bzip2)z(gzip)S(sparse)O(to-stdout)P(absolute-names)m(touch)X(exclude-from)*T(files-from)*C(directory):f(file):a[!txc][!jzJa]", TOYFLAG_USR|TOYFLAG_BIN))
USE_TASKSET(NEWTOY(taskset, "<1^pa", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_STAYROOT))
USE_TCPSVD(NEWTOY(tcpsvd, "^<3c#=30<1C:b#=20<0u:l:hEv", TOYFLAG_USR|TOYFLAG_BIN))
diff --git a/android/mac/generated/flags.h b/android/mac/generated/flags.h
index d015b67..f032f90 100644
--- a/android/mac/generated/flags.h
+++ b/android/mac/generated/flags.h
@@ -2948,9 +2948,9 @@
#undef FOR_tac
#endif
-// tail ?fFs#=1c-n-[-cn][-fF] ?fFs#=1c-n-[-cn][-fF]
+// tail ?fFs:c-n-[-cn][-fF] ?fFs:c-n-[-cn][-fF]
#undef OPTSTR_tail
-#define OPTSTR_tail "?fFs#=1c-n-[-cn][-fF]"
+#define OPTSTR_tail "?fFs:c-n-[-cn][-fF]"
#ifdef CLEANUP_tail
#undef CLEANUP_tail
#undef FOR_tail
diff --git a/android/mac/generated/globals.h b/android/mac/generated/globals.h
index 73cc23c..6eae6f7 100644
--- a/android/mac/generated/globals.h
+++ b/android/mac/generated/globals.h
@@ -1472,10 +1472,17 @@
// toys/posix/tail.c
struct tail_data {
- 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;
};
// toys/posix/tar.c
diff --git a/android/mac/generated/help.h b/android/mac/generated/help.h
index 161d57d..7b121e2 100644
--- a/android/mac/generated/help.h
+++ b/android/mac/generated/help.h
@@ -514,7 +514,7 @@
#define HELP_strings "usage: strings [-fo] [-t oxd] [-n LEN] [FILE...]\n\nDisplay printable strings in a binary file\n\n-f Show filename\n-n At least LEN characters form a string (default 4)\n-o Show offset (ala -t d)\n-t Show offset type (o=octal, d=decimal, x=hexadecimal)"
-#define HELP_split "usage: split [-a SUFFIX_LEN] [-b BYTES] [-l LINES] [-n PARTS] [INPUT [OUTPUT]]\n\nCopy INPUT (or stdin) data to a series of OUTPUT (or \"x\") files with\nalphabetically increasing suffix (aa, ab, ac... az, ba, bb...).\n\n-a Suffix length (default 2)\n-b BYTES/file (10, 10k, 10m, 10g...)\n-l LINES/file (default 1000)\n-n PARTS"
+#define HELP_split "usage: split [-a SUFFIX_LEN] [-b BYTES] [-l LINES] [-n PARTS] [INPUT [OUTPUT]]\n\nCopy INPUT (or stdin) data to a series of OUTPUT (or \"x\") files with\nalphabetically increasing suffix (aa, ab, ac... az, ba, bb...).\n\n-a Suffix length (default 2)\n-b BYTES/file (10, 10k, 10m, 10g...)\n-l LINES/file (default 1000)\n-n PARTS many equal length files"
#define HELP_sort "usage: sort [-Mbcdfginrsuz] [FILE...] [-k#[,#[x]] [-t X]] [-o FILE]\n\nSort all lines of text from input files (or stdin) to stdout.\n-M Month sort (jan, feb, etc)\n-V Version numbers (name-1.234-rc6.5b.tgz)\n-b Ignore leading blanks (or trailing blanks in second part of key)\n-c Check whether input is sorted\n-d Dictionary order (use alphanumeric and whitespace chars only)\n-f Force uppercase (case insensitive sort)\n-g General numeric sort (double precision with nan and inf)\n-i Ignore nonprinting characters\n-k Sort by \"key\" (see below)\n-n Numeric order (instead of alphabetical)\n-o Output to FILE instead of stdout\n-r Reverse\n-s Skip fallback sort (only sort with keys)\n-t Use a key separator other than whitespace\n-u Unique lines only\n-x Hexadecimal numerical sort\n-z Zero (null) terminated lines\n\nSorting by key looks at a subset of the words on each line. -k2 uses the\nsecond word to the end of the line, -k2,2 looks at only the second word,\n-k2,4 looks from the start of the second to the end of the fourth word.\n-k2.4,5 starts from the fourth character of the second word, to the end\nof the fifth word. Specifying multiple keys uses the later keys as tie\nbreakers, in order. A type specifier appended to a sort key (such as -2,2n)\napplies only to sorting that key."
diff --git a/android/mac/generated/newtoys.h b/android/mac/generated/newtoys.h
index ad171c5..a37ec0c 100644
--- a/android/mac/generated/newtoys.h
+++ b/android/mac/generated/newtoys.h
@@ -263,7 +263,7 @@
USE_SYSCTL(NEWTOY(sysctl, "^neNqwpaA[!ap][!aq][!aw][+aA]", TOYFLAG_SBIN))
USE_SYSLOGD(NEWTOY(syslogd,">0l#<1>8=8R:b#<0>99=1s#<0=200m#<0>71582787=20O:p:f:a:nSKLD", TOYFLAG_SBIN|TOYFLAG_STAYROOT))
USE_TAC(NEWTOY(tac, NULL, TOYFLAG_USR|TOYFLAG_BIN))
-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))
USE_TAR(NEWTOY(tar, "&(restrict)(full-time)(no-recursion)(numeric-owner)(no-same-permissions)(overwrite)(exclude)*(mode):(mtime):(group):(owner):(to-command):o(no-same-owner)p(same-permissions)k(keep-old)c(create)|h(dereference)x(extract)|t(list)|v(verbose)I(use-compress-program):J(xz)j(bzip2)z(gzip)S(sparse)O(to-stdout)P(absolute-names)m(touch)X(exclude-from)*T(files-from)*C(directory):f(file):a[!txc][!jzJa]", TOYFLAG_USR|TOYFLAG_BIN))
USE_TASKSET(NEWTOY(taskset, "<1^pa", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_STAYROOT))
USE_TCPSVD(NEWTOY(tcpsvd, "^<3c#=30<1C:b#=20<0u:l:hEv", TOYFLAG_USR|TOYFLAG_BIN))
diff --git a/lib/dirtree.c b/lib/dirtree.c
index 43ecbd8..4023101 100644
--- a/lib/dirtree.c
+++ b/lib/dirtree.c
@@ -162,7 +162,6 @@
// according to the fddir() man page, the filehandle in the DIR * can still
// be externally used by things that don't lseek() it.
- // The extra parentheses are to shut the stupid compiler up.
while ((entry = readdir(dir))) {
if ((flags&DIRTREE_PROC) && !isdigit(*entry->d_name)) continue;
if (!(new = dirtree_add_node(node, entry->d_name, flags))) continue;
diff --git a/lib/lib.c b/lib/lib.c
index 9f9b813..49fd5dd 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -713,9 +713,9 @@
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 @@
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 cf1920f..7484eb8 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -112,7 +112,8 @@
// 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/cp.test b/tests/cp.test
index b0d5d3a..3e25f6c 100755
--- a/tests/cp.test
+++ b/tests/cp.test
@@ -140,6 +140,14 @@
'two\n' '' ''
testing '-u2' 'echo two>two; sleep .1; echo one>one; cp -u one two; cat two' \
'one\n' '' ''
+mkdir a b
+
+echo potato > a/one
+echo potato > a/two
+touch b/one b/two
+testing '-i' 'cp -ri a/. b/. 2>/dev/null; cmp -s a/one b/one || cmp -s a/one b/two && echo yes' \
+ 'yes\n' '' 'n\ny\n'
+rm -rf one two a b
# cp -r ../source destdir
# cp -r one/two/three missing
diff --git a/tests/split.test b/tests/split.test
index 4a52243..57270ff 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 "reassembly" \
'ls whang* | sort | xargs cat > reassembled && seq 1 20000 | diff -u reassembled - && echo yes' \
"yes\n" "" ""
+rm -f file whang* reassembled
-rm file whang* reassembled
+testing "-n" "split -n 3 input; md5sum xaa xab xac" \
+ "494bb8fb423bfa1a5fd66dd0b98f866d xaa\n449acfdbc692780de30a2df05c5d32aa xab\n15ab4be57aebe9a1e445195d5094036c xac\n" \
+ "$(seq 1 10000)" ""
+
diff --git a/tests/tail.test b/tests/tail.test
index 036f433..94644c8 100755
--- a/tests/tail.test
+++ b/tests/tail.test
@@ -73,3 +73,9 @@
'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/cp.c b/toys/posix/cp.c
index 8eaecc9..ed62f04 100644
--- a/toys/posix/cp.c
+++ b/toys/posix/cp.c
@@ -124,9 +124,10 @@
static int cp_node(struct dirtree *try)
{
int fdout = -1, cfd = try->parent ? try->parent->extra : AT_FDCWD,
+ save = DIRTREE_SAVE*(CFG_MV && toys.which->name[0] == 'm'), rc = 0,
tfd = dirtree_parentfd(try);
unsigned flags = toys.optflags;
- char *catch = try->parent ? try->name : TT.destname, *err = "%s";
+ char *s = 0, *catch = try->parent ? try->name : TT.destname, *err = "%s";
struct stat cst;
if (!dirtree_notdotdot(try)) return 0;
@@ -135,8 +136,13 @@
if (S_ISDIR(try->st.st_mode) && try->again) {
fdout = try->extra;
err = 0;
- } else {
+ // If mv child had a problem, free data and don't try to delete parent dir.
+ if (try->child) {
+ save = 0;
+ llist_traverse(try->child, free);
+ }
+ } else {
// -d is only the same as -r for symlinks, not for directories
if (S_ISLNK(try->st.st_mode) && (flags & FLAG_d)) flags |= FLAG_r;
@@ -150,35 +156,27 @@
error_msg("'%s' is '%s'", catch, err = dirtree_path(try, 0));
free(err);
- return 0;
+ return save;
}
// Handle -inuvF
-
if (!faccessat(cfd, catch, F_OK, 0) && !S_ISDIR(cst.st_mode)) {
- char *s;
-
- if (S_ISDIR(try->st.st_mode)) {
+ if (S_ISDIR(try->st.st_mode))
error_msg("dir at '%s'", s = dirtree_path(try, 0));
- free(s);
- return 0;
- } else if ((flags & FLAG_F) && unlinkat(cfd, catch, 0)) {
+ else if ((flags & FLAG_F) && unlinkat(cfd, catch, 0))
error_msg("unlink '%s'", catch);
- return 0;
- } else if (flags & FLAG_n) return 0;
- else if ((flags & FLAG_u) && nanodiff(&try->st.st_mtim, &cst.st_mtim)>0)
- return 0;
else if (flags & FLAG_i) {
fprintf(stderr, "%s: overwrite '%s'", toys.which->name,
s = dirtree_path(try, 0));
- free(s);
- if (!yesno(0)) return 0;
- }
+ if (yesno(0)) rc++;
+ } else if (!((flags&FLAG_u) && nanodiff(&try->st.st_mtim, &cst.st_mtim)>0)
+ && !(flags & FLAG_n)) rc++;
+ free(s);
+ if (!rc) return save;
}
if (flags & FLAG_v) {
- char *s = dirtree_path(try, 0);
- printf("%s '%s'\n", toys.which->name, s);
+ printf("%s '%s'\n", toys.which->name, s = dirtree_path(try, 0));
free(s);
}
@@ -246,13 +244,11 @@
} else if (!S_ISREG(try->st.st_mode)
&& (try->parent || (flags & (FLAG_a|FLAG_P|FLAG_r))))
{
- int i;
-
// make symlink, or make block/char/fifo/socket
if (S_ISLNK(try->st.st_mode)
- ? ((i = readlinkat0(tfd, try->name, toybuf, sizeof(toybuf))) &&
- ((!unlinkat(cfd, catch, 0) || ENOENT == errno) &&
- !symlinkat(toybuf, cfd, catch)))
+ ? readlinkat0(tfd, try->name, toybuf, sizeof(toybuf)) &&
+ (!unlinkat(cfd, catch, 0) || ENOENT == errno) &&
+ !symlinkat(toybuf, cfd, catch)
: !mknodat(cfd, catch, try->st.st_mode, try->st.st_rdev))
{
err = 0;
@@ -307,8 +303,6 @@
// Did we make a thing?
if (fdout != -1) {
- int rc;
-
// Inability to set --preserve isn't fatal, some require root access.
// ownership
@@ -342,23 +336,21 @@
xclose(fdout);
}
- if (CFG_MV && toys.which->name[0] == 'm')
+ if (save)
if (unlinkat(tfd, try->name, S_ISDIR(try->st.st_mode) ? AT_REMOVEDIR :0))
err = "%s";
}
if (err) {
- char *f = 0;
-
if (catch == try->name) {
- f = dirtree_path(try, 0);
+ s = dirtree_path(try, 0);
while (try->parent) try = try->parent;
- catch = xmprintf("%s%s", TT.destname, f+strlen(try->name));
- free(f);
- f = catch;
- }
+ catch = xmprintf("%s%s", TT.destname, s+strlen(try->name));
+ free(s);
+ s = catch;
+ } else s = 0;
perror_msg(err, catch);
- free(f);
+ free(s);
}
return 0;
}
diff --git a/toys/posix/split.c b/toys/posix/split.c
index 5161aff..293343b 100644
--- a/toys/posix/split.c
+++ b/toys/posix/split.c
@@ -22,7 +22,7 @@
-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 @@
// 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 @@
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 842814c..0d13ab0 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 @@
#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 @@
return 1;
}
-static void show_new(int fd, char *path)
+// For -f and -F
+static void tail_continue()
{
- 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()
-{
+ long long pos;
+ char *path;
struct stat sb;
- int i, old_fd;
+ int i = 0, fd, len;
- 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);
+ 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 @@
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 @@
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 @@
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();
}