summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--exec.c49
1 files changed, 44 insertions, 5 deletions
diff --git a/exec.c b/exec.c
index e58a89b..fccda7a 100644
--- a/exec.c
+++ b/exec.c
@@ -17,7 +17,7 @@ void execute_command(Command cmd, int* pgid) {
pid_t pid = fork();
if (pid == 0) {
- if (*pgid == -1) {
+ if (*pgid == -1) {
setpgid(0, 0);
} else {
setpgid(0, *pgid);
@@ -28,7 +28,7 @@ void execute_command(Command cmd, int* pgid) {
for (int j = 0; j < cmd.redirs.count; j++) {
FdRedirect fd_rd = *get_from_FdRedirectVec(&cmd.redirs, j);
- dup2(fd_rd.old_fd, fd_rd.new_fd);
+ dup2(fd_rd.new_fd, fd_rd.old_fd);
}
for (int i = 0; i < cmd.child_close_fds.count; i++) {
@@ -62,10 +62,49 @@ Command make_command(ExpandedPipelineElement elem) {
};
StrVec arg_strs = make_StrVec(256);
for (int i = 0; i < elem.cmd_slots.count; i++) {
- Slot slot = *get_from_SlotVec(&elem.cmd_slots, i);
- switch (slot.kind) {
+ Slot curr_slot = *get_from_SlotVec(&elem.cmd_slots, i);
+ Slot next_slot;
+ int fd;
+ FdRedirect redir;
+ switch (curr_slot.kind) {
case SLOT_STR:
- push_to_StrVec(&arg_strs, slot.as.str);
+ push_to_StrVec(&arg_strs, curr_slot.as.str);
+ break;
+ case SLOT_SPEC:
+ switch (curr_slot.as.spec) {
+ case READ_REDIR:
+ i++;
+ next_slot = *get_from_SlotVec(&elem.cmd_slots, i);
+ if (next_slot.kind != SLOT_STR) exit(6);
+ fd = open(next_slot.as.str, O_RDONLY, 0644);
+ redir = (FdRedirect){.old_fd = STDIN_FILENO, .new_fd = fd};
+ push_to_FdRedirectVec(&cmd.redirs, redir);
+ push_to_IntVec(&cmd.parent_close_fds, fd);
+ push_to_IntVec(&cmd.child_close_fds, fd);
+ break;
+ case WRITE_REDIR_TRUNC:
+ i++;
+ next_slot = *get_from_SlotVec(&elem.cmd_slots, i);
+ if (next_slot.kind != SLOT_STR) exit(6);
+ fd = open(next_slot.as.str, O_WRONLY | O_CREAT | O_TRUNC , 0644);
+ redir = (FdRedirect){.old_fd = STDOUT_FILENO, .new_fd = fd};
+ push_to_FdRedirectVec(&cmd.redirs, redir);
+ push_to_IntVec(&cmd.parent_close_fds, fd);
+ push_to_IntVec(&cmd.child_close_fds, fd);
+ break;
+ case WRITE_REDIR_APPEND:
+ i++;
+ next_slot = *get_from_SlotVec(&elem.cmd_slots, i);
+ if (next_slot.kind != SLOT_STR) exit(6);
+ fd = open(next_slot.as.str, O_WRONLY | O_CREAT | O_APPEND, 0644);
+ redir = (FdRedirect){.old_fd = STDOUT_FILENO, .new_fd = fd};
+ push_to_FdRedirectVec(&cmd.redirs, redir);
+ push_to_IntVec(&cmd.parent_close_fds, fd);
+ push_to_IntVec(&cmd.child_close_fds, fd);
+ break;
+ default:
+ exit(7);
+ }
break;
default:
exit(67);