diff options
| author | Rob Landley <rob@landley.net> | 2021-07-05 00:57:46 -0500 | 
|---|---|---|
| committer | Rob Landley <rob@landley.net> | 2021-07-05 00:57:46 -0500 | 
| commit | 3fbacb1f5c5eab1727d0bed0bd50d659af854ec7 (patch) | |
| tree | d4b067f98a2844b560e148f10119de3d77be6359 | |
| parent | 98613d454671ebc3e1a761788453542cd93e6ada (diff) | |
| download | toybox-3fbacb1f5c5eab1727d0bed0bd50d659af854ec7.tar.gz | |
Add split -n test, handle more than one leftover byte, clarify help text.
| -rwxr-xr-x | tests/split.test | 18 | ||||
| -rw-r--r-- | toys/posix/split.c | 14 | 
2 files changed, 15 insertions, 17 deletions
| 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/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); | 
