Changeset View
Changeset View
Standalone View
Standalone View
bin/dd/dd.c
Show First 20 Lines • Show All 130 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
static void | static void | ||||
setup(void) | setup(void) | ||||
{ | { | ||||
u_int cnt; | u_int cnt; | ||||
cap_rights_t rights; | cap_rights_t rights; | ||||
unsigned long cmds[] = { FIODTYPE, MTIOCTOP }; | unsigned long cmds[] = { FIODTYPE, MTIOCTOP }; | ||||
int oflags = 0; | |||||
if (ddflags & C_DIRECT) | |||||
oflags |= O_DIRECT; | |||||
if (in.name == NULL) { | if (in.name == NULL) { | ||||
in.name = "stdin"; | in.name = "stdin"; | ||||
in.fd = STDIN_FILENO; | in.fd = STDIN_FILENO; | ||||
} else { | } else { | ||||
in.fd = open(in.name, O_RDONLY, 0); | in.fd = open(in.name, O_RDONLY | oflags, 0); | ||||
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 (cap_rights_limit(in.fd, &rights) == -1 && errno != ENOSYS) | if (cap_rights_limit(in.fd, &rights) == -1 && errno != ENOSYS) | ||||
err(1, "unable to limit capability rights"); | err(1, "unable to limit capability rights"); | ||||
if (files_cnt > 1 && !(in.flags & ISTAPE)) | if (files_cnt > 1 && !(in.flags & ISTAPE)) | ||||
errx(1, "files is not supported for non-tape devices"); | errx(1, "files is not supported for non-tape devices"); | ||||
cap_rights_set(&rights, CAP_FTRUNCATE, CAP_IOCTL, CAP_WRITE); | cap_rights_set(&rights, CAP_FTRUNCATE, CAP_IOCTL, CAP_WRITE); | ||||
if (out.name == NULL) { | if (out.name == NULL) { | ||||
/* No way to check for read access here. */ | /* No way to check for read access here. */ | ||||
out.fd = STDOUT_FILENO; | out.fd = STDOUT_FILENO; | ||||
out.name = "stdout"; | out.name = "stdout"; | ||||
} else { | } else { | ||||
#define OFLAGS \ | oflags |= O_CREAT | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC); | ||||
(O_CREAT | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC)) | out.fd = open(out.name, O_RDWR | oflags, DEFFILEMODE); | ||||
out.fd = open(out.name, O_RDWR | OFLAGS, DEFFILEMODE); | |||||
/* | /* | ||||
* 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) { | ||||
out.fd = open(out.name, O_WRONLY | OFLAGS, DEFFILEMODE); | out.fd = open(out.name, O_WRONLY | oflags, DEFFILEMODE); | ||||
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 428 Lines • Show Last 20 Lines |