Dev (#2)
* Restart from scratch branched out on dev removed all previous source files started test on new env2lst function * norm: splitted functions in main into appropriate file Moved lstdelenvelem & env2lst to ms_env.c Normed out files * Feat(WIP): Added possibility to cmd_env to execute commands with altered env. Added dummy function for cd, echo, setenv & unsetenv Started work on env copy feature * feature: switched env from ft_list to specific list * feature: added execution of commands from path * feature: added env, setenv & unsetenv builtins added -i option & command execution to env added setenv & unsetenv builtins * Clean-up: normed out files * Changed comportment on error for check_path functions * feature: added completion for ~ and ${} WIP leaks detected on ${} completion * fix leak on ext need test * feature: added echo cmd changed printf ref to ft_printf * feature: add cd built-in * Simple norm-out * moved env functions to libft * fixed out-of-memory access in extension * Resolved infinite loop in extension revolving WIP norming in cd * Normedout cmd_cd.c * Normed out functions in cmd_cd * Normed out funtions sorting out needed * removed -fsanitize from Makefile * corrected env -i crash & SHLVL * Delete norm.txt * added put_error_cd2 in put_error_cd.c * added inline environment
This commit is contained in:
140
srcs/cmd_cd.c
140
srcs/cmd_cd.c
@@ -6,101 +6,68 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/01/07 16:44:40 by tmaze #+# #+# */
|
||||
/* Updated: 2019/01/30 17:02:05 by tmaze ### ########.fr */
|
||||
/* Updated: 2020/01/26 22:17:48 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
void put_error_cd(char *file, char *msg)
|
||||
{
|
||||
ft_putstr("cd: ");
|
||||
ft_putstr(file);
|
||||
ft_putstr(": ");
|
||||
ft_putendl(msg);
|
||||
}
|
||||
|
||||
char *check_path_slash_cd(char *exec)
|
||||
{
|
||||
int i;
|
||||
struct stat info;
|
||||
struct stat info2;
|
||||
|
||||
if (exec[0] == '/')
|
||||
{
|
||||
if ((i = access(exec, F_OK)) != 0)
|
||||
put_error_cd(exec, "no such file or directory");
|
||||
if (i != 0)
|
||||
return (NULL);
|
||||
if ((i = stat(exec, &info)) != 0)
|
||||
put_error_cd(exec, "can't determine info");
|
||||
if (i != 0)
|
||||
return (NULL);
|
||||
if (!S_ISDIR(info.st_mode))
|
||||
{
|
||||
put_error_cd(exec, "not a directory");
|
||||
return (NULL);
|
||||
}
|
||||
return (put_error_cd(exec, "no such file or directory"));
|
||||
if ((i = lstat(exec, &info)) != 0)
|
||||
return (put_error_cd(exec, "can't determine info"));
|
||||
if ((S_ISLNK(info.st_mode)
|
||||
&& ((i = stat(exec, &info2)) != 0 || !S_ISDIR(info2.st_mode)))
|
||||
&& !S_ISDIR(info.st_mode))
|
||||
return (put_error_cd(exec, "not a directory"));
|
||||
if ((i = access(exec, X_OK)) != 0)
|
||||
put_error_cd(exec, "permission denied");
|
||||
if (i != 0)
|
||||
return (NULL);
|
||||
return (put_error_cd(exec, "permission denied"));
|
||||
return (exec);
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
int cmd_cd_update_env(char *path, t_list **env, char opt)
|
||||
int cmd_cd_update_env(char *path, t_env **env, char opt)
|
||||
{
|
||||
t_envelem *pwd;
|
||||
char *oldpwd;
|
||||
char *c_path;
|
||||
char p_path[4096];
|
||||
t_env *pwd;
|
||||
char *oldpwd;
|
||||
char *c_path;
|
||||
char p_path[4096];
|
||||
|
||||
ft_bzero(p_path, 4096);
|
||||
if (getcwd(p_path, 4096) == NULL)
|
||||
if (!(pwd = ft_envgetelem("PWD", *env)))
|
||||
return (put_error_cd2(path, "env var PWD undefined"));
|
||||
if (getcwd(p_path, 4096) == NULL || (oldpwd = ft_strdup(pwd->val)) == NULL
|
||||
|| ft_realpath(path, &c_path) == NULL)
|
||||
return (put_error_cd2(path, "error"));
|
||||
if (ft_envupdate("PWD", *env, ((opt == 'P') ? p_path : c_path)) == NULL
|
||||
|| ft_envupdate("OLDPWD", *env, oldpwd) == NULL)
|
||||
{
|
||||
put_error_cd(path, "error");
|
||||
return (1);
|
||||
}
|
||||
if ((pwd = env_getelemfromkey("PWD", *env)) == NULL)
|
||||
{
|
||||
put_error_cd(path, "error");
|
||||
return (1);
|
||||
}
|
||||
if ((oldpwd = ft_strdup(pwd->val)) == NULL)
|
||||
{
|
||||
put_error_cd(path, "error");
|
||||
return (1);
|
||||
}
|
||||
if (ft_realpath(path, &c_path) == NULL)
|
||||
{
|
||||
put_error_cd(path, "error");
|
||||
return (1);
|
||||
}
|
||||
if (env_addupdate("PWD", ((opt == 'P') ? p_path : c_path), env) == NULL
|
||||
|| env_addupdate("OLDPWD", oldpwd, env) == NULL)
|
||||
{
|
||||
put_error_cd(path, "error");
|
||||
ft_strdel(&oldpwd);
|
||||
ft_strdel(&c_path);
|
||||
return (1);
|
||||
return (put_error_cd2(path, "error"));
|
||||
}
|
||||
ft_strdel(&oldpwd);
|
||||
ft_strdel(&c_path);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int cmd_cd_core(char *path, t_list **env, char opt)
|
||||
int cmd_cd_core(char *path, t_env **env, char opt)
|
||||
{
|
||||
t_envelem *pwd;
|
||||
t_env *pwd;
|
||||
|
||||
if (check_path_slash_cd(path) == NULL)
|
||||
{
|
||||
put_error_cd(path, "invalid path");
|
||||
return (1);
|
||||
}
|
||||
if (chdir(path) == -1
|
||||
|| (pwd = env_getelemfromkey("PWD", *env)) == NULL)
|
||||
|| (pwd = ft_envgetelem("PWD", *env)) == NULL)
|
||||
{
|
||||
put_error_cd(path, "error");
|
||||
return (1);
|
||||
@@ -133,35 +100,42 @@ char cd_getparams(char **argv, size_t *i)
|
||||
return (ret[0]);
|
||||
}
|
||||
|
||||
int cmd_cd(char **argv, t_list **env)
|
||||
int cmd_cd_switchboard(char **av, t_env **env, char opt, int i)
|
||||
{
|
||||
t_envelem *elem;
|
||||
size_t i;
|
||||
char *path;
|
||||
char opt;
|
||||
int ret;
|
||||
t_env *e;
|
||||
char *p;
|
||||
int ret;
|
||||
|
||||
i = 0;
|
||||
if ((opt = cd_getparams(argv, &i)) == '\0')
|
||||
return (1);
|
||||
if (!argv[i] && (elem = env_getelemfromkey("HOME", *env)) != NULL && elem->val != NULL)
|
||||
return (cmd_cd_core(elem->val, env, opt));
|
||||
else if (argv[i] && argv[i][0] == '/')
|
||||
return (cmd_cd_core(argv[i], env, opt));
|
||||
else if (argv[i] && argv[i][0] == '-' && (elem = env_getelemfromkey("OLDPWD", *env)) != NULL)
|
||||
return (cmd_cd_core(elem->val, env, opt));
|
||||
else if (argv[i] && argv[i][0] != '/' && (elem = env_getelemfromkey("PWD", *env)) != NULL && elem->val != NULL)
|
||||
if ((!av[i] && (e = ft_envgetelem("HOME", *env)) != NULL && e->val
|
||||
!= NULL) || (av[i] && av[i][0] == '-'
|
||||
&& (e = ft_envgetelem("OLDPWD", *env)) != NULL))
|
||||
return (cmd_cd_core(e->val, env, opt));
|
||||
else if (av[i] && av[i][0] == '/')
|
||||
return (cmd_cd_core(av[i], env, opt));
|
||||
else if (av[i] && av[i][0] != '/'
|
||||
&& (e = ft_envgetelem("PWD", *env)) != NULL && e->val != NULL)
|
||||
{
|
||||
if ((path = ft_strnew(ft_strlen(elem->val) + ft_strlen(argv[i]) + 1)) == NULL)
|
||||
put_error_cd(argv[i], "memory error");
|
||||
if (path == NULL)
|
||||
if ((p = ft_strnew(ft_strlen(e->val) + ft_strlen(av[i]) + 1)) == NULL)
|
||||
put_error_cd(av[i], "memory error");
|
||||
if (p == NULL)
|
||||
return (1);
|
||||
ft_strcpy(path, elem->val);
|
||||
path[ft_strlen(path)] = '/';
|
||||
ft_strcat(path, argv[i]);
|
||||
ret = cmd_cd_core(path, env, opt);
|
||||
ft_strdel(&path);
|
||||
ft_strcpy(p, e->val);
|
||||
p[ft_strlen(p)] = '/';
|
||||
ft_strcat(p, av[i]);
|
||||
ret = cmd_cd_core(p, env, opt);
|
||||
ft_strdel(&p);
|
||||
return (ret);
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
int cmd_cd(char **argv, t_env **env)
|
||||
{
|
||||
size_t i;
|
||||
char opt;
|
||||
|
||||
i = 0;
|
||||
if ((opt = cd_getparams(argv, &i)) == '\0')
|
||||
return (1);
|
||||
return (cmd_cd_switchboard(argv, env, opt, i));
|
||||
}
|
||||
|
Reference in New Issue
Block a user