Changeset View
Changeset View
Standalone View
Standalone View
bin/dd/dd.c
Show First 20 Lines • Show All 93 Lines • ▼ Show 20 Lines | |||||
volatile sig_atomic_t need_progress; | volatile sig_atomic_t need_progress; | ||||
volatile sig_atomic_t kill_signal; | volatile sig_atomic_t kill_signal; | ||||
int | int | ||||
main(int argc __unused, char *argv[]) | main(int argc __unused, char *argv[]) | ||||
{ | { | ||||
struct itimerval itv = { { 1, 0 }, { 1, 0 } }; /* SIGALARM every second, if needed */ | struct itimerval itv = { { 1, 0 }, { 1, 0 } }; /* SIGALARM every second, if needed */ | ||||
(void)siginterrupt(SIGINT, 1); | prepare_io(); | ||||
(void)signal(SIGINT, terminate); | |||||
(void)setlocale(LC_CTYPE, ""); | (void)setlocale(LC_CTYPE, ""); | ||||
jcl(argv); | jcl(argv); | ||||
setup(); | setup(); | ||||
caph_cache_catpages(); | caph_cache_catpages(); | ||||
if (caph_enter() < 0) | if (caph_enter() < 0) | ||||
err(1, "unable to enter capability mode"); | err(1, "unable to enter capability mode"); | ||||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | setup(void) | ||||
if (in.name == NULL) { | if (in.name == NULL) { | ||||
in.name = "stdin"; | in.name = "stdin"; | ||||
in.fd = STDIN_FILENO; | in.fd = STDIN_FILENO; | ||||
} else { | } else { | ||||
iflags = 0; | iflags = 0; | ||||
if (ddflags & C_IDIRECT) | if (ddflags & C_IDIRECT) | ||||
iflags |= O_DIRECT; | iflags |= O_DIRECT; | ||||
check_terminate(); | before_io(); | ||||
in.fd = open(in.name, O_RDONLY | iflags, 0); | in.fd = open(in.name, O_RDONLY | iflags, 0); | ||||
check_terminate(); | after_io(); | ||||
if (in.fd == -1) | if (in.fd == -1) | ||||
err(1, "%s", in.name); | err(1, "%s", in.name); | ||||
} | } | ||||
getfdtype(&in); | getfdtype(&in); | ||||
cap_rights_init(&rights, CAP_READ, CAP_SEEK); | cap_rights_init(&rights, CAP_READ, CAP_SEEK); | ||||
if (caph_rights_limit(in.fd, &rights) == -1) | if (caph_rights_limit(in.fd, &rights) == -1) | ||||
Show All 20 Lines | setup(void) | ||||
} else { | } else { | ||||
oflags = O_CREAT; | oflags = O_CREAT; | ||||
if (!(ddflags & (C_SEEK | C_NOTRUNC))) | if (!(ddflags & (C_SEEK | C_NOTRUNC))) | ||||
oflags |= O_TRUNC; | oflags |= O_TRUNC; | ||||
if (ddflags & C_OFSYNC) | if (ddflags & C_OFSYNC) | ||||
oflags |= O_FSYNC; | oflags |= O_FSYNC; | ||||
if (ddflags & C_ODIRECT) | if (ddflags & C_ODIRECT) | ||||
oflags |= O_DIRECT; | oflags |= O_DIRECT; | ||||
check_terminate(); | before_io(); | ||||
out.fd = open(out.name, O_RDWR | oflags, DEFFILEMODE); | out.fd = open(out.name, O_RDWR | oflags, DEFFILEMODE); | ||||
check_terminate(); | after_io(); | ||||
/* | /* | ||||
* May not have read access, so try again with write only. | * May not have read access, so try again with write only. | ||||
* Without read we may have a problem if output also does | * Without read we may have a problem if output also does | ||||
* not support seeks. | * not support seeks. | ||||
*/ | */ | ||||
if (out.fd == -1) { | if (out.fd == -1) { | ||||
before_io(); | |||||
out.fd = open(out.name, O_WRONLY | oflags, DEFFILEMODE); | out.fd = open(out.name, O_WRONLY | oflags, DEFFILEMODE); | ||||
check_terminate(); | after_io(); | ||||
out.flags |= NOREAD; | out.flags |= NOREAD; | ||||
cap_rights_clear(&rights, CAP_READ); | cap_rights_clear(&rights, CAP_READ); | ||||
} | } | ||||
if (out.fd == -1) | if (out.fd == -1) | ||||
err(1, "%s", out.name); | err(1, "%s", out.name); | ||||
} | } | ||||
getfdtype(&out); | getfdtype(&out); | ||||
▲ Show 20 Lines • Show All 200 Lines • ▼ Show 20 Lines | if (ddflags & C_SYNC) { | ||||
else if (ddflags & (C_BLOCK | C_UNBLOCK)) | else if (ddflags & (C_BLOCK | C_UNBLOCK)) | ||||
memset(in.dbp, ' ', in.dbsz); | memset(in.dbp, ' ', in.dbsz); | ||||
else | else | ||||
memset(in.dbp, 0, in.dbsz); | memset(in.dbp, 0, in.dbsz); | ||||
} | } | ||||
in.dbrcnt = 0; | in.dbrcnt = 0; | ||||
fill: | fill: | ||||
check_terminate(); | before_io(); | ||||
n = read(in.fd, in.dbp + in.dbrcnt, in.dbsz - in.dbrcnt); | n = read(in.fd, in.dbp + in.dbrcnt, in.dbsz - in.dbrcnt); | ||||
check_terminate(); | after_io(); | ||||
/* EOF */ | /* EOF */ | ||||
if (n == 0 && in.dbrcnt == 0) | if (n == 0 && in.dbrcnt == 0) | ||||
return; | return; | ||||
/* Read error */ | /* Read error */ | ||||
if (n == -1) { | if (n == -1) { | ||||
/* | /* | ||||
▲ Show 20 Lines • Show All 164 Lines • ▼ Show 20 Lines | do { | ||||
*/ | */ | ||||
out.seek_offset = lseek(out.fd, pending, SEEK_CUR); | out.seek_offset = lseek(out.fd, pending, SEEK_CUR); | ||||
if (out.seek_offset == -1) | if (out.seek_offset == -1) | ||||
err(2, "%s: seek error creating sparse file", | err(2, "%s: seek error creating sparse file", | ||||
out.name); | out.name); | ||||
pending = 0; | pending = 0; | ||||
} | } | ||||
if (cnt) { | if (cnt) { | ||||
check_terminate(); | before_io(); | ||||
nw = write(out.fd, outp, cnt); | nw = write(out.fd, outp, cnt); | ||||
check_terminate(); | after_io(); | ||||
out.seek_offset = 0; | out.seek_offset = 0; | ||||
} else { | } else { | ||||
return; | return; | ||||
} | } | ||||
} | } | ||||
if (nw <= 0) { | if (nw <= 0) { | ||||
if (nw == 0) | if (nw == 0) | ||||
Show All 37 Lines |