| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | ** EPITECH PROJECT, 2024 | ||
| 3 | ** 42sh | ||
| 4 | ** File description: | ||
| 5 | ** The file containing the left redirections functions | ||
| 6 | */ | ||
| 7 | /** | ||
| 8 | * @file left_redirections.c | ||
| 9 | * @brief The file containing the left redirections functions | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include "../../include/myshell.h" | ||
| 13 | |||
| 14 | /** | ||
| 15 | * @brief Error handling for the left double redirection | ||
| 16 | * @param mysh The shell structure | ||
| 17 | * @param input The input command | ||
| 18 | * @return <b>int</b> <u>1</u> if an error occurred, <u>0</u> otherwise | ||
| 19 | */ | ||
| 20 | 6 | static int error_handling(mysh_t *mysh, input_command_t *input) | |
| 21 | { | ||
| 22 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (input->left[0] == '\0') { |
| 23 | ✗ | my_fprintf(2, "Missing name for redirect.\n"); | |
| 24 | ✗ | mysh->exit_status = 1; | |
| 25 | ✗ | return 1; | |
| 26 | } | ||
| 27 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (input->command[0] == '\0') { |
| 28 | ✗ | my_fprintf(2, "Invalid null command.\n"); | |
| 29 | ✗ | mysh->exit_status = 1; | |
| 30 | ✗ | return 1; | |
| 31 | } | ||
| 32 | 6 | return 0; | |
| 33 | } | ||
| 34 | |||
| 35 | /** | ||
| 36 | * @brief Execute the child process of the left double redirection | ||
| 37 | * @param mysh The shell structure | ||
| 38 | * @param input The input command | ||
| 39 | * @param fd The file descriptor | ||
| 40 | * @return <b>void</b> | ||
| 41 | */ | ||
| 42 | 6 | static void child_process(mysh_t *mysh, input_command_t *input, int fd[2]) | |
| 43 | { | ||
| 44 | 6 | close(fd[1]); | |
| 45 | 6 | dup2(fd[0], 0); | |
| 46 | 6 | analyse_parentheses(mysh, input); | |
| 47 | 6 | my_exit(mysh, mysh->exit_status, ""); | |
| 48 | ✗ | } | |
| 49 | |||
| 50 | /** | ||
| 51 | * @brief Read the input of the left double redirection and push it in a node | ||
| 52 | * @param mysh The shell structure | ||
| 53 | * @param input The input command | ||
| 54 | * @param node The node | ||
| 55 | * @return <b>void</b> | ||
| 56 | */ | ||
| 57 | 6 | static void read_input(mysh_t *mysh, input_command_t *input, node_t **node) | |
| 58 | { | ||
| 59 | 6 | int size = 0; | |
| 60 | 6 | char *line = NULL; | |
| 61 | |||
| 62 |
4/4✓ Branch 0 taken 24 times.
✓ Branch 1 taken 2 times.
✓ Branch 3 taken 20 times.
✓ Branch 4 taken 4 times.
|
26 | while (size != EOF && my_strcmp(line, input->left) != 0) { |
| 63 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 6 times.
|
20 | if (line != NULL) |
| 64 | 14 | my_push_back(node, my_strdup(line), STRING); | |
| 65 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
|
20 | IS_ATTY_PRINT("? "); |
| 66 | 20 | FREE(line); | |
| 67 | 20 | size = my_getline(&line, stdin); | |
| 68 | 20 | set_command_in_history(mysh, line); | |
| 69 | } | ||
| 70 | 6 | FREE(line); | |
| 71 | 6 | } | |
| 72 | |||
| 73 | /** | ||
| 74 | * @brief Execute the left double redirection | ||
| 75 | * @param mysh The shell structure | ||
| 76 | * @param input The input command | ||
| 77 | * @return <b>void</b> | ||
| 78 | */ | ||
| 79 | 6 | void exec_left_double_redirection(mysh_t *mysh, input_command_t *input) | |
| 80 | { | ||
| 81 | 6 | node_t *node = NULL; | |
| 82 | 6 | int fd[2] = {0}; | |
| 83 | 6 | pid_t pid = 0; | |
| 84 | |||
| 85 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
|
6 | if (error_handling(mysh, input)) |
| 86 | ✗ | return; | |
| 87 | 6 | my_add_chr(input->left, '\n'); | |
| 88 | 6 | pipe(fd); | |
| 89 | 6 | pid = fork(); | |
| 90 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | if (pid == 0) { |
| 91 | 6 | child_process(mysh, input, fd); | |
| 92 | } else { | ||
| 93 | 6 | close(fd[0]); | |
| 94 | 6 | read_input(mysh, input, &node); | |
| 95 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 6 times.
|
20 | for (node_t *tmp = node; tmp != NULL; tmp = tmp->next) |
| 96 | 14 | my_fprintf(fd[1], "%s", tmp->data); | |
| 97 | 6 | my_delete_list(&node); | |
| 98 | 6 | close(fd[1]); | |
| 99 | 6 | waitpid(pid, &mysh->exit_status, 0); | |
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | /** | ||
| 104 | * @brief Execute the left simple redirection | ||
| 105 | * @param mysh The shell structure | ||
| 106 | * @param input The input command | ||
| 107 | * @return <b>void</b> | ||
| 108 | */ | ||
| 109 | 20 | void exec_left_simple_redirection(mysh_t *mysh, input_command_t *input) | |
| 110 | { | ||
| 111 | int fd; | ||
| 112 | |||
| 113 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
|
20 | if (input->left[0] == '\0') { |
| 114 | ✗ | my_fprintf(2, "Missing name for redirect.\n"); | |
| 115 | ✗ | mysh->exit_status = 1; | |
| 116 | ✗ | return; | |
| 117 | } | ||
| 118 | 20 | fd = open(input->left, O_RDONLY); | |
| 119 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 16 times.
|
20 | if (fd == -1) { |
| 120 | 4 | mysh->exit_status = 1; | |
| 121 | 4 | my_fprintf(2, "%s: %s.\n", input->left, strerror(errno)); | |
| 122 | 4 | return; | |
| 123 | } | ||
| 124 | 16 | mysh->saved_stdin = dup(0); | |
| 125 | 16 | dup2(fd, 0); | |
| 126 | 16 | close(fd); | |
| 127 | 16 | analyse_parentheses(mysh, input); | |
| 128 | 16 | dup2(mysh->saved_stdin, 0); | |
| 129 | 16 | close(mysh->saved_stdin); | |
| 130 | } | ||
| 131 |