1#!/bin/sh
2# Exercise stdbuf functionality
3
4# Copyright (C) 2009-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_ stdbuf env
21
22getlimits_
23
24# stdbuf fails when the absolute top build dir name contains e.g.,
25# space, TAB, NL
26lf='
27'
28case $abs_top_builddir in
29  *[\\\"\#\$\&\'\`$lf\ \	]*)
30    skip_ "unsafe absolute build directory name: $abs_top_builddir";;
31esac
32
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.
38mkfifo_or_skip_ fifo
39
40
41# Verify input parameter checking
42stdbuf -o1 true || fail=1 # verify size syntax
43stdbuf -oK true || fail=1 # verify size syntax
44stdbuf -o0 true || fail=1 # verify unbuffered syntax
45stdbuf -oL true || fail=1 # verify line buffered syntax
46
47# Capital 'L' required
48# Internal error is a particular status
49returns_ 125 stdbuf -ol true || fail=1
50
51returns_ 125 stdbuf -o$SIZE_OFLOW true || fail=1 # size too large
52returns_ 125 stdbuf -iL true || fail=1 # line buffering stdin disallowed
53returns_ 125 stdbuf true || fail=1 # a buffering mode must be specified
54stdbuf -i0 -o0 -e0 true || fail=1 #check all files
55returns_ 126 env . && { returns_ 126 stdbuf -o1 . || fail=1; } # invalid command
56returns_ 127 stdbuf -o1 no_such || fail=1 # no such command
57
58# Terminate any background processes
59cleanup_() { kill $pid 2>/dev/null && wait $pid; }
60
61# Ensure line buffering stdout takes effect
62stdbuf_linebuffer()
63{
64  local delay="$1"
65
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
72}
73
74retry_delay_ stdbuf_linebuffer .1 6 || fail=1
75
76stdbuf_unbuffer()
77{
78  local delay="$1"
79
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
87}
88
89retry_delay_ stdbuf_unbuffer .1 6 || fail=1
90
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
108
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()
113   # {
114   #   local delay="$1"
115   #
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
121   # }
122   #
123   # retry_delay_ stdbuf_blockbuffer .1 6 || fail=1
124
125Exit $fail
126