/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1991, 1993, 19944* The Regents of the University of California. All rights reserved.5*6* This code is derived from software contributed to Berkeley by7* Keith Muller of the University of California, San Diego and Lance8* Visser of Convex Computer Corporation.9*10* Redistribution and use in source and binary forms, with or without11* modification, are permitted provided that the following conditions12* are met:13* 1. Redistributions of source code must retain the above copyright14* notice, this list of conditions and the following disclaimer.15* 2. Redistributions in binary form must reproduce the above copyright16* notice, this list of conditions and the following disclaimer in the17* documentation and/or other materials provided with the distribution.18* 3. Neither the name of the University nor the names of its contributors19* may be used to endorse or promote products derived from this software20* without specific prior written permission.21*22* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND23* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE24* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE25* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE26* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL27* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS28* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)29* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT30* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY31* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF32* SUCH DAMAGE.33*/3435#include <sys/types.h>36#include <sys/mtio.h>3738#include <err.h>39#include <errno.h>40#include <inttypes.h>41#include <limits.h>42#include <signal.h>43#include <unistd.h>4445#include "dd.h"46#include "extern.h"4748static off_t49seek_offset(IO *io)50{51off_t n;52size_t sz;5354n = io->offset;55sz = io->dbsz;5657_Static_assert(sizeof(io->offset) == sizeof(int64_t), "64-bit off_t");5859/*60* If the lseek offset will be negative, verify that this is a special61* device file. Some such files (e.g. /dev/kmem) permit "negative"62* offsets.63*64* Bail out if the calculation of a file offset would overflow.65*/66if ((io->flags & ISCHR) == 0 && (n < 0 || n > OFF_MAX / (ssize_t)sz))67errx(1, "seek offsets cannot be larger than %jd",68(intmax_t)OFF_MAX);69else if ((io->flags & ISCHR) != 0 && (uint64_t)n > UINT64_MAX / sz)70errx(1, "seek offsets cannot be larger than %ju",71(uintmax_t)UINT64_MAX);7273return ((off_t)( (uint64_t)n * sz ));74}7576/*77* Position input/output data streams before starting the copy. Device type78* dependent. Seekable devices use lseek, and the rest position by reading.79* Seeking past the end of file can cause null blocks to be written to the80* output.81*/82void83pos_in(void)84{85off_t cnt;86int warned;87ssize_t nr;88size_t bcnt;8990/* If known to be seekable, try to seek on it. */91if (in.flags & ISSEEK) {92errno = 0;93if (lseek(in.fd, seek_offset(&in), SEEK_CUR) == -1 &&94errno != 0)95err(1, "%s", in.name);96return;97}9899/* Don't try to read a really weird amount (like negative). */100if (in.offset < 0)101errx(1, "%s: illegal offset", "iseek/skip");102103/*104* Read the data. If a pipe, read until satisfy the number of bytes105* being skipped. No differentiation for reading complete and partial106* blocks for other devices.107*/108for (bcnt = in.dbsz, cnt = in.offset, warned = 0; cnt;) {109if ((nr = read(in.fd, in.db, bcnt)) > 0) {110if (in.flags & ISPIPE) {111if (!(bcnt -= nr)) {112bcnt = in.dbsz;113--cnt;114}115} else116--cnt;117if (need_summary)118summary();119if (need_progress)120progress();121continue;122}123124if (nr == 0) {125if (files_cnt > 1) {126--files_cnt;127continue;128}129errx(1, "skip reached end of input");130}131132/*133* Input error -- either EOF with no more files, or I/O error.134* If noerror not set die. POSIX requires that the warning135* message be followed by an I/O display.136*/137if (ddflags & C_NOERROR) {138if (!warned) {139warn("%s", in.name);140warned = 1;141summary();142}143continue;144}145err(1, "%s", in.name);146}147}148149void150pos_out(void)151{152struct mtop t_op;153off_t cnt;154ssize_t n;155156/*157* If not a tape, try seeking on the file. Seeking on a pipe is158* going to fail, but don't protect the user -- they shouldn't159* have specified the seek operand.160*/161if (out.flags & (ISSEEK | ISPIPE)) {162errno = 0;163if (lseek(out.fd, seek_offset(&out), SEEK_CUR) == -1 &&164errno != 0)165err(1, "%s", out.name);166return;167}168169/* Don't try to read a really weird amount (like negative). */170if (out.offset < 0)171errx(1, "%s: illegal offset", "oseek/seek");172173/* If no read access, try using mtio. */174if (out.flags & NOREAD) {175t_op.mt_op = MTFSR;176t_op.mt_count = out.offset;177178if (ioctl(out.fd, MTIOCTOP, &t_op) == -1)179err(1, "%s", out.name);180return;181}182183/* Read it. */184for (cnt = 0; cnt < out.offset; ++cnt) {185before_io();186n = read(out.fd, out.db, out.dbsz);187after_io();188if (n > 0)189continue;190if (n == -1)191err(1, "%s", out.name);192193/*194* If reach EOF, fill with NUL characters; first, back up over195* the EOF mark. Note, cnt has not yet been incremented, so196* the EOF read does not count as a seek'd block.197*/198t_op.mt_op = MTBSR;199t_op.mt_count = 1;200if (ioctl(out.fd, MTIOCTOP, &t_op) == -1)201err(1, "%s", out.name);202203while (cnt++ < out.offset) {204before_io();205n = write(out.fd, out.db, out.dbsz);206after_io();207if (n == -1)208err(1, "%s", out.name);209if (n != out.dbsz)210errx(1, "%s: write failure", out.name);211}212break;213}214}215216217