GCC Code Coverage Report


Directory: ./
File: src/builtins/cd.c
Date: 2024-05-07 14:54:03
Exec Total Coverage
Lines: 31 47 66.0%
Functions: 3 3 100.0%
Branches: 18 34 52.9%

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 43 int cd_home(mysh_t *mysh)
21 {
22 char *path;
23
24
2/4
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 43 times.
86 if (mysh->args[1] == NULL ||
25
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
43 (my_strcmp(mysh->args[1], "~") == 0 && mysh->args[2] != NULL)) {
26 path = get_env_var(mysh->env, "HOME");
27 if (path == NULL) {
28 my_fprintf(2, "cd: No home directory.\n");
29 return 1;
30 }
31 if (mysh->old_pwd != NULL)
32 FREE(mysh->old_pwd);
33 mysh->old_pwd = getcwd(NULL, 0);
34 if (chdir(path) == -1) {
35 my_fprintf(2, "%s: %s.\n", path, strerror(errno));
36 return 1;
37 }
38 return 0;
39 }
40 43 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 43 int cd_back(mysh_t *mysh)
50 {
51
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 if (mysh->args[1] != NULL &&
52
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 37 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
43 mysh->args[1][0] == '-' && mysh->args[1][1] == '\0') {
53
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (mysh->old_pwd == NULL) {
54 my_fprintf(2, ": No such file or directory.\n");
55 return 1;
56 }
57
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 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 6 times.
✗ Branch 1 not taken.
6 if (mysh->old_pwd != NULL)
62 6 FREE(mysh->old_pwd);
63 6 mysh->old_pwd = getcwd(NULL, 0);
64 6 replace_env_var(mysh->env, "OLDPWD", mysh->old_pwd);
65 6 return 0;
66 }
67 37 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 43 int exec_cd(mysh_t *mysh)
76 {
77 43 int home = cd_home(mysh);
78 43 int back = cd_back(mysh);
79
80
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 if (home != -1)
81 return home;
82
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 42 times.
43 if (mysh->args[2] != NULL) {
83 1 my_fprintf(2, "cd: Too many arguments.\n");
84 1 return 1;
85 }
86
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 36 times.
42 if (back != -1)
87 6 return back;
88
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 23 times.
36 if (mysh->old_pwd != NULL)
89 13 FREE(mysh->old_pwd);
90 36 mysh->old_pwd = getcwd(NULL, 0);
91
2/2
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 25 times.
36 if (chdir(mysh->args[1]) == -1) {
92 11 my_fprintf(2, "%s: %s.\n", mysh->args[1], strerror(errno));
93 11 return 1;
94 }
95 25 return 0;
96 }
97