summaryrefslogtreecommitdiffstats
path: root/src/s_set_cwd.c
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-12-18 21:04:29 +0100
committerJozanLeClerc <bousset.rudy@gmail.com>2020-12-18 21:04:29 +0100
commitf543cdc19dae32a70e7efae35f499008bacb2f6b (patch)
tree59ddbdfe412ba4d0f69d9b798cd9e70836d95919 /src/s_set_cwd.c
parentRemoved TODO.org (diff)
parentqweqweqweqwe (diff)
download42-minishell-f543cdc19dae32a70e7efae35f499008bacb2f6b.tar.gz
42-minishell-f543cdc19dae32a70e7efae35f499008bacb2f6b.tar.bz2
42-minishell-f543cdc19dae32a70e7efae35f499008bacb2f6b.tar.xz
42-minishell-f543cdc19dae32a70e7efae35f499008bacb2f6b.tar.zst
42-minishell-f543cdc19dae32a70e7efae35f499008bacb2f6b.zip
Merge branch 'master' of jozanleclerc.xyz:42-minishell
Diffstat (limited to 'src/s_set_cwd.c')
-rw-r--r--src/s_set_cwd.c75
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);
+ }
+}