diff --git a/include/shell.h b/include/shell.h index 4d73686..6659f29 100644 --- a/include/shell.h +++ b/include/shell.h @@ -102,6 +102,7 @@ int execute_with_pipes(cmds_p *command); int execute(cmd_pipe *command, char **envp); int launch(cmd_pipe *command, char **envp); int mexecvpe(char *file, char **argv, char **envp); +int complete_envs(char ***envp); int sh_cd(char **args); int sh_exec(char **args, char **envp); diff --git a/include/utils.h b/include/utils.h index eb164df..3829f86 100644 --- a/include/utils.h +++ b/include/utils.h @@ -19,5 +19,6 @@ int append_to_str_arr(char ***arr, ssize_t *sz, char *str); char *get_curr_dir_name(); int get_num_of_lines(FILE *file); bool str_is_in_arr(char **arr, size_t sz, char *str); +char *get_env_var(char *name); #endif \ No newline at end of file diff --git a/src/complete.c b/src/complete.c index d9b9b56..4e4c1ee 100644 --- a/src/complete.c +++ b/src/complete.c @@ -146,7 +146,6 @@ ssize_t get_complete_options(char ***opts, char *line, char **to_complete) *to_complete = strdup(""); sz = get_path_commands_list(opts); - // sz = get_dir_list(opts, "/usr/bin", 1); append_builtin_list(opts, &sz); } @@ -203,7 +202,10 @@ ssize_t filter_options(char ***comp_list, ssize_t *size, char *filter_string) ssize_t get_path_commands_list(char ***opts) { - char *paths_str = getenv("PATH"); + char *paths_str = get_env_var("PATH"); + if (paths_str == NULL) + return -1; + char **paths = NULL; int path_am = sep_string(paths_str, &paths, ":"); ssize_t sz = 0; @@ -225,5 +227,6 @@ ssize_t get_path_commands_list(char ***opts) } } + free(paths_str); return sz; } \ No newline at end of file diff --git a/src/execute.c b/src/execute.c index 4c990a4..01c2fb9 100644 --- a/src/execute.c +++ b/src/execute.c @@ -28,7 +28,6 @@ int execute_with_pipes(cmds_p *command) for (int i = 0; i < command->pipes_am - 1 && curr != NULL && status == 0; i++) { - if (pipe(tmp_fd) < 0) { perror("pipe"); @@ -124,7 +123,15 @@ int launch(cmd_pipe *command, char **envp) int sh_cd(char **args) { if (args[1] == NULL) - chdir(getenv("HOME")); + { + char *home_env = get_env_var("HOME"); + if (home_env == NULL) + return 1; + + chdir(home_env); + free(home_env); + } + else if (chdir(args[1]) < 0) { perror("cd"); @@ -210,13 +217,20 @@ int mexecvpe(char *file, char **argv, char **envp) if (file == NULL || file[0] == '\0') return -1; - if (envp == NULL) + if (envp == NULL || envp[0] == NULL) execvp(file, argv); + int res = complete_envs(&envp); + + if (res < 0) + return -1; + if (strchr(file, '/') != NULL) execve(file, argv, envp); - char *path = getenv("PATH"); + char *path = get_env_var("PATH"); + if (path == NULL) + return -1; size_t file_len = strlen(file) + 1, path_len = strlen(path) + 1; @@ -236,4 +250,31 @@ int mexecvpe(char *file, char **argv, char **envp) if (*subp++ == '\0') break; } +} + +int complete_envs(char ***envp) +{ + for (int i = 0; environ[i] != NULL; i++) + { + char *name = strtok(strdup(environ[i]), "="); + bool modded = false; + for (int j = 0; (*envp)[j] != NULL; j++) + { + if (strncmp((*envp)[j], name, strlen(name)) == 0) + { + modded = true; + (*envp)[j] = strdup(environ[i]); + } + } + + if (!modded) + { + ssize_t size = get_null_term_arr_size(*envp); + int rs = append_to_str_arr(envp, &size, environ[i]); + if (rs < 0) + return rs; + } + } + + return 0; } \ No newline at end of file diff --git a/src/history.c b/src/history.c index e25f9c9..0638e41 100644 --- a/src/history.c +++ b/src/history.c @@ -199,15 +199,13 @@ void init_history() void open_history_file() { char *data_path; - if ((data_path = getenv("XDG_DATA_HOME")) == NULL) + if ((data_path = get_env_var("XDG_DATA_HOME")) == NULL) { char *user_home; - if ((user_home = getenv("HOME")) == NULL) - { - perror("getenv"); + if ((user_home = get_env_var("HOME")) == NULL) exit(EXIT_FAILURE); - } - data_path = strdup(user_home); + + data_path = user_home; data_path = realloc(data_path, strlen(data_path) + strlen("/.local/share/") + 1); data_path = strcat(data_path, "/.local/share/"); } @@ -233,6 +231,8 @@ void open_history_file() } fseek(term.hist.file, 0, SEEK_SET); + + free(data_path); } void close_history_file() diff --git a/src/line_parce.c b/src/line_parce.c index 9a9dfea..0584173 100644 --- a/src/line_parce.c +++ b/src/line_parce.c @@ -240,10 +240,10 @@ cmds_p *process_line(char *line) if (msg == NULL) { - msg = getenv(tmp); + msg = get_env_var(tmp); if (msg == NULL) - msg = "\n"; + msg = strdup(""); } append_to_str_arr(&curr_pipe->args, &curr_pipe->args_am, msg); diff --git a/src/prompt.c b/src/prompt.c index 18cd0cc..aff9db9 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -42,7 +42,7 @@ char *compose_prompt() char *prompt = strdup(""); // Username - char *username = getenv("USER"); + char *username = strdup(getlogin()); if (username == NULL) username = "none"; prompt = realloc(prompt, strlen(prompt) + strlen("\033[97;44m") + strlen(username) + 2); diff --git a/src/utils.c b/src/utils.c index 7320792..e6ec61d 100644 --- a/src/utils.c +++ b/src/utils.c @@ -201,7 +201,7 @@ char *get_curr_dir_name() int get_num_of_lines(FILE *file) { int n = 0; - char ch, pch; + int ch, pch; long curr_pos = ftell(file); fseek(file, 0, SEEK_SET); @@ -244,4 +244,16 @@ bool str_is_in_arr(char **arr, size_t sz, char *str) } return false; -} \ No newline at end of file +} + +char *get_env_var(char *name) +{ + char *env_var = getenv(name); + if (env_var == NULL) + { + fprintf(stderr, "No $%s environment variable\n", name); + return NULL; + } + + return strdup(env_var); +}