diff options
| author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-08-05 20:34:04 +0200 | 
|---|---|---|
| committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-08-05 20:34:04 +0200 | 
| commit | 9f626ecdde8f9662468bb633e02854982ca80311 (patch) | |
| tree | e87a7ce0d8b25b02125e902c7b1bf0a4d51ae5ac /src | |
| parent | Fixed $PWD at startup (diff) | |
| download | 42-minishell-9f626ecdde8f9662468bb633e02854982ca80311.tar.gz 42-minishell-9f626ecdde8f9662468bb633e02854982ca80311.tar.bz2 42-minishell-9f626ecdde8f9662468bb633e02854982ca80311.tar.xz 42-minishell-9f626ecdde8f9662468bb633e02854982ca80311.tar.zst 42-minishell-9f626ecdde8f9662468bb633e02854982ca80311.zip  | |
fixed pwd
Diffstat (limited to '')
| -rw-r--r-- | src/b_cd.c | 90 | 
1 files changed, 30 insertions, 60 deletions
@@ -60,6 +60,7 @@ static void  	{  		ft_sprintf(fmt, "%s=%s", "OLDPWD", pwd);  		b_export_with_equals(fmt, msh); +		ft_memdel((void*)pwd);  	}  	else  	{ @@ -70,21 +71,31 @@ static void  }  static void -	b_fill_repath(char *repath[], +	b_fill_repath(char repath[],  				char *splited[])  {  	size_t	i;  	size_t	j;  	i = 0; +	repath[0] = (splited[0] == NULL) ? '/' : repath[0];  	while (splited[i] != NULL)  	{  		if (ft_strncmp(splited[i], "..", 3) == 0)  		{ -			j = ft_strlen(*repath); -			while (*repath[j] != '/') +			j = ft_strlen(repath); +			while (repath[j] != '/' && j > 0)  				j--; -			*repath[j] = '\0'; +			repath[j] = '\0'; +		} +		else if (ft_strncmp(splited[i], ".", 2) == 0) +		{ +		} +		else +		{ +			j = ft_strlen(repath) + 1; +			repath[j - 1] = '/'; +			ft_strlcpy(repath + j, splited[i], ft_strlen(splited[i]) + 1);  		}  		i++;  	} @@ -93,71 +104,30 @@ static void  static void  	b_upgrade_pwd(const char path[], t_msh *msh)  { +	char	*tmp;  	char	**splited;  	char	repath[262144]; -	t_bool	slash_first; +	char	fmt[262144];  	b_set_oldpwd(msh); -	slash_first = FALSE; -	if (path[0] == '/') -		slash_first = TRUE; -	if (slash_first == TRUE) -		ft_memcpy(repath, "/", 2); -	else  -	{ -		/* msh->cwd; */ -	} +	repath[0] = '\0'; +	if (path[0] != '/') +		ft_memcpy(repath, msh->cwd, ft_strlen(msh->cwd));  	if ((splited = ft_split(path, '/')) == NULL)  		f_fail_alloc_and_destroy(msh); -	b_fill_repath((char**)&repath, splited); +	b_fill_repath(repath, splited); +	repath[0] = (repath[0] == '\0') ? '/' : repath[0];  	ft_delwords(splited); -	u_subst_var_value("$PWD", repath, msh); -	ft_printf("[%s]\n", repath); -} - -/* TODO: fix export OLDPWDQWE= */ - -static void -	b_old_upgrade_pwd(const char path[], t_msh *msh) -{ -	char	**splited; -	char	*rtmp; -	char	repath[131072]; -	size_t	i; -	size_t	j; - -	if (path[0] == '/') -	{ -		u_subst_var_value("$PWD", path, msh); -		/* TODO: /home///////////////////////////////////jozan////////////// */ -		return ; -	} -	rtmp = u_get_var_value("$PWD", msh); -	ft_memcpy(repath, rtmp, ft_strlen(rtmp) + 1); -	ft_memdel((void*)&rtmp); -	splited = ft_split(path, '/'); -	i = 0; -	while (splited[i] != '\0') +	if ((tmp = u_get_var_value("$PWD", msh)) != NULL) +		u_subst_var_value("$PWD", repath, msh); +	else  	{ -		ft_printf("{%s}\n{%s}\n", path, path + i); -		if (splited[i][0] == '.' && splited[i][1] == '.') -		{ -			j = ft_strlen(repath); -			ft_printf("[%s]\n", repath); -			while (repath[j] != '/') -				j--; -			repath[j] = '\0'; -		} -		else -		{ -			j = ft_strlen(repath) + 1; -			repath[j - 1] = '/'; -			ft_strlcpy(repath + j, splited[i], ft_strlen(splited[i]) + 1); -		} -		i++; +		ft_sprintf(fmt, "%s=%s", "PWD", repath); +		b_export_with_equals(fmt, msh);  	} -	ft_delwords(splited); -	u_subst_var_value("$PWD", repath, msh); +	ft_memdel((void*)&msh->cwd); +	if ((msh->cwd = ft_strdup(repath)) == NULL) +		f_fail_alloc_and_destroy(msh);  }  uint8_t  | 
