1#!/bin/sh
2# test diagnostics are printed when seeking too far in seekable files.
3
4# Copyright (C) 2008-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_ dd
21require_sparse_support_ # for 'truncate --size=$OFF_T_MAX'
22eval $(getlimits) # for OFF_T limits
23export LC_ALL=C
24
25printf "1234" > file || framework_failure_
26
27echo "\
28dd: 'standard input': cannot skip to specified offset
290+0 records in
300+0 records out" > skip_err || framework_failure_
31
32# skipping beyond number of blocks in file should issue a warning
33dd bs=1 skip=5 count=0 status=noxfer < file 2> err || fail=1
34compare skip_err err || fail=1
35
36# skipping beyond number of bytes in file should issue a warning
37dd bs=3 skip=2 count=0 status=noxfer < file 2> err || fail=1
38compare skip_err err || fail=1
39
40# skipping beyond number of blocks in pipe should issue a warning
41cat file | dd bs=1 skip=5 count=0 status=noxfer 2> err || fail=1
42compare skip_err err || fail=1
43
44# skipping beyond number of bytes in pipe should issue a warning
45cat file | dd bs=3 skip=2 count=0 status=noxfer 2> err || fail=1
46compare skip_err err || fail=1
47
48# Check seeking beyond file already offset into
49# skipping beyond number of blocks in file should issue a warning
50(dd bs=1 skip=1 count=0 2>/dev/null &&
51 dd bs=1 skip=4 status=noxfer 2> err) < file || fail=1
52compare skip_err err || fail=1
53
54# Check seeking beyond file already offset into
55# skipping beyond number of bytes in file should issue a warning
56(dd bs=1 skip=1 count=0 2>/dev/null &&
57 dd bs=2 skip=2 status=noxfer 2> err) < file || fail=1
58compare skip_err err || fail=1
59
60# seeking beyond end of file is OK
61dd bs=1 seek=5 count=0 status=noxfer > file 2> err || fail=1
62echo "0+0 records in
630+0 records out" > err_ok || framework_failure_
64compare err_ok err || fail=1
65
66# skipping > OFF_T_MAX should fail immediately
67dd bs=1 skip=$OFF_T_OFLOW count=0 status=noxfer < file 2> err && fail=1
68# error message should be "... invalid number: strerror(EOVERFLOW)"
69grep "invalid number:" err >/dev/null || fail=1
70dd bs=1 skip=${OFF_T_OFLOW}x$OFF_T_OFLOW count=0 status=noxfer < file 2> err &&
71    fail=1
72grep "invalid number:" err >/dev/null || fail=1
73
74# skipping > max file size should fail immediately
75if ! truncate --size=$OFF_T_MAX in 2>/dev/null; then
76  # truncate is to ensure file system doesn't actually support OFF_T_MAX files
77  dd bs=1 skip=$OFF_T_MAX count=0 status=noxfer < file 2> err \
78    && lseek_ok=yes \
79    || lseek_ok=no
80
81  if test $lseek_ok = yes; then
82    # On Solaris 10 at least, lseek(>max file size) succeeds,
83    # so just check for the skip warning.
84    compare skip_err err || fail=1
85  else
86    # On Linux kernels at least, lseek(>max file size) fails.
87    # error message should be "... cannot skip: strerror(EINVAL)"
88    grep "cannot skip:" err >/dev/null || fail=1
89  fi
90fi
91
92Exit $fail
93