Implement seekability for disk devices (not just regular files).

Also, fix pos_out() to do the same checks pos_in() did.

Done for:	jdp, luigi, the good of the world
This commit is contained in:
Brian Feldman 1999-07-13 18:44:56 +00:00
parent 4dc0c8f521
commit 769e5815e3
3 changed files with 13 additions and 11 deletions

View File

@ -46,11 +46,12 @@ static char const copyright[] =
static char sccsid[] = "@(#)dd.c 8.5 (Berkeley) 4/2/94"; static char sccsid[] = "@(#)dd.c 8.5 (Berkeley) 4/2/94";
#endif #endif
static const char rcsid[] = static const char rcsid[] =
"$Id: dd.c,v 1.16 1999/04/25 21:13:33 imp Exp $"; "$Id: dd.c,v 1.18 1999/06/20 14:58:51 green Exp $";
#endif /* not lint */ #endif /* not lint */
#include <sys/param.h> #include <sys/param.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/diskslice.h>
#include <sys/mtio.h> #include <sys/mtio.h>
#include <ctype.h> #include <ctype.h>
@ -224,10 +225,14 @@ getfdtype(io)
IO *io; IO *io;
{ {
struct mtget mt; struct mtget mt;
struct diskslices ds;
struct stat sb; struct stat sb;
if (fstat(io->fd, &sb)) if (fstat(io->fd, &sb))
err(1, "%s", io->name); err(1, "%s", io->name);
if ((S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) &&
ioctl(io->fd, DIOCGSLICEINFO, &ds) != -1)
io->flags |= ISDISK;
if (S_ISCHR(sb.st_mode)) if (S_ISCHR(sb.st_mode))
io->flags |= ioctl(io->fd, MTIOCGET, &mt) ? ISCHR : ISTAPE; io->flags |= ioctl(io->fd, MTIOCGET, &mt) ? ISCHR : ISTAPE;
else if (lseek(io->fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) else if (lseek(io->fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE)

View File

@ -35,7 +35,7 @@
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* @(#)dd.h 8.3 (Berkeley) 4/2/94 * @(#)dd.h 8.3 (Berkeley) 4/2/94
* $Id: dd.h,v 1.8 1998/02/11 02:23:31 asami Exp $ * $Id: dd.h,v 1.10 1999/06/20 14:58:51 green Exp $
*/ */
/* Input/output stream state. */ /* Input/output stream state. */
@ -49,7 +49,8 @@ typedef struct {
#define ISCHR 0x01 /* character device (warn on short) */ #define ISCHR 0x01 /* character device (warn on short) */
#define ISPIPE 0x02 /* pipe (not truncatable) */ #define ISPIPE 0x02 /* pipe (not truncatable) */
#define ISTAPE 0x04 /* tape (not seekable) */ #define ISTAPE 0x04 /* tape (not seekable) */
#define NOREAD 0x08 /* not readable */ #define ISDISK 0x08 /* disk (valid to seek on) */
#define NOREAD 0x10 /* not readable */
u_int flags; u_int flags;
char *name; /* name */ char *name; /* name */

View File

@ -40,7 +40,7 @@
static char sccsid[] = "@(#)position.c 8.3 (Berkeley) 4/2/94"; static char sccsid[] = "@(#)position.c 8.3 (Berkeley) 4/2/94";
#endif #endif
static const char rcsid[] = static const char rcsid[] =
"$Id: position.c,v 1.8 1998/05/13 07:33:54 charnier Exp $"; "$Id: position.c,v 1.10 1999/06/20 14:58:55 green Exp $";
#endif /* not lint */ #endif /* not lint */
#include <sys/types.h> #include <sys/types.h>
@ -66,7 +66,7 @@ pos_in()
off_t cnt; off_t cnt;
/* If not a character, pipe or tape device, try to seek on it. */ /* If not a character, pipe or tape device, try to seek on it. */
if (!(in.flags & (ISCHR|ISPIPE|ISTAPE))) { if (!(in.flags & (ISCHR|ISPIPE|ISTAPE)) || in.flags & ISDISK) {
if (lseek(in.fd, in.offset * in.dbsz, SEEK_CUR) == -1) if (lseek(in.fd, in.offset * in.dbsz, SEEK_CUR) == -1)
err(1, "%s", in.name); err(1, "%s", in.name);
return; return;
@ -121,12 +121,8 @@ pos_out()
off_t cnt; off_t cnt;
ssize_t n; ssize_t n;
/* /* If not a character, pipe or tape device, try to seek on it. */
* If not a tape, try seeking on the file. Seeking on a pipe is if (!(out.flags & (ISCHR|ISPIPE|ISTAPE)) || out.flags & ISDISK) {
* going to fail, but don't protect the user -- they shouldn't
* have specified the seek operand.
*/
if (!(out.flags & ISTAPE)) {
if (lseek(out.fd, out.offset * out.dbsz, SEEK_SET) == -1) if (lseek(out.fd, out.offset * out.dbsz, SEEK_SET) == -1)
err(1, "%s", out.name); err(1, "%s", out.name);
return; return;