dd: don’t trust st_size on /proc/files
[coreutils.git] / tests / misc / stdbuf.sh
blobe53b4a3f0b86a1c94d867c732b3e0a185feb0a1c
1 #!/bin/sh
2 # Exercise stdbuf functionality
4 # Copyright (C) 2009-2024 Free Software Foundation, Inc.
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.
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.
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/>.
19 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
20 print_ver_ stdbuf env
22 getlimits_
24 # stdbuf fails when the absolute top build dir name contains e.g.,
25 # space, TAB, NL
26 lf='
28 case $abs_top_builddir in
29 *[\\\"\#\$\&\'\`$lf\ \ ]*)
30 skip_ "unsafe absolute build directory name: $abs_top_builddir";;
31 esac
33 # Use a fifo rather than a pipe in the tests below
34 # so that the producer (uniq) will wait until the
35 # consumer (dd) opens the fifo therefore increasing
36 # the chance that dd will read the data from each
37 # write separately.
38 mkfifo_or_skip_ fifo
41 # Verify input parameter checking
42 stdbuf -o1 true || fail=1 # verify size syntax
43 stdbuf -oK true || fail=1 # verify size syntax
44 stdbuf -o0 true || fail=1 # verify unbuffered syntax
45 stdbuf -oL true || fail=1 # verify line buffered syntax
47 # Capital 'L' required
48 # Internal error is a particular status
49 returns_ 125 stdbuf -ol true || fail=1
51 returns_ 125 stdbuf -o$SIZE_OFLOW true || fail=1 # size too large
52 returns_ 125 stdbuf -iL true || fail=1 # line buffering stdin disallowed
53 returns_ 125 stdbuf true || fail=1 # a buffering mode must be specified
54 stdbuf -i0 -o0 -e0 true || fail=1 #check all files
55 returns_ 126 env . && { returns_ 126 stdbuf -o1 . || fail=1; } # invalid command
56 returns_ 127 stdbuf -o1 no_such || fail=1 # no such command
58 # Terminate any background processes
59 cleanup_() { kill $pid 2>/dev/null && wait $pid; }
61 # Ensure line buffering stdout takes effect
62 stdbuf_linebuffer()
64 local delay="$1"
66 printf '1\n' > exp
67 > out || framework_failure_
68 dd count=1 if=fifo > out 2> err & pid=$!
69 (printf '1\n'; sleep $delay; printf '2\n') | stdbuf -oL uniq > fifo
70 wait $pid
71 compare exp out
74 retry_delay_ stdbuf_linebuffer .1 6 || fail=1
76 stdbuf_unbuffer()
78 local delay="$1"
80 # Ensure un buffering stdout takes effect
81 printf '1\n' > exp
82 > out || framework_failure_
83 dd count=1 if=fifo > out 2> err & pid=$!
84 (printf '1\n'; sleep $delay; printf '2\n') | stdbuf -o0 uniq > fifo
85 wait $pid
86 compare exp out
89 retry_delay_ stdbuf_unbuffer .1 6 || fail=1
91 # Ensure un buffering stdin takes effect
92 # The following works for me, but is racy. I.e., we're depending
93 # on dd to run and close the fifo before the second write by uniq.
94 # If we add a sleep, then we're just testing -oL
95 # printf '3\n' > exp
96 # dd count=1 if=fifo > /dev/null 2> err &
97 # printf '1\n\2\n3\n' | (stdbuf -i0 -oL uniq > fifo; cat) > out
98 # wait # for dd to complete
99 # compare exp out || fail=1
100 # One could remove the need for dd (used to close the fifo to get uniq to quit
101 # early), if head -n1 read stdin char by char. Note uniq | head -c2 doesn't
102 # suffice due to the buffering implicit in the pipe. sed currently does read
103 # stdin char by char, so we can test with 'sed 1q'. However I'm wary about
104 # adding this dependency on a program outside of coreutils.
105 # printf '2\n' > exp
106 # printf '1\n2\n' | (stdbuf -i0 sed 1q >/dev/null; cat) > out
107 # compare exp out || fail=1
109 # Ensure block buffering stdout takes effect
110 # We don't currently test block buffering failures as
111 # this doesn't work on GLIBC-2.7 or GLIBC-2.9 at least.
112 # stdbuf_blockbuffer()
114 # local delay="$1"
116 # printf '1\n2\n' > exp
117 # dd count=1 if=fifo > out 2> err &
118 # (printf '1\n'; sleep $delay; printf '2\n') | stdbuf -o4 uniq > fifo
119 # wait # for dd to complete
120 # compare exp out
123 # retry_delay_ stdbuf_blockbuffer .1 6 || fail=1
125 Exit $fail