Deleted Added
full compact
position.c (256281) position.c (321140)
1/*-
2 * Copyright (c) 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Keith Muller of the University of California, San Diego and Lance
7 * Visser of Convex Computer Corporation.
8 *

--- 23 unchanged lines hidden (view full) ---

32 */
33
34#ifndef lint
35#if 0
36static char sccsid[] = "@(#)position.c 8.3 (Berkeley) 4/2/94";
37#endif
38#endif /* not lint */
39#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Keith Muller of the University of California, San Diego and Lance
7 * Visser of Convex Computer Corporation.
8 *

--- 23 unchanged lines hidden (view full) ---

32 */
33
34#ifndef lint
35#if 0
36static char sccsid[] = "@(#)position.c 8.3 (Berkeley) 4/2/94";
37#endif
38#endif /* not lint */
39#include <sys/cdefs.h>
40__FBSDID("$FreeBSD: stable/10/bin/dd/position.c 250469 2013-05-10 18:43:36Z eadler $");
40__FBSDID("$FreeBSD: stable/10/bin/dd/position.c 321140 2017-07-18 17:36:25Z ngie $");
41
42#include <sys/types.h>
43#include <sys/mtio.h>
44
45#include <err.h>
46#include <errno.h>
47#include <inttypes.h>
41
42#include <sys/types.h>
43#include <sys/mtio.h>
44
45#include <err.h>
46#include <errno.h>
47#include <inttypes.h>
48#include <limits.h>
48#include <signal.h>
49#include <unistd.h>
50
51#include "dd.h"
52#include "extern.h"
53
49#include <signal.h>
50#include <unistd.h>
51
52#include "dd.h"
53#include "extern.h"
54
55static off_t
56seek_offset(IO *io)
57{
58 off_t n;
59 size_t sz;
60
61 n = io->offset;
62 sz = io->dbsz;
63
64 _Static_assert(sizeof(io->offset) == sizeof(int64_t), "64-bit off_t");
65
66 /*
67 * If the lseek offset will be negative, verify that this is a special
68 * device file. Some such files (e.g. /dev/kmem) permit "negative"
69 * offsets.
70 *
71 * Bail out if the calculation of a file offset would overflow.
72 */
73 if ((io->flags & ISCHR) == 0 && (n < 0 || n > OFF_MAX / (ssize_t)sz))
74 errx(1, "seek offsets cannot be larger than %jd",
75 (intmax_t)OFF_MAX);
76 else if ((io->flags & ISCHR) != 0 && (uint64_t)n > UINT64_MAX / sz)
77 errx(1, "seek offsets cannot be larger than %ju",
78 (uintmax_t)UINT64_MAX);
79
80 return ((off_t)( (uint64_t)n * sz ));
81}
82
54/*
55 * Position input/output data streams before starting the copy. Device type
56 * dependent. Seekable devices use lseek, and the rest position by reading.
57 * Seeking past the end of file can cause null blocks to be written to the
58 * output.
59 */
60void
61pos_in(void)
62{
63 off_t cnt;
64 int warned;
65 ssize_t nr;
66 size_t bcnt;
67
68 /* If known to be seekable, try to seek on it. */
69 if (in.flags & ISSEEK) {
70 errno = 0;
83/*
84 * Position input/output data streams before starting the copy. Device type
85 * dependent. Seekable devices use lseek, and the rest position by reading.
86 * Seeking past the end of file can cause null blocks to be written to the
87 * output.
88 */
89void
90pos_in(void)
91{
92 off_t cnt;
93 int warned;
94 ssize_t nr;
95 size_t bcnt;
96
97 /* If known to be seekable, try to seek on it. */
98 if (in.flags & ISSEEK) {
99 errno = 0;
71 if (lseek(in.fd, in.offset * in.dbsz, SEEK_CUR) == -1 &&
100 if (lseek(in.fd, seek_offset(&in), SEEK_CUR) == -1 &&
72 errno != 0)
73 err(1, "%s", in.name);
74 return;
75 }
76
77 /* Don't try to read a really weird amount (like negative). */
78 if (in.offset < 0)
79 errx(1, "%s: illegal offset", "iseek/skip");

--- 51 unchanged lines hidden (view full) ---

131
132 /*
133 * If not a tape, try seeking on the file. Seeking on a pipe is
134 * going to fail, but don't protect the user -- they shouldn't
135 * have specified the seek operand.
136 */
137 if (out.flags & (ISSEEK | ISPIPE)) {
138 errno = 0;
101 errno != 0)
102 err(1, "%s", in.name);
103 return;
104 }
105
106 /* Don't try to read a really weird amount (like negative). */
107 if (in.offset < 0)
108 errx(1, "%s: illegal offset", "iseek/skip");

--- 51 unchanged lines hidden (view full) ---

160
161 /*
162 * If not a tape, try seeking on the file. Seeking on a pipe is
163 * going to fail, but don't protect the user -- they shouldn't
164 * have specified the seek operand.
165 */
166 if (out.flags & (ISSEEK | ISPIPE)) {
167 errno = 0;
139 if (lseek(out.fd, out.offset * out.dbsz, SEEK_CUR) == -1 &&
168 if (lseek(out.fd, seek_offset(&out), SEEK_CUR) == -1 &&
140 errno != 0)
141 err(1, "%s", out.name);
142 return;
143 }
144
145 /* Don't try to read a really weird amount (like negative). */
146 if (out.offset < 0)
147 errx(1, "%s: illegal offset", "oseek/seek");

--- 39 unchanged lines hidden ---
169 errno != 0)
170 err(1, "%s", out.name);
171 return;
172 }
173
174 /* Don't try to read a really weird amount (like negative). */
175 if (out.offset < 0)
176 errx(1, "%s: illegal offset", "oseek/seek");

--- 39 unchanged lines hidden ---