redirect #17

Merged
cromer merged 22 commits from redirect into master 2021-07-27 23:36:31 -04:00
Showing only changes of commit 5f9b4bf81d - Show all commits

View File

@ -22,32 +22,32 @@
void open_redirect(Redirect *redirect) { void open_redirect(Redirect *redirect) {
if (redirect->filename != NULL) { if (redirect->filename != NULL) {
redirect->fd_copy = dup(redirect->fd); redirect->fd_copy = dup(redirect->fd);
if (redirect->fd_copy == -1) { if (redirect->fd_copy == -1) {
perror("dup"); perror("dup");
}
else {
if (redirect->append) {
redirect->fd_new = open(redirect->filename, O_WRONLY | O_CREAT | O_APPEND, 0664);
} }
else { else {
if (redirect->append) { redirect->fd_new = open(redirect->filename, O_WRONLY | O_CREAT | O_TRUNC, 0664);
redirect->fd_new = open(redirect->filename, O_WRONLY | O_CREAT | O_APPEND, 0664); }
if (redirect->fd_new == -1) {
fprintf(stderr, "open: Could not open file %s: \"%s\"\n", redirect->filename, strerror(errno));
free(redirect->filename);
redirect->filename = NULL;
}
else {
if (close(redirect->fd) == -1) {
perror("close");
} }
else { if (dup(redirect->fd_new) == -1) {
redirect->fd_new = open(redirect->filename, O_WRONLY | O_CREAT | O_TRUNC, 0664); perror("dup");
}
if (redirect->fd_new == -1) {
fprintf(stderr, "open: Could not open file %s: \"%s\"\n", redirect->filename, strerror(errno));
free(redirect->filename);
redirect->filename = NULL;
}
else {
if (close(redirect->fd) == -1) {
perror("close");
}
if (dup(redirect->fd_new) == -1) {
perror("dup");
}
} }
} }
} }
}
} }
void close_redirect(Redirect *redirect) { void close_redirect(Redirect *redirect) {