| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | ** EPITECH PROJECT, 2024 | ||
| 3 | ** 42sh | ||
| 4 | ** File description: | ||
| 5 | ** The file containing the condition functions | ||
| 6 | */ | ||
| 7 | /** | ||
| 8 | * @file condition.c | ||
| 9 | * @brief The file containing the condition functions | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include "../../include/myshell.h" | ||
| 13 | |||
| 14 | /** | ||
| 15 | * @brief The child process who execute the command to get the result | ||
| 16 | * @param fd The pipe file descriptor | ||
| 17 | * @return <b>void</b> | ||
| 18 | */ | ||
| 19 | 16 | static void child_process(int *fd) | |
| 20 | { | ||
| 21 | static char *command[] = {"bc", "-l", NULL}; | ||
| 22 | |||
| 23 | 16 | close(fd[1]); | |
| 24 | 16 | dup2(fd[0], 0); | |
| 25 | 16 | get_mysh()->args = command; | |
| 26 | 16 | exec_command(get_mysh()); | |
| 27 | 16 | my_exit(get_mysh(), get_mysh()->exit_status, NULL); | |
| 28 | ✗ | } | |
| 29 | |||
| 30 | /** | ||
| 31 | * @brief Read the result of the operation | ||
| 32 | * @param new_fd The pipe file descriptor to read | ||
| 33 | * @param pid The pid of the child process | ||
| 34 | * @param str The string to calculate | ||
| 35 | * @param saved_stdout The saved stdout | ||
| 36 | * @return <b>char *</b> The result of the operation | ||
| 37 | */ | ||
| 38 | 16 | static char *read_result(int *new_fd, pid_t pid, char *str, int saved_stdout) | |
| 39 | { | ||
| 40 | 16 | char *result = calloc(1024, sizeof(char)); | |
| 41 | 16 | int nb_bytes = 0; | |
| 42 | 16 | int status = 0; | |
| 43 | |||
| 44 | 16 | waitpid(pid, &status, 0); | |
| 45 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | if (status != 0) |
| 46 | ✗ | return NULL; | |
| 47 | 16 | nb_bytes = read(new_fd[0], result, 1024); | |
| 48 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | if (nb_bytes == -1) |
| 49 | ✗ | my_fprintf(2, "%s\n", strerror(errno)); | |
| 50 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | while (nb_bytes == 1024) { |
| 51 | ✗ | result = realloc(result, sizeof(char) * (1024 + my_strlen(result))); | |
| 52 | ✗ | nb_bytes = read(new_fd[0], result + my_strlen(result), 1024); | |
| 53 | } | ||
| 54 | 16 | result[my_strlen(result) - 1] = '\0'; | |
| 55 | 16 | dup2(saved_stdout, 1); | |
| 56 | 16 | close(saved_stdout); | |
| 57 | 16 | return result; | |
| 58 | } | ||
| 59 | |||
| 60 | /** | ||
| 61 | * @brief Get the result of an operation | ||
| 62 | * @param str The string to calculate | ||
| 63 | * @return <b>char *</b> The result of the string | ||
| 64 | */ | ||
| 65 | 16 | static char *get_result(char *str) | |
| 66 | { | ||
| 67 | 16 | int fd[2] = {0}; | |
| 68 | 16 | int new_fd[2] = {0}; | |
| 69 | 16 | pid_t pid = 0; | |
| 70 | 16 | int saved_stdout = dup(1); | |
| 71 | |||
| 72 | 16 | pipe(new_fd); | |
| 73 | 16 | dup2(new_fd[1], 1); | |
| 74 | 16 | close(new_fd[1]); | |
| 75 | 16 | pipe(fd); | |
| 76 | 16 | pid = fork(); | |
| 77 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16 times.
|
32 | if (pid == 0) |
| 78 | 16 | child_process(fd); | |
| 79 | 16 | close(fd[0]); | |
| 80 | 16 | my_fprintf(fd[1], "%s\n", str); | |
| 81 | 16 | close(fd[1]); | |
| 82 | 16 | return read_result(new_fd, pid, str, saved_stdout); | |
| 83 | } | ||
| 84 | |||
| 85 | /** | ||
| 86 | * @brief Get the condition result from a string | ||
| 87 | * @param str The string to calculate | ||
| 88 | * @return <b>int</b> The result of the condition : 0 if false, 1 if true | ||
| 89 | * and -1 if an error occurred | ||
| 90 | */ | ||
| 91 | 16 | int get_condition(char *str) | |
| 92 | { | ||
| 93 | 16 | char *result = get_result(str); | |
| 94 | 16 | int nb = 0; | |
| 95 | |||
| 96 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | if (result == NULL) { |
| 97 | ✗ | my_fprintf(2, "if: Expression Syntax.\n"); | |
| 98 | ✗ | return -1; | |
| 99 | } | ||
| 100 | 16 | nb = my_super_number(result, (NB){0, 0, 0, 1}); | |
| 101 | 16 | FREE(result); | |
| 102 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | if (my_errno) { |
| 103 | ✗ | my_fprintf(2, "if: Expression Syntax.\n"); | |
| 104 | ✗ | return -1; | |
| 105 | } | ||
| 106 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 10 times.
|
16 | if (nb == 0) |
| 107 | 6 | return 0; | |
| 108 | 10 | return 1; | |
| 109 | } | ||
| 110 |