From: Jim Meyering Date: Tue, 22 Aug 2000 11:12:01 +0000 (+0000) Subject: Don't even try to use lseek on character devices. X-Git-Tag: FILEUTILS-4_0z~126 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ba9af72e94e7441723352a30fba4847654881071;p=thirdparty%2Fcoreutils.git Don't even try to use lseek on character devices. (buggy_lseek_support): New function. (skip): Use it. Reported by Martin Gallant via Michael Stone. --- diff --git a/src/dd.c b/src/dd.c index 2a89b6b014..ef5a9ed626 100644 --- a/src/dd.c +++ b/src/dd.c @@ -718,6 +718,37 @@ swab_buffer (unsigned char *buf, size_t *nread) return ++bufstart; } +/* Return nonzero iff the file referenced by FDESC is of a type for + which lseek's return value is known to be invalid on some systems. + Otherwise, return zero. + For example, return nonzero if FDESC references a character device + (on any system) because the lseek on many Linux systems incorrectly + returns `0' for tape devices, even though the function fails to + perform the requested operation. In that case, lseek should return + nonzero and set errno. */ + +static int +buggy_lseek_support (int fdesc) +{ + /* We have to resort to this because on some systems, lseek doesn't work + on some special files but doesn't return an error, either. + In particular, the Linux tape drivers are a problem. + For example, when I did the following using dd-4.0y or earlier on a + Linux-2.2.17 system with a Exabyte SCSI tape drive: + + dev=/dev/nst0 + reset='mt -f $dev rewind; mt -f $dev fsf 1' + eval $reset; dd if=$dev bs=32k of=out1 + eval $reset; dd if=$dev bs=32k of=out2 skip=1 + + the resulting files, out1 and out2, would compare equal. */ + + struct stat stats; + + return (fstat (fdesc, &stats) == 0 + && (S_ISCHR (stats.st_mode))); +} + /* Throw away RECORDS blocks of BLOCKSIZE bytes on file descriptor FDESC, which is open with read permission for FILE. Store up to BLOCKSIZE bytes of the data at a time in BUF, if necessary. */ @@ -732,6 +763,7 @@ skip (int fdesc, char *file, uintmax_t records, size_t blocksize, operation, fall back on using read. */ o = records * blocksize; if (o / blocksize != records + || buggy_lseek_support (fdesc) || lseek (fdesc, o, SEEK_SET) == -1) { while (records-- > 0)