diff options
-rw-r--r-- | TODO.org | 3 | ||||
-rw-r--r-- | src/b_cd.c | 42 | ||||
-rw-r--r-- | src/b_export.c | 10 | ||||
-rw-r--r-- | src/b_export_mute.c | 12 | ||||
-rw-r--r-- | src/b_export_next.c | 9 | ||||
-rw-r--r-- | src/m_init.c | 18 | ||||
-rw-r--r-- | src/m_loop.c | 20 | ||||
-rw-r--r-- | src/m_mshrc.c | 9 | ||||
-rw-r--r-- | src/m_prompt.c | 32 | ||||
-rw-r--r-- | src/p_args.c | 36 | ||||
-rw-r--r-- | src/p_args.h | 2 | ||||
-rw-r--r-- | src/p_lcom.c | 2 | ||||
-rw-r--r-- | src/p_lcom.h | 2 | ||||
-rw-r--r-- | src/p_lcom_next.c | 93 | ||||
-rw-r--r-- | src/p_lcom_next.h | 2 | ||||
-rw-r--r-- | src/s_com.c | 7 | ||||
-rw-r--r-- | src/s_init.c | 44 | ||||
-rw-r--r-- | src/s_struct.h | 4 | ||||
-rw-r--r-- | src/u_vars.c | 109 | ||||
-rw-r--r-- | src/u_vars.h | 10 |
20 files changed, 215 insertions, 251 deletions
@@ -6,7 +6,7 @@ ** DONE [#A] /true/path/to/non-existant/bin/fail-plus-bad-fork ** DONE [#A] fix pwd when cd / ** DONE [#A] Multiline pipes (ls |) -** TODO [#A] Go full tok + quotes +** DONE [#A] Go full tok + quotes ** TODO [#A] Variables quotes qwe="okokoko kkok" asd='bav' ** TODO [#A] ls qweqwe; echo $? <---- substitute ** TODO [#A] Careful p_line \; @@ -17,6 +17,7 @@ ** DONE [#C] fix that goddamn exit(2) 25 leak ** DONE [#C] lpipes leaks on builtins (same as above man) ** DONE [#C] leaks on builtins (same as above man) +** TODO [#C] Variables stack rework ** TODO [#C] Handle memory ** TODO [#C] Go full stack to ken dash in the ass ** TODO [#C] Handle comments better # @@ -16,6 +16,7 @@ #include <limits.h> #include "b_export_next.h" +#include "d_define.h" #include "f_fail.h" #include "s_destroy.h" #include "s_struct.h" @@ -24,39 +25,25 @@ #include "u_vars_next.h" static void - set_path(char **path, - char *args[], - t_msh *msh) -{ - if ((*path = ft_strdup(*args)) == NULL) - { - f_alloc_and_destroy_msh(msh); - } -} - -static void b_set_oldpwd(t_msh *msh) { - char *pwd; - char *tmp; - char fmt[PATH_MAX]; + char pwd[PATH_MAX]; + char tmp[PATH_MAX]; - if ((pwd = u_get_var_value("$PWD", msh)) == NULL) + u_get_var_value(pwd, "$PWD", PATH_MAX, msh); + if (pwd[0] == C_NUL) { - if ((pwd = ft_strdup(msh->cwd)) == NULL) - f_alloc_and_destroy_msh(msh); + ft_strlcpy(pwd, msh->cwd, PATH_MAX); } - if ((tmp = u_get_var_value("$OLDPWD", msh)) == NULL) + u_get_var_value(tmp, "$OLDPWD", PATH_MAX, msh); + if (tmp[0] == C_NUL) { - ft_sprintf(fmt, "%s=%s", "OLDPWD", pwd); - b_export_with_equals(fmt, msh); - ft_memdel((void*)pwd); + ft_sprintf(tmp, "%s=%s", "OLDPWD", pwd); + b_export_with_equals(tmp, msh); } else { u_subst_var_value("$OLDPWD", pwd, msh); - ft_memdel((void*)&pwd); - ft_memdel((void*)&tmp); } } @@ -125,7 +112,7 @@ uint8_t t_msh *msh) { const uint64_t argc = u_builtins_get_argc((const char**)args); - char *path; + char path[PATH_MAX]; if (argc >= 2) { @@ -134,7 +121,8 @@ uint8_t } else if (argc == 0) { - if ((path = u_get_var_value("$HOME", msh)) == NULL) + u_get_var_value(path, "$HOME", PATH_MAX, msh); + if (path[0] == C_NUL) { ft_dprintf(STDERR_FILENO, "minishell: cd: %s\n", FT_FAIL_HOME_NOT_SET); @@ -142,14 +130,12 @@ uint8_t } } else - set_path(&path, args, msh); + ft_strlcpy(path, *args, PATH_MAX); if (chdir(path) != 0) { f_fail_chd("cd", path, msh); - ft_memdel((void*)&path); return (1); } b_upgrade_pwd(path, msh); - ft_memdel((void*)&path); return (0); } diff --git a/src/b_export.c b/src/b_export.c index 9987583..aa0c5d0 100644 --- a/src/b_export.c +++ b/src/b_export.c @@ -84,12 +84,11 @@ void } uint8_t - b_export(char *args[], - t_msh *msh) + b_export(char *args[], t_msh *msh) { /* TODO: norme */ char **ptr; - char *varval; + char varval[4096]; char fmt[4096]; t_bool next; uint8_t r; @@ -111,12 +110,11 @@ uint8_t { next = TRUE; ft_sprintf(fmt, "$%s", *ptr); - varval = u_get_cstm_vr(fmt, msh); - if (varval != NULL) + u_get_custom_var(varval, fmt, 4096, msh); + if (varval[0] != C_NUL) { ft_sprintf(fmt, "%s=%s", *ptr, varval); b_add_to_env_from_globals(*ptr, fmt, msh); - ft_memdel((void*)&varval); } } else if (next == FALSE && check_equals(*ptr) == TRUE) diff --git a/src/b_export_mute.c b/src/b_export_mute.c index 4969320..ac72a7e 100644 --- a/src/b_export_mute.c +++ b/src/b_export_mute.c @@ -16,7 +16,7 @@ #include "b_export.h" #include "b_export_next.h" -#include "d_enum.h" +#include "d_define.h" #include "f_fail.h" #include "s_destroy.h" #include "s_line.h" @@ -26,12 +26,11 @@ #include "u_vars.h" uint8_t - b_export_mute(char *args[], - t_msh *msh) + b_export_mute(char *args[], t_msh *msh) { /* TODO: norme */ char **ptr; - char *varval; + char varval[4096]; char fmt[4096]; t_bool next; uint8_t r; @@ -52,12 +51,11 @@ uint8_t { next = TRUE; ft_sprintf(fmt, "$%s", *ptr); - varval = u_get_cstm_vr(fmt, msh); - if (varval != NULL) + u_get_custom_var(varval, fmt, 4096, msh); + if (varval[0] != C_NUL) { ft_sprintf(fmt, "%s=%s", *ptr, varval); b_add_to_env_from_globals(*ptr, fmt, msh); - ft_memdel((void*)&varval); } } else if (next == FALSE && check_equals(*ptr) == TRUE) diff --git a/src/b_export_next.c b/src/b_export_next.c index c46389a..059fbc6 100644 --- a/src/b_export_next.c +++ b/src/b_export_next.c @@ -92,10 +92,9 @@ static void } void - b_export_with_equals(const char arg[], - t_msh *msh) + b_export_with_equals(const char arg[], t_msh *msh) { - char *varval; + char varval[4096]; char **var; int64_t env_i; @@ -105,8 +104,10 @@ void ft_memdel((void*)&msh->envp[env_i]); if ((msh->envp[env_i] = ft_strdup(arg)) == NULL) f_alloc_and_destroy_msh(msh); + return ; } - else if ((varval = u_get_cstm_vr(var[FT_VAR_NAME], msh)) != NULL) + u_get_custom_var(varval, var[FT_VAR_NAME], 4096, msh); + if (varval[0] != C_NUL) { b_add_to_env(arg, msh); lvars_delone(&msh->vars, var[FT_VAR_NAME] + 1); diff --git a/src/m_init.c b/src/m_init.c index 8b8a994..91baf7c 100644 --- a/src/m_init.c +++ b/src/m_init.c @@ -12,7 +12,9 @@ #include <libft.h> #include <stdlib.h> +#include <limits.h> +#include "d_define.h" #include "s_struct.h" #include "u_vars.h" #include "u_vars_next.h" @@ -20,17 +22,21 @@ void m_init_custom_vars(t_msh *msh) { - char *home; - char fmt[255]; + char home[PATH_MAX]; + char fmt[PATH_MAX]; u_subst_var_value("$PS1", FT_DEFAULT_PS_ONE, msh); u_subst_var_value("$PS2", FT_DEFAULT_PS_TWO, msh); u_subst_var_value("$PS3", FT_DEFAULT_PS_THR, msh); u_subst_var_value("$PS4", FT_DEFAULT_PS_FOU, msh); - if ((home = u_get_var_value("$HOME", msh)) != NULL) + u_get_var_value(home, "$HISTFILE", PATH_MAX, msh); + if (home[0] == C_NUL) { - ft_sprintf(fmt, "%s/%s", home, FT_DEFAULT_HISTFILE); - u_subst_var_value("$HISTFILE", fmt, msh); - ft_memdel((void*)&home); + u_get_var_value(home, "$HOME", PATH_MAX, msh); + if (home[0] != C_NUL) + { + ft_sprintf(fmt, "%s/%s", home, FT_DEFAULT_HISTFILE); + u_subst_var_value("$HISTFILE", fmt, msh); + } } } diff --git a/src/m_loop.c b/src/m_loop.c index 17668a5..d5a3ceb 100644 --- a/src/m_loop.c +++ b/src/m_loop.c @@ -15,7 +15,9 @@ #include <stdlib.h> #include <fcntl.h> #include <unistd.h> +#include <limits.h> +#include "d_define.h" #include "e_line.h" #include "m_loop_next.h" #include "m_prompt.h" @@ -36,18 +38,20 @@ void m_dump_hist(t_msh *msh) { int32_t fd; - char *histfile_path; + char histfile[PATH_MAX]; - if (ft_strlen(msh->hist) > 0 && - (histfile_path = u_get_var_value("$HISTFILE", msh)) != NULL) + if (ft_strlen(msh->hist) > 0) { - if ((fd = open(histfile_path, - O_WRONLY | O_CREAT | O_APPEND, 0644)) != -1) + u_get_var_value(histfile, "$HISTFILE", PATH_MAX, msh); + if (histfile[0] != C_NUL) { - write(fd, msh->hist, ft_strlen(msh->hist)); - close(fd); + if ((fd = open(histfile, + O_WRONLY | O_CREAT | O_APPEND, 0644)) != -1) + { + ft_dprintf(fd, "%s", msh->hist); + close(fd); + } } - ft_memdel((void*)&histfile_path); } } diff --git a/src/m_mshrc.c b/src/m_mshrc.c index f730113..7f6b8b9 100644 --- a/src/m_mshrc.c +++ b/src/m_mshrc.c @@ -15,6 +15,7 @@ #include <stdlib.h> #include <fcntl.h> #include <unistd.h> +#include <limits.h> #include "d_define.h" #include "s_struct.h" @@ -24,15 +25,13 @@ uint8_t m_source_mshrc(t_msh *msh) { + char file[PATH_MAX]; + char home[PATH_MAX]; int32_t fd; - char *home; - char file[2048]; uint8_t ret; - if ((home = u_get_var_value("$HOME", msh)) == NULL) - return (0); + u_get_var_value(home, "$HOME", PATH_MAX, msh); ft_sprintf(file, "%s/%s", home, FT_MINISHELLRC); - ft_memdel((void*)&home); if ((fd = open(file, O_RDONLY)) < 0) return (0); ret = m_loop(fd, msh); diff --git a/src/m_prompt.c b/src/m_prompt.c index 1ea8d70..d0269e4 100644 --- a/src/m_prompt.c +++ b/src/m_prompt.c @@ -13,6 +13,7 @@ #include <libft.h> #include <stdint.h> #include <unistd.h> +#include <limits.h> #include "d_define.h" #include "m_prompt.h" @@ -20,12 +21,11 @@ #include "u_vars.h" static char - *m_subst_prompt_rice(char var[], - t_msh *msh) + *m_subst_prompt_rice(char var[], t_msh *msh) { + char tmp[PATH_MAX]; size_t i; size_t j; - char *tmp; i = 0; while (var[i] != '\0') @@ -34,19 +34,16 @@ static char { if (var[i + 1] == 'w') { - tmp = u_get_var_value("$PWD", msh); - if (tmp != NULL) - { + u_get_var_value(tmp, "$PWD", PATH_MAX, msh); + if (tmp[0] != C_NUL) var = ft_strsubst(var, "\\w", tmp); - ft_memdel((void*)&tmp); - } else var = ft_strsubst(var, "\\w", "?"); } else if (var[i + 1] == 'W') { - tmp = u_get_var_value("$PWD", msh); - if (tmp != NULL) + u_get_var_value(tmp, "$PWD", PATH_MAX, msh); + if (tmp[0] != C_NUL) { j = ft_strlen(tmp); if (j > 1) @@ -58,7 +55,6 @@ static char ((ft_strlen(tmp) - j) + 1) * sizeof(char)); } var = ft_strsubst(var, "\\W", tmp); - ft_memdel((void*)&tmp); } else var = ft_strsubst(var, "\\w", "?"); @@ -74,26 +70,24 @@ static char } static void - m_update_psx(uint8_t x, - t_msh *msh) + m_update_psx(uint8_t x, t_msh *msh) { - char *var; + char var[2048]; char psx[5]; ft_sprintf(psx, "$PS%hhu", x); - if ((var = u_get_var_value(psx, msh)) == NULL) + u_get_var_value(var, psx, 2048, msh); + if (var[0] == C_NUL) { msh->ps[x - 1][0] = '\0'; return ; } - var = m_subst_prompt_rice(var, msh); + m_subst_prompt_rice(var, msh); ft_strlcpy(msh->ps[x - 1], var, ft_strlen(var) + 1); - ft_memdel((void*)&var); } void - m_prompt_psx(uint8_t x, - t_msh *msh) + m_prompt_psx(uint8_t x, t_msh *msh) { m_update_psx(x, msh); ft_dprintf(STDERR_FILENO, "%s", msh->ps[x - 1]); diff --git a/src/p_args.c b/src/p_args.c index ae92523..b449ba3 100644 --- a/src/p_args.c +++ b/src/p_args.c @@ -20,27 +20,6 @@ #include "p_args_escape.h" #include "u_utils.h" -/* ================= */ -/* TODO: DELETE THIS */ -/* ================= */ - -/* static void */ -/* p_print(char *words[]) */ -/* { */ -/* char **ptr; */ - -/* ptr = words; */ -/* while (*ptr != NULL) */ -/* { */ -/* ft_printf("[%s]\n", *ptr); */ -/* ptr++; */ -/* } */ -/* } */ - -/* ================== */ -/* TODO: DELETE ABOVE */ -/* ================== */ - static void p_meet_bs(char *ptr, t_quote_mode mode) { @@ -166,15 +145,14 @@ static char return (NULL); } p_args_escape_chars_and_quotes(words); - /* p_print(words); */ - /* exit(0); */ return (words); } char - **p_split_args(const char word[], int8_t redir) + **p_split_args(char word[], int8_t redir) { char **words; + size_t i; words = NULL; if (redir == 0) @@ -183,6 +161,16 @@ char return (NULL); return (words); } + i = ft_strlen(word); + while (ft_ischarset("<>", word[i]) == FALSE) + i--; + i--; + while (redir > 0 && ft_isdigit(word[i]) == TRUE) + i--; + word[i] = C_NUL; + ft_printf("[%s]\n", word); + if ((words = p_split_words_no_rdr(word)) == NULL) + return (NULL); return (words); /* char **words; */ /* char *subst; */ diff --git a/src/p_args.h b/src/p_args.h index 899f7ea..fa61d92 100644 --- a/src/p_args.h +++ b/src/p_args.h @@ -15,6 +15,6 @@ #include <stdint.h> -char **p_split_args(const char word[], int8_t redir); +char **p_split_args(char word[], int8_t redir); #endif diff --git a/src/p_lcom.c b/src/p_lcom.c index bd5edcb..a8c42ce 100644 --- a/src/p_lcom.c +++ b/src/p_lcom.c @@ -86,7 +86,7 @@ static void } int8_t - get_redir(const char word[], + p_get_redir(const char word[], t_com **com) { /* TODO: norme */ diff --git a/src/p_lcom.h b/src/p_lcom.h index 2ff2c7e..796c684 100644 --- a/src/p_lcom.h +++ b/src/p_lcom.h @@ -17,7 +17,7 @@ #include "s_struct.h" -int8_t get_redir(const char word[], t_com **com); +int8_t p_get_redir(const char word[], t_com **com); int8_t p_lcom(const char line[], t_msh *msh); #endif diff --git a/src/p_lcom_next.c b/src/p_lcom_next.c index 435a710..7bc306e 100644 --- a/src/p_lcom_next.c +++ b/src/p_lcom_next.c @@ -13,72 +13,94 @@ #include <libft.h> #include <stdlib.h> #include <stdint.h> +#include <limits.h> -#include "d_enum.h" +#include "d_define.h" #include "s_destroy.h" #include "f_fail.h" #include "s_struct.h" +#include "u_utils.h" #include "u_vars.h" #include "u_vars_next.h" /* TODO: norme */ -static int8_t - subst_those_vars(int64_t i, - char **p_words, - t_msh *msh) +/* static int8_t */ +/* p_subst_those_vars(int64_t i, */ +/* char **p_words, */ +/* t_msh *msh) */ +/* { */ +/* size_t varlen; */ +/* char *s_varname; */ +/* char *varval; */ + +/* varval = NULL; */ +/* s_varname = NULL; */ +/* varlen = i + 1; */ +/* while ((*p_words)[varlen] != '\0' && */ +/* ft_ischarset("$=/#@%^*+{}[],.-", (*p_words)[varlen]) == FALSE) */ +/* varlen += 1; */ +/* if (!(s_varname = ft_substr(*p_words, (uint32_t)i, varlen - i))) */ +/* return (-1); */ +/* varval = u_get_var_value(s_varname, msh); */ +/* *p_words = ft_strsubst(*p_words, s_varname, varval); */ +/* ft_memdel((void*)&s_varname); */ +/* ft_memdel((void*)&varval); */ +/* return (0); */ +/* } */ + +static char + *p_subst_this_var(int64_t i, char word[], t_msh *msh) { + char tmp[4096]; + char varval[4096]; + char *ptr; size_t varlen; - char *s_varname; - char *varval; - varval = NULL; - s_varname = NULL; + ptr = word; varlen = i + 1; - while ((*p_words)[varlen] != '\0' && - ft_ischarset("$=/#@%^*+{}[],.-", (*p_words)[varlen]) == FALSE) + while (ptr[varlen] != C_NUL && + ft_ischarset("$=\\/@%^*+{}[]<>,.-", ptr[varlen]) == FALSE && + ft_iswhitespace(ptr[varlen]) == FALSE) varlen += 1; - if (!(s_varname = ft_substr(*p_words, (uint32_t)i, varlen - i))) - return (-1); - varval = u_get_var_value(s_varname, msh); - *p_words = ft_strsubst(*p_words, s_varname, varval); - ft_memdel((void*)&s_varname); - ft_memdel((void*)&varval); - return (0); + ft_strlcpy(tmp, ptr + i, varlen + 1 - i); + u_get_var_value(varval, tmp, 4096, msh); + ft_strlcpy(tmp, ptr + varlen + 1, varlen); + word = ft_nrealloc(word, i, i + ft_strlen(varval) + ft_strlen(tmp) + 1); + ft_strlcpy(word + i, varval, ft_strlen(varval) + 1); + ft_printf("[%s]\n", tmp); + ft_strlcpy(word + i + ft_strlen(varval), tmp, ft_strlen(tmp) + 1); + exit(0); + return (word); } char - **p_subst_vars(char *words[], - t_msh *msh) + *p_subst_vars(char word[], t_msh *msh) { - char **p_words; - int64_t i; + char *ptr; - p_words = words; - i = 0; - while (*p_words) + ptr = word; + while (*ptr != C_NUL) { - while ((i = ft_strlchr((*p_words), '$')) != -1) + if (*ptr == '$' && u_is_not_escaped(word, ptr) == TRUE) { - if (*(*p_words) + i - 1 != '\\') - { - if (subst_those_vars(i, p_words, msh) != 0) - return (NULL); - } + if ((word = p_subst_this_var((ptr - word), word, msh)) == NULL) + return (NULL); } - p_words += 1; + ptr++; } - return (words); + return (word); } char **p_subst_home(char *words[], t_msh *msh) { - char *path; + char path[PATH_MAX]; char **ptr; - if ((path = u_get_var_value("$HOME", msh)) == NULL) + u_get_var_value(path, "$HOME", PATH_MAX, msh); + if (path[0] == C_NUL) return (words); ptr = words; while (*ptr != NULL) @@ -89,7 +111,6 @@ char } ptr++; } - ft_memdel((void*)&path); return (words); } diff --git a/src/p_lcom_next.h b/src/p_lcom_next.h index dea86bd..8fbbb99 100644 --- a/src/p_lcom_next.h +++ b/src/p_lcom_next.h @@ -17,7 +17,7 @@ #include "s_struct.h" -char **p_subst_vars(char *words[], t_msh *msh); +char *p_subst_vars(char word[], t_msh *msh); char **p_subst_args(const char word[], int8_t redir); char **p_subst_home(char *word[], t_msh *msh); char **p_check_args_equals(char *words[], t_msh *msh); diff --git a/src/s_com.c b/src/s_com.c index 99b3158..ac23f35 100644 --- a/src/s_com.c +++ b/src/s_com.c @@ -112,12 +112,11 @@ t_com com->rdrfd = 0; com->rdrpath = NULL; com->env_fork = NULL; - if (get_redir(word, &com) != 0) + if (p_get_redir(word, &com) != 0) return (NULL); - if ((words = p_split_args(word, com->redir)) == NULL) + if ((word = p_subst_vars(word, msh)) == NULL) return (NULL); - /* TODO: subst vars is before getting words fuck my life */ - if ((words = p_subst_vars(words, msh)) == NULL) + if ((words = p_split_args(word, com->redir)) == NULL) return (NULL); if ((words = p_subst_home(words, msh)) == NULL) return (NULL); diff --git a/src/s_init.c b/src/s_init.c index 7353ed8..8fc8a79 100644 --- a/src/s_init.c +++ b/src/s_init.c @@ -107,64 +107,60 @@ static char static void inc_shlvl(t_msh *msh) { - int32_t shlvl; - char *str_one; - char *str_two; - char *tmp; char fmt[8]; + char tmp[255]; + char *str; + int32_t shlvl; - if ((tmp = u_get_var_value("$SHLVL", msh)) == NULL) + u_get_var_value(tmp, "$SHLVL", 255, msh); + if (tmp[0] == C_NUL) { ft_memcpy(fmt, "SHLVL=1", 8); b_export_with_equals(fmt, msh); } else { - ft_memdel((void*)&tmp); - str_two = u_get_var_value("$SHLVL", msh); - shlvl = ft_atoi(str_two); + shlvl = ft_atoi(tmp); if (shlvl >= 999) f_shlvl_too_high(shlvl); shlvl = (shlvl >= 999) ? 0 : shlvl; shlvl = (shlvl < 0) ? 0 : shlvl + 1; - str_one = ft_itoa(shlvl); - u_subst_var_value("$SHLVL", str_one, msh); - ft_memdel((void*)&str_one); - ft_memdel((void*)&str_two); + str = ft_itoa(shlvl); + u_subst_var_value("$SHLVL", str, msh); + ft_memdel((void*)&str); } } -static char - *set_cwd(t_msh *msh) +static void + set_cwd(char cwd[], t_msh *msh) { - char *cwd; char fmt[PATH_MAX]; DIR *dir; - if ((cwd = u_get_var_value("$PWD", msh)) == NULL) + u_get_var_value(cwd, "$PWD", PATH_MAX, msh); + if (cwd[0] == C_NUL) { - cwd = getcwd(NULL, 0); + getcwd(cwd, PATH_MAX); ft_sprintf(fmt, "%s=%s", "PWD", cwd); b_export_with_equals(fmt, msh); - return (cwd); + return ; } if ((dir = opendir(cwd)) != NULL) closedir(dir); else if (errno == ENOENT) { - ft_memdel((void*)&cwd); - cwd = getcwd(NULL, 0); + getcwd(cwd, PATH_MAX); ft_sprintf(fmt, "%s=%s", "PWD", cwd); b_export_with_equals(fmt, msh); - return (cwd); + return ; } - return (cwd); } t_msh *init_msh(char *const argv[], char *const envp[]) { + char cwd[PATH_MAX]; t_msh *msh; if (!(msh = (t_msh*)malloc(sizeof(t_msh)))) @@ -179,7 +175,9 @@ t_msh init_buptr(msh); msh->curr = NULL; msh->vars = NULL; - msh->cwd = set_cwd(msh); + set_cwd(cwd, msh); + if ((msh->cwd = ft_strdup(cwd)) == NULL) + return (NULL); msh->env_fork_tmp[0][0] = '\0'; inc_shlvl(msh); s_init_sqb_ref_one(msh); diff --git a/src/s_struct.h b/src/s_struct.h index 01c8f1f..a1d1828 100644 --- a/src/s_struct.h +++ b/src/s_struct.h @@ -43,7 +43,7 @@ typedef struct s_com int8_t redir; } t_com; -struct s_lpipes +struct s_lpipes { struct s_com *com; struct s_lpipes *next; @@ -61,8 +61,8 @@ typedef struct s_line { struct s_com *com; struct s_lpipes *pipes; - struct s_line *next; uint8_t nextif; + struct s_line *next; } t_line; typedef struct s_msh diff --git a/src/u_vars.c b/src/u_vars.c index 9594036..95c6ffa 100644 --- a/src/u_vars.c +++ b/src/u_vars.c @@ -21,26 +21,11 @@ #include "s_struct.h" #include "u_utils.h" -static char - *u_dup_env(char *p_env, - t_msh *msh) -{ - char *varval; - - if (!(varval = ft_strdup(p_env))) - { - f_alloc_and_destroy_msh(msh); - } - return (varval); -} - -static char - *u_get_frm_env(const char varname[], - t_msh *msh) +static void + u_get_frm_env(char str[], const char varname[], size_t dstsize, t_msh *msh) { char **env_dup; char *pp_env; - char *varval; size_t i; env_dup = u_get_env_var_names(msh); @@ -54,47 +39,38 @@ static char pp_env += 1; if (*pp_env == '=') pp_env += 1; - varval = u_dup_env(pp_env, msh); + ft_strlcpy(str, pp_env, dstsize); ft_delwords(env_dup); - return (varval); + return ; } i++; } ft_delwords(env_dup); - return (NULL); } -static char - *u_get_special_var(const char varname[], +static void + u_get_special_var(char str[], + const char varname[], + size_t dstsize, t_msh *msh) { - char *varval; - if (ft_strncmp(varname, FT_RET_VAR, 3) == 0) { - if ((varval = ft_uitoa(msh->ret)) == NULL) - { - f_alloc_and_destroy_msh(msh); - } - return (varval); + ft_strlcpy(str, ft_uitoa(msh->ret), 4); } else if (ft_strncmp(varname, FT_ZER_VAR, 3) == 0) { - if ((varval = ft_strdup(msh->shname)) == NULL) - { - f_alloc_and_destroy_msh(msh); - } - return (varval); + ft_strlcpy(str, msh->shname, dstsize); } - return (NULL); } -char - *u_get_cstm_vr(const char varname[], - t_msh *msh) +void + u_get_custom_var(char str[], + const char varname[], + size_t dstsize, + t_msh *msh) { t_lvars *ptr; - char *varval; ptr = msh->vars; while (ptr != NULL && @@ -104,51 +80,40 @@ char } if (ptr != NULL) { - if (!(varval = ft_strdup(ptr->val))) - { - f_alloc_and_destroy_msh(msh); - } - return (varval); - } - else - { - return (NULL); + ft_strlcpy(str, ptr->val, dstsize); } } /* -** char* -** u_get_var_value(const char varname[], t_msh *msh); +** void +** u_get_var_value(char str[], const char varname[], size_t dstsize, t_msh *msh) ** ** DESCRIPTION -** The u_get_var_value() function returns -** a heap-allocated, null-terminated string -** that may later be free'd containing the -** value of the variable varname[] including -** the '$' prefix. NULL is returned if varname[] -** wasn't found. +** The u_get_var_value() function fills +** str[] with a null-terminated string +** containing the value of the variable varname[] +** including the '$' prefix, in a similar way as +** sprintf does. No more than dstsize bytes will be +** copied into str[]. str[0] is set to \000 if +** varname[] wasn't found. */ -char - *u_get_var_value(const char varname[], +void + u_get_var_value(char str[], + const char varname[], + size_t dstsize, t_msh *msh) { - /* TODO: check behaviour on empty vars -> "QWE=" */ - char *varval; - - varval = NULL; - if ((varval = u_get_special_var(varname, msh)) != NULL) - { - return (varval); - } - else if ((varval = u_get_cstm_vr(varname, msh)) != NULL) + str[0] = C_NUL; + u_get_special_var(str, varname, dstsize, msh); + if (str[0] != C_NUL) { - return (varval); + return ; } - else + u_get_custom_var(str, varname, dstsize, msh); + if (str[0] != C_NUL) { - varval = u_get_frm_env(varname, msh); - return (varval); + return ; } - return (NULL); + u_get_frm_env(str, varname, dstsize, msh); } diff --git a/src/u_vars.h b/src/u_vars.h index 0d83b4b..fbdaffc 100644 --- a/src/u_vars.h +++ b/src/u_vars.h @@ -15,7 +15,13 @@ #include "s_struct.h" -char *u_get_cstm_vr(const char varname[], t_msh *msh); -char *u_get_var_value(const char varname[], t_msh *msh); +void u_get_custom_var(char str[], + const char varname[], + size_t dstsize, + t_msh *msh); +void u_get_var_value(char str[], + const char varname[], + size_t dstsize, + t_msh *msh); #endif |