1/* 2 * Copyright (c) 1985, 1987, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 28 unchanged lines hidden (view full) --- 37 The Regents of the University of California. All rights reserved.\n"; 38#endif /* not lint */ 39 40#ifndef lint 41#if 0 42static char sccsid[] = "@(#)tcopy.c 8.2 (Berkeley) 4/17/94"; 43#endif 44static const char rcsid[] = |
45 "$Id: tcopy.c,v 1.4 1997/08/14 06:41:00 charnier Exp $"; |
46#endif /* not lint */ 47 48#include <sys/types.h> 49#include <sys/stat.h> 50#include <sys/ioctl.h> 51#include <sys/mtio.h> 52 53#include <err.h> --- 6 unchanged lines hidden (view full) --- 60#include <unistd.h> 61 62#include "pathnames.h" 63 64#define MAXREC (64 * 1024) 65#define NOCOUNT (-2) 66 67int filen, guesslen, maxblk = MAXREC; |
68u_int64_t lastrec, record, size, tsize; |
69FILE *msg = stdout; 70 71void *getspace __P((int)); 72void intr __P((int)); 73static void usage __P((void)); 74void verify __P((int, int, char *)); 75void writeop __P((int, int)); |
76void rewind_tape(int); |
77 78int 79main(argc, argv) 80 int argc; 81 char *argv[]; 82{ 83 register int lastnread, nread, nw, inp, outp; 84 enum {READ, VERIFY, COPY, COPYVERIFY} op = READ; --- 67 unchanged lines hidden (view full) --- 152 needeof = 0; 153 for (lastnread = NOCOUNT;;) { 154 if ((nread = read(inp, buff, maxblk)) == -1) { 155 while (errno == EINVAL && (maxblk -= 1024)) { 156 nread = read(inp, buff, maxblk); 157 if (nread >= 0) 158 goto r1; 159 } |
160 err(1, "read error, file %d, record %qu", filen, record); |
161 } else if (nread != lastnread) { 162 if (lastnread != 0 && lastnread != NOCOUNT) { 163 if (lastrec == 0 && nread == 0) |
164 fprintf(msg, "%qu records\n", record); |
165 else if (record - lastrec > 1) |
166 fprintf(msg, "records %qu to %qu\n", |
167 lastrec, record); 168 else |
169 fprintf(msg, "record %qu\n", lastrec); |
170 } 171 if (nread != 0) 172 fprintf(msg, "file %d: block size %d: ", 173 filen, nread); 174 (void) fflush(stdout); 175 lastrec = record; 176 } 177r1: guesslen = 0; 178 if (nread > 0) { 179 if (op == COPY || op == COPYVERIFY) { 180 if (needeof) { 181 writeop(outp, MTWEOF); 182 needeof = 0; 183 } 184 nw = write(outp, buff, nread); 185 if (nw != nread) { 186 if (nw == -1) { |
187 warn("write error, file %d, record %qu", filen, record); |
188 } else { |
189 warnx("write error, file %d, record %qu", filen, record); |
190 warnx("write (%d) != read (%d)", nw, nread); 191 } 192 errx(5, "copy aborted"); 193 } 194 } 195 size += nread; 196 record++; 197 } else { 198 if (lastnread <= 0 && lastnread != NOCOUNT) { 199 fprintf(msg, "eot\n"); 200 break; 201 } 202 fprintf(msg, |
203 "file %d: eof after %qu records: %qu bytes\n", |
204 filen, record, size); 205 needeof = 1; 206 filen++; 207 tsize += size; 208 size = record = lastrec = 0; 209 lastnread = 0; 210 } 211 lastnread = nread; 212 } |
213 fprintf(msg, "total length: %qu bytes\n", tsize); |
214 (void)signal(SIGINT, oldsig); 215 if (op == COPY || op == COPYVERIFY) { 216 writeop(outp, MTWEOF); 217 writeop(outp, MTWEOF); 218 if (op == COPYVERIFY) { |
219 rewind_tape(outp); 220 rewind_tape(inp); |
221 verify(inp, outp, buff); 222 } 223 } 224 exit(0); 225} 226 227void 228verify(inp, outp, outb) --- 50 unchanged lines hidden (view full) --- 279} 280 281void 282intr(signo) 283 int signo; 284{ 285 if (record) 286 if (record - lastrec > 1) |
287 fprintf(msg, "records %qu to %qu\n", lastrec, record); |
288 else |
289 fprintf(msg, "record %qu\n", lastrec); 290 fprintf(msg, "interrupt at file %d: record %qu\n", filen, record); |
291 fprintf(msg, "total length: %ld bytes\n", tsize + size); 292 exit(1); 293} 294 295void * 296getspace(blk) 297 int blk; 298{ --- 17 unchanged lines hidden (view full) --- 316} 317 318static void 319usage() 320{ 321 fprintf(stderr, "usage: tcopy [-cvx] [-s maxblk] [src [dest]]\n"); 322 exit(1); 323} |
324 325void 326rewind_tape(int fd) 327{ 328 struct stat sp; 329 330 if(fstat(fd, &sp)) 331 errx(12, "fstat in rewind"); 332 333 /* 334 * don't want to do tape ioctl on regular files: 335 */ 336 if( S_ISREG(sp.st_mode) ) { 337 if( lseek(fd, 0, SEEK_SET) == -1 ) 338 errx(13, "lseek"); 339 } else 340 /* assume its a tape */ 341 writeop(fd, MTREW); 342} |