1#!/bin/sh
2# various csplit tests
3
4# Copyright (C) 2001-2023 Free Software Foundation, Inc.
5
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
19. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
20print_ver_ csplit
21
22
23# csplit could get a failed assertion to 2.0.17
24(echo a; echo; echo) > in
25csplit in '/^$/' 2 > out || fail=1
26cat <<EOF > exp
272
280
292
30EOF
31compare exp out || fail=1
32rm -f in out exp
33
34# Ensure that xx02 contains just two newlines.
35# This would fail due to reading from freed buffer with coreutils-5.0.91.
36printf '\n\n' > exp
37cp xx02 out || fail=1
38compare exp out || fail=1
39rm -f in out exp
40
41# csplit would infloop
42(echo; echo a) > in
43csplit in '/a/-1' '{*}' > out || fail=1
44cat <<EOF > exp
450
463
47EOF
48compare exp out || fail=1
49rm -f in out exp
50
51# 'echo |csplit - 1 1' used to abort.
52echo > in
53csplit in 1 1 > out 2> err || fail=1
54cat <<EOF > exp
550
560
571
58EOF
59compare exp out || fail=1
60cat <<\EOF > experr
61csplit: warning: line number '1' is the same as preceding line number
62EOF
63compare experr err || fail=1
64rm -f in out exp err experr
65
66# 'echo | csplit -b '%0#6.3x' - 1' incorrectly warned about the format
67# up through coreutils 8.6.
68echo > in
69csplit -b '%0#6.3x' in 1 > out 2> err || fail=1
70cat <<EOF > exp
710
721
73EOF
74compare exp out || fail=1
75touch experr
76compare experr err || fail=1
77compare 'xx   000' experr || fail=1
78compare 'xx 0x001' in || fail=1
79rm -f in out exp err experr xx*
80
81# make sure 'csplit FILE 0' fails.
82echo > in
83csplit in 0 > out 2> err && fail=1
84csplit in 2 1 > out 2>> err && fail=1
85csplit in 3 3 > out 2>> err && fail=1
86cat <<\EOF > experr
87csplit: 0: line number must be greater than zero
88csplit: line number '1' is smaller than preceding line number, 2
89csplit: warning: line number '3' is the same as preceding line number
90csplit: '3': line number out of range
91EOF
92compare experr err || fail=1
93
94# Ensure that lines longer than the initial buffer length don't cause
95# trouble (e.g. reading from freed memory, resulting in corrupt output).
96# This test failed at least in coreutils-5.2.1 and 5.3.0, and was fixed
97# in 5.3.1.
98rm -f in out exp err experr xx??
99printf 'x%8199s\nx\n%8199s\nx\n' x x > in
100csplit in '/x\{1\}/' '{*}' > /dev/null || fail=1
101cat xx?? | compare - in || fail=1
102
103Exit $fail
104