diff options
Diffstat (limited to 'src/s_set_cwd.c')
-rw-r--r-- | src/s_set_cwd.c | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/s_set_cwd.c b/src/s_set_cwd.c new file mode 100644 index 0000000..38eb750 --- /dev/null +++ b/src/s_set_cwd.c @@ -0,0 +1,75 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* s_init_next.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <dirent.h> +#include <errno.h> +#include <libft.h> +#ifdef __linux__ +# include <linux/limits.h> +#else +# include <limits.h> +#endif +#include <paths.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "b_export_next.h" +#include "f_fail.h" +#include "s_destroy.h" +#include "s_struct.h" +#include "u_vars.h" +#include "u_vars_next.h" + +static void + s_set_cwd_next(char *cwd, char *fmt, t_msh *msh) +{ + if (getcwd(cwd, PATH_MAX) != NULL) + { + ft_sprintf(fmt, "%s=%s", "PWD", cwd); + b_export_with_equals(fmt, msh); + } + else + { + ft_dprintf(STDERR_FILENO, "minishell: %s\n", strerror(errno)); + } +} + +void + s_set_cwd(char cwd[], t_msh *msh) +{ + char fmt[PATH_MAX]; + DIR *dir; + + u_get_var_value(cwd, "$PWD", PATH_MAX, msh); + if (cwd[0] == C_NUL) + { + if (getcwd(cwd, PATH_MAX) != NULL) + { + ft_sprintf(fmt, "%s=%s", "PWD", cwd); + b_export_with_equals(fmt, msh); + } + else + { + ft_dprintf(STDERR_FILENO, "minishell: %s\n", strerror(errno)); + } + return ; + } + if ((dir = opendir(cwd)) != NULL) + { + closedir(dir); + } + else if (errno == ENOENT) + { + s_set_cwd_next(cwd, fmt, msh); + } +} |