#include #include #include #include #include #include extern const char *__progname; static const char *fromfile; static off_t fromoff; static const char *tofile; static off_t tooff; static off_t len; static int reverse; static int fromfd; static int frompipe; static int tofd; static int topipe; static int off_t_val(const char *s, off_t *otp, const char *tag) { char *e; u_quad_t v; v = strtoq(s,&e,0); if (*e || (e == s)) { fprintf(stderr,"%s: invalid %s value `%s'\n",__progname,tag,s); return(1); } *otp = v; return(0); } static void handleargs(int ac, char **av) { int i; int j; int errs; errs = 0; reverse = 0; j = 1; for (i=1;i 0) { n = sizeof(buf); if (n > left) n = left; if (reverse) { fromoff -= n; tooff -= n; } if (frompipe) { r = read(fromfd,&buf[0],n); } else { r = pread(fromfd,&buf[0],n,fromoff); } if (r < 0) { fprintf(stderr,"%s: read from %s at %llu (after copying %llu): %s\n", __progname, fromfile, (unsigned long long int)fromoff, (unsigned long long int)(len-left), strerror(errno) ); exit(1); } if (r == 0) { fprintf(stderr,"%s: EOF on %s at %llu (after copying %llu)\n", __progname, fromfile, (unsigned long long int)fromoff, (unsigned long long int)(len-left) ); exit(1); } if (reverse && (r != n)) { fprintf(stderr,"%s: read from %s at %lld got %d (wanted %d)\n",__progname,fromfile,(long long int)fromoff,r,n); exit(1); } wo = 0; while (wo < r) { if (topipe) { w = write(tofd,&buf[wo],r-wo); } else { w = pwrite(tofd,&buf[wo],r-wo,tooff+wo); } if (w < 0) { fprintf(stderr,"%s: write to %s at %llu (after copying %llu): %s\n", __progname, tofile, (unsigned long long int)tooff, (unsigned long long int)(wo+len-left), strerror(errno) ); exit(1); } if (w != r-wo) { fprintf(stderr,"%s: short write to %s at %llu (after copying %llu): wanted %u, wrote %u (retrying remainder)\n", __progname, tofile, (unsigned long long int)tooff, (unsigned long long int)(wo+len-left), (unsigned int)(r-wo), (unsigned int)w ); } wo += w; } left -= r; if (! reverse) { fromoff += r; tooff += r; } } } int main(int, char **); int main(int ac, char **av) { handleargs(ac,av); openfiles(); docopy(); exit(0); }