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