1#!/bin/sh
2# exercise head -c
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_ head
21getlimits_
22
23vm=$(get_min_ulimit_v_ head -c1 /dev/null) \
24  || skip_ "this shell lacks ulimit support"
25
26# exercise the fix of 2001-08-18, based on test case from Ian Bruce
27echo abc > in || framework_failure_
28(head -c1; head -c1) < in > out || fail=1
29case "$(cat out)" in
30  ab) ;;
31  *) fail=1 ;;
32esac
33
34# Test for a bug in coreutils 5.0.1 through 8.22.
35printf 'abc\ndef\n' > in1 || framework_failure_
36(dd bs=1 skip=1 count=0 status=none && head -c-4) < in1 > out1 || fail=1
37case "$(cat out1)" in
38  bc) ;;
39  *) fail=1 ;;
40esac
41
42# Only allocate memory as needed.
43# Coreutils <= 8.21 would allocate memory up front
44# based on the value passed to -c
45(ulimit -v $(($vm+8000)) && head --bytes=-$SSIZE_MAX < /dev/null) || fail=1
46
47# Make sure it works on funny files in /proc and /sys.
48
49for file in /proc/version /sys/kernel/profiling; do
50  if test -r $file; then
51    cp -f $file copy &&
52    head -c -1 copy > exp1 || framework_failure_
53
54    head -c -1 $file > out1 || fail=1
55    compare exp1 out1 || fail=1
56  fi
57done
58
59Exit $fail
60