diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-09-19 16:59:38 +0200 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-09-19 16:59:38 +0200 |
commit | 49b6183dc0171ac040bdd2faf6f8efa0448567c5 (patch) | |
tree | eaff08562cedef9bf4267b0d17efce0485d2f51e /src | |
parent | TODO update (diff) | |
download | 42-minishell-49b6183dc0171ac040bdd2faf6f8efa0448567c5.tar.gz 42-minishell-49b6183dc0171ac040bdd2faf6f8efa0448567c5.tar.bz2 42-minishell-49b6183dc0171ac040bdd2faf6f8efa0448567c5.tar.xz 42-minishell-49b6183dc0171ac040bdd2faf6f8efa0448567c5.tar.zst 42-minishell-49b6183dc0171ac040bdd2faf6f8efa0448567c5.zip |
Cleaner variables handling $0 $1 $2 $3
Diffstat (limited to 'src')
-rw-r--r-- | src/d_define.h | 1 | ||||
-rw-r--r-- | src/f_alloc.c | 4 | ||||
-rw-r--r-- | src/f_chdir.c | 2 | ||||
-rw-r--r-- | src/f_errno.c | 2 | ||||
-rw-r--r-- | src/f_fail.c | 2 | ||||
-rw-r--r-- | src/f_file.c | 2 | ||||
-rw-r--r-- | src/f_redir.c | 2 | ||||
-rw-r--r-- | src/m_argv.c | 4 | ||||
-rw-r--r-- | src/m_minishell.c | 7 | ||||
-rw-r--r-- | src/s_destroy.c | 1 | ||||
-rw-r--r-- | src/s_init.c | 7 | ||||
-rw-r--r-- | src/s_init.h | 3 | ||||
-rw-r--r-- | src/s_struct.h | 3 | ||||
-rw-r--r-- | src/u_vars.c | 15 |
14 files changed, 31 insertions, 24 deletions
diff --git a/src/d_define.h b/src/d_define.h index 674a141..fb387b3 100644 --- a/src/d_define.h +++ b/src/d_define.h @@ -49,7 +49,6 @@ ** ====== VARS ====== */ -#define FT_ZER_VAR "$0" #define FT_RET_VAR "$?" #define FT_PID_VAR "$$" #define FT_ARGC_VAR "$#" diff --git a/src/f_alloc.c b/src/f_alloc.c index 641bc9f..c1e0c76 100644 --- a/src/f_alloc.c +++ b/src/f_alloc.c @@ -26,7 +26,7 @@ void char tmp[255]; s_line_clear(&msh->curr); - ft_strlcpy(tmp, msh->shname, ft_strlen(msh->shname) + 1); + ft_strlcpy(tmp, msh->argv[0], 255); s_destroy(msh); ft_dprintf(STDERR_FILENO, "%s: %s\n", tmp, strerror(errno)); exit(FT_RET_ALLOC); @@ -36,5 +36,5 @@ void f_alloc_and_clear_line(t_msh *msh) { s_line_clear(&msh->curr); - ft_dprintf(STDERR_FILENO, "%s: %s\n", msh->shname, strerror(errno)); + ft_dprintf(STDERR_FILENO, "%s: %s\n", msh->argv[0], strerror(errno)); } diff --git a/src/f_chdir.c b/src/f_chdir.c index 4623c82..ce69ca5 100644 --- a/src/f_chdir.c +++ b/src/f_chdir.c @@ -23,5 +23,5 @@ void t_msh *msh) { ft_dprintf(STDERR_FILENO, "%s: %s: %s: %s\n", - msh->shname, concern, path, strerror(errno)); + msh->argv[0], concern, path, strerror(errno)); } diff --git a/src/f_errno.c b/src/f_errno.c index 360cf9f..4fd53a6 100644 --- a/src/f_errno.c +++ b/src/f_errno.c @@ -22,5 +22,5 @@ void t_msh *msh) { ft_dprintf(STDERR_FILENO, "%s: %s: %s\n", - msh->shname, concern, strerror(errno)); + msh->argv[0], concern, strerror(errno)); } diff --git a/src/f_fail.c b/src/f_fail.c index 3704b1f..c4868b2 100644 --- a/src/f_fail.c +++ b/src/f_fail.c @@ -24,7 +24,7 @@ static void const char msg[], t_msh *msh) { - ft_dprintf(STDERR_FILENO, "%s: %s: %s\n", msh->shname, concern, msg); + ft_dprintf(STDERR_FILENO, "%s: %s: %s\n", msh->argv[0], concern, msg); } void diff --git a/src/f_file.c b/src/f_file.c index c26596c..47b9ab6 100644 --- a/src/f_file.c +++ b/src/f_file.c @@ -23,7 +23,7 @@ void f_open_file(char filename[], t_msh *msh) { - ft_dprintf(STDERR_FILENO, "%s: %s: %s\n", msh->shname, + ft_dprintf(STDERR_FILENO, "%s: %s: %s\n", msh->argv[0], filename, strerror(errno)); s_destroy(msh); diff --git a/src/f_redir.c b/src/f_redir.c index 71e5613..2883977 100644 --- a/src/f_redir.c +++ b/src/f_redir.c @@ -24,7 +24,7 @@ void t_msh *msh) { ft_dprintf(STDERR_FILENO, "%s: %s: %s\n", - msh->shname, path, strerror(errno)); + msh->argv[0], path, strerror(errno)); s_line_clear(&msh->curr); s_destroy(msh); exit(1); diff --git a/src/m_argv.c b/src/m_argv.c index 7661ef7..bd415ec 100644 --- a/src/m_argv.c +++ b/src/m_argv.c @@ -56,6 +56,8 @@ static void m_read_script(char *const argv[], t_msh *msh) if ((fd = open(*(argv + 1), O_RDONLY)) == -1) f_open_file(*(argv + 1), msh); msh->fd = fd; + msh->argv = (char**)(argv + 1); + msh->argc -= 1; msh->ret = m_loop(fd, msh); close(fd); } @@ -75,7 +77,7 @@ uint8_t m_argv(int argc, char *const argv[], t_msh *msh) if (*(argv + 2) == NULL) { ft_dprintf(STDERR_FILENO, "%s: %s: option requires an argument\n", - msh->shname, FT_OPT_COMMAND); + msh->argv[0], FT_OPT_COMMAND); return (2); } msh->ret = m_comm(*(argv + 2), msh); diff --git a/src/m_minishell.c b/src/m_minishell.c index 6be605a..aac3af7 100644 --- a/src/m_minishell.c +++ b/src/m_minishell.c @@ -23,15 +23,12 @@ #include "s_init.h" #include "s_destroy.h" -int - main(int argc, - char *const argv[], - char *const envp[]) +int main(int argc, char *const argv[], char *const envp[]) { int32_t ret; t_msh *msh; - if ((msh = init_msh(argv, envp)) == NULL) + if ((msh = init_msh(argc, argv, envp)) == NULL) { ft_dprintf(STDERR_FILENO, "%s\n", strerror(errno)); return (FT_RET_ALLOC); diff --git a/src/s_destroy.c b/src/s_destroy.c index 3e7eb21..12c2f7d 100644 --- a/src/s_destroy.c +++ b/src/s_destroy.c @@ -20,7 +20,6 @@ void s_destroy(t_msh *msh) { ft_memdel((void*)&msh->prev_hist); - ft_memdel((void*)&msh->shname); ft_memdel((void*)&msh->cwd); ft_delwords(msh->envp); lvars_clear(&msh->vars); diff --git a/src/s_init.c b/src/s_init.c index 7d2b656..c71ac9a 100644 --- a/src/s_init.c +++ b/src/s_init.c @@ -155,19 +155,18 @@ static void } } -t_msh - *init_msh(char *const argv[], char *const envp[]) +t_msh *init_msh(int32_t argc, char *const argv[], char *const envp[]) { char cwd[PATH_MAX]; t_msh *msh; if ((msh = (t_msh*)malloc(sizeof(t_msh))) == NULL) return (NULL); - if ((msh->shname = ft_strdup(argv[0])) == NULL) - return (NULL); msh->envp = NULL; if ((msh->envp = dupenv(envp)) == NULL) return (NULL); + msh->argc = argc - 1; + msh->argv = (char**)argv; msh->ret = 0; init_buptr(msh); msh->fd = 0; diff --git a/src/s_init.h b/src/s_init.h index 3539086..a598043 100644 --- a/src/s_init.h +++ b/src/s_init.h @@ -14,9 +14,10 @@ #define S_INIT_H #include <stdlib.h> +#include <stdint.h> #include "s_struct.h" -t_msh *init_msh(char *const argv[], char *const envp[]); +t_msh *init_msh(int32_t argc, char *const argv[], char *const envp[]); #endif diff --git a/src/s_struct.h b/src/s_struct.h index c9f86e1..f47eda0 100644 --- a/src/s_struct.h +++ b/src/s_struct.h @@ -80,13 +80,14 @@ typedef struct s_msh struct s_lpipes *pipes; struct s_lvars *vars; struct s_lalias *alias; + int32_t argc; + char **argv; char **envp; char *prev_hist; char *curr_hist; char ps[4][255]; char env_fork_tmp[128][4096]; char sqb_ref[FT_ID_SQB_COUNT][4]; - char *shname; char *cwd; int32_t fd; uint8_t (*bu_ptr[FT_BUILTINS_COUNT])(char **, struct s_msh*); diff --git a/src/u_vars.c b/src/u_vars.c index 28040b4..eafcace 100644 --- a/src/u_vars.c +++ b/src/u_vars.c @@ -54,16 +54,25 @@ static void size_t dstsize, t_msh *msh) { - char tmp[4]; + char tmp[255]; + int32_t n; if (ft_strncmp(varname, FT_RET_VAR, 3) == 0) { ft_uitoa_s(tmp, msh->ret); ft_strlcpy(str, tmp, 4); } - else if (ft_strncmp(varname, FT_ZER_VAR, 3) == 0) + else if (ft_strncmp(varname, FT_ARGC_VAR, 3) == 0) { - ft_strlcpy(str, msh->shname, dstsize); + ft_uitoa_s(tmp, msh->argc); + ft_strlcpy(str, tmp, 255); + } + else if (ft_strlen(varname) == 2 && ft_isdigit(*(varname + 1)) == TRUE) + { + if ((n = ft_atoi(varname + 1)) < msh->argc) + ft_strlcpy(str, msh->argv[n], dstsize); + else + (void)ft_memcpy(str, "", 1 * sizeof(char)); } } |