| Line | Branch | Exec | Source | 
|---|---|---|---|
| 1 | /* | ||
| 2 | ** EPITECH PROJECT, 2024 | ||
| 3 | ** 42sh | ||
| 4 | ** File description: | ||
| 5 | ** The file containing the cd builtin | ||
| 6 | */ | ||
| 7 | /** | ||
| 8 | * @file cd.c | ||
| 9 | * @brief The file containing the cd builtin | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include "../../include/myshell.h" | ||
| 13 | |||
| 14 | /** | ||
| 15 | * @brief Check if the cd command is a cd ~ and execute it | ||
| 16 | * @param mysh The shell structure | ||
| 17 | * @return <b>int</b> <u>0</u> if the command succeed, | ||
| 18 | * <u>1</u> if the command failed and <u>-1</u> if the command is not a cd ~ | ||
| 19 | */ | ||
| 20 | 34 | int cd_home(mysh_t *mysh) | |
| 21 | { | ||
| 22 | char *path; | ||
| 23 | |||
| 24 | 3/4✓ Branch 0 taken 31 times. ✓ Branch 1 taken 3 times. ✗ Branch 2 not taken. ✓ Branch 3 taken 31 times. | 65 | if (mysh->args[1] == NULL || | 
| 25 | 0/2✗ Branch 1 not taken. ✗ Branch 2 not taken. | 31 | (my_strcmp(mysh->args[1], "~") == 0 && mysh->args[2] != NULL)) { | 
| 26 | 3 | path = get_env_var(mysh->env, "HOME"); | |
| 27 | 2/2✓ Branch 0 taken 2 times. ✓ Branch 1 taken 1 times. | 3 | if (path == NULL) { | 
| 28 | 2 | my_fprintf(2, "cd: No home directory.\n"); | |
| 29 | 2 | return 1; | |
| 30 | } | ||
| 31 | 1/2✗ Branch 0 not taken. ✓ Branch 1 taken 1 times. | 1 | if (mysh->old_pwd != NULL) | 
| 32 | ✗ | FREE(mysh->old_pwd); | |
| 33 | 1 | mysh->old_pwd = getcwd(NULL, 0); | |
| 34 | 1/2✗ Branch 1 not taken. ✓ Branch 2 taken 1 times. | 1 | if (chdir(path) == -1) { | 
| 35 | ✗ | my_fprintf(2, "%s: %s.\n", path, strerror(errno)); | |
| 36 | ✗ | return 1; | |
| 37 | } | ||
| 38 | 1 | return 0; | |
| 39 | } | ||
| 40 | 31 | return -1; | |
| 41 | } | ||
| 42 | |||
| 43 | /** | ||
| 44 | * @brief Check if the cd command is a cd - and execute it | ||
| 45 | * @param mysh The shell structure | ||
| 46 | * @return <b>int</b> <u>0</u> if the command succeed, | ||
| 47 | * <u>1</u> if the command failed and <u>-1</u> if the command is not a cd - | ||
| 48 | */ | ||
| 49 | 34 | int cd_back(mysh_t *mysh) | |
| 50 | { | ||
| 51 | 2/2✓ Branch 0 taken 31 times. ✓ Branch 1 taken 3 times. | 34 | if (mysh->args[1] != NULL && | 
| 52 | 3/4✓ Branch 0 taken 4 times. ✓ Branch 1 taken 27 times. ✓ Branch 2 taken 4 times. ✗ Branch 3 not taken. | 31 | mysh->args[1][0] == '-' && mysh->args[1][1] == '\0') { | 
| 53 | 2/2✓ Branch 0 taken 1 times. ✓ Branch 1 taken 3 times. | 4 | if (mysh->old_pwd == NULL) { | 
| 54 | 1 | my_fprintf(2, ": No such file or directory.\n"); | |
| 55 | 1 | return 1; | |
| 56 | } | ||
| 57 | 1/2✗ Branch 1 not taken. ✓ Branch 2 taken 3 times. | 3 | if (chdir(mysh->old_pwd) == -1) { | 
| 58 | ✗ | my_fprintf(2, "%s: %s.\n", mysh->old_pwd, strerror(errno)); | |
| 59 | ✗ | return 1; | |
| 60 | } | ||
| 61 | 1/2✓ Branch 0 taken 3 times. ✗ Branch 1 not taken. | 3 | if (mysh->old_pwd != NULL) | 
| 62 | 3 | FREE(mysh->old_pwd); | |
| 63 | 3 | mysh->old_pwd = getcwd(NULL, 0); | |
| 64 | 3 | replace_env_var(mysh->env, "OLDPWD", mysh->old_pwd); | |
| 65 | 3 | return 0; | |
| 66 | } | ||
| 67 | 30 | return -1; | |
| 68 | } | ||
| 69 | |||
| 70 | /** | ||
| 71 | * @brief The cd builtin | ||
| 72 | * @param mysh The shell structure | ||
| 73 | * @return <b>int</b> <u>0</u> if the command succeed, <u>1</u> otherwise | ||
| 74 | */ | ||
| 75 | 34 | int exec_cd(mysh_t *mysh) | |
| 76 | { | ||
| 77 | 34 | int home = cd_home(mysh); | |
| 78 | 34 | int back = cd_back(mysh); | |
| 79 | |||
| 80 | 2/2✓ Branch 0 taken 3 times. ✓ Branch 1 taken 31 times. | 34 | if (home != -1) | 
| 81 | 3 | return home; | |
| 82 | 2/2✓ Branch 0 taken 3 times. ✓ Branch 1 taken 28 times. | 31 | if (mysh->args[2] != NULL) { | 
| 83 | 3 | my_fprintf(2, "cd: Too many arguments.\n"); | |
| 84 | 3 | return 1; | |
| 85 | } | ||
| 86 | 2/2✓ Branch 0 taken 4 times. ✓ Branch 1 taken 24 times. | 28 | if (back != -1) | 
| 87 | 4 | return back; | |
| 88 | 2/2✓ Branch 0 taken 2 times. ✓ Branch 1 taken 22 times. | 24 | if (mysh->old_pwd != NULL) | 
| 89 | 2 | FREE(mysh->old_pwd); | |
| 90 | 24 | mysh->old_pwd = getcwd(NULL, 0); | |
| 91 | 2/2✓ Branch 1 taken 9 times. ✓ Branch 2 taken 15 times. | 24 | if (chdir(mysh->args[1]) == -1) { | 
| 92 | 9 | my_fprintf(2, "%s: %s.\n", mysh->args[1], strerror(errno)); | |
| 93 | 9 | return 1; | |
| 94 | } | ||
| 95 | 15 | return 0; | |
| 96 | } | ||
| 97 |