| Line | Branch | Exec | Source | 
|---|---|---|---|
| 1 | /* | ||
| 2 | ** EPITECH PROJECT, 2024 | ||
| 3 | ** 42sh | ||
| 4 | ** File description: | ||
| 5 | ** The file containing the 42shrc functions | ||
| 6 | */ | ||
| 7 | /** | ||
| 8 | * @file config_file.c | ||
| 9 | * @brief The file containing the 42shrc functions | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include "../include/myshell.h" | ||
| 13 | |||
| 14 | /** | ||
| 15 | * @brief Create the 42shrc file | ||
| 16 | * @param mysh The shell structure | ||
| 17 | * @param path The path of the file | ||
| 18 | * @return <b>void</b> | ||
| 19 | */ | ||
| 20 | 1 | static void create_42shrc(mysh_t *mysh, char *path) | |
| 21 | { | ||
| 22 | 1 | char *var = my_malloc_strdup("MYSH"); | |
| 23 | 1 | char *str = "#42sh configuration file\n\n#42sh created by:\n#\t" | |
| 24 | "Arthur WARIN\n#\tChristophe VANDEVOIR\n#\tGianni TUERO\n#\t" | ||
| 25 | "Nicolas TORO\n#\tRaphael ROSSIGNOL\n\n#ALIAS\n\n#PATH\n\n#COMMAND\n"; | ||
| 26 | |||
| 27 | 1 | mysh->config_file = open(path, O_CREAT | O_RDWR, 0664); | |
| 28 | 1 | write(mysh->config_file, str, my_strlen(str)); | |
| 29 | 1 | set_new_env_var(mysh, var, path); | |
| 30 | 1 | } | |
| 31 | |||
| 32 | /** | ||
| 33 | * @brief Check if the 42shrc file exists in the home directory | ||
| 34 | * and create it if it doesn't | ||
| 35 | * @param mysh The shell structure | ||
| 36 | * @return <b>void</b> | ||
| 37 | */ | ||
| 38 | 371 | void check_42shrc(mysh_t *mysh) | |
| 39 | { | ||
| 40 | 371 | char *home = get_env_var(mysh->env, "HOME"); | |
| 41 | char *path; | ||
| 42 | struct stat file_infos; | ||
| 43 | |||
| 44 | 1/2✓ Branch 0 taken 371 times. ✗ Branch 1 not taken. | 371 | if (home == NULL) | 
| 45 | 371 | path = my_malloc_strdup("./.42shrc"); | |
| 46 | else { | ||
| 47 | ✗ | path = CALLOC(my_strlen(home) + 11, sizeof(char)); | |
| 48 | ✗ | my_strcat(path, home); | |
| 49 | ✗ | my_strcat(path, "/.42shrc"); | |
| 50 | } | ||
| 51 | 371 | mysh->config_file = open(path, O_RDWR); | |
| 52 | 3/4✓ Branch 0 taken 370 times. ✓ Branch 1 taken 1 times. ✗ Branch 3 not taken. ✓ Branch 4 taken 370 times. | 371 | if (mysh->config_file == -1 || stat(path, &file_infos) == -1) | 
| 53 | 1 | create_42shrc(mysh, path); | |
| 54 | else | ||
| 55 | 370 | execute_bash_file(mysh, mysh->config_file, file_infos.st_size); | |
| 56 | 371 | } | |
| 57 |