summaryrefslogtreecommitdiffstats
path: root/src/u_path.c
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-09-15 19:59:41 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-09-15 19:59:41 +0200
commitca2e36781039eb7e9901ccde395600e7af87ff4f (patch)
tree8a9d85433e943fbbd079bc55700e048a54d9bf24 /src/u_path.c
parenttype builtin fix (diff)
download42-minishell-ca2e36781039eb7e9901ccde395600e7af87ff4f.tar.gz
42-minishell-ca2e36781039eb7e9901ccde395600e7af87ff4f.tar.bz2
42-minishell-ca2e36781039eb7e9901ccde395600e7af87ff4f.tar.xz
42-minishell-ca2e36781039eb7e9901ccde395600e7af87ff4f.tar.zst
42-minishell-ca2e36781039eb7e9901ccde395600e7af87ff4f.zip
Huge fixes and stack stuff
Diffstat (limited to '')
-rw-r--r--src/u_path.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/u_path.c b/src/u_path.c
new file mode 100644
index 0000000..b43946f
--- /dev/null
+++ b/src/u_path.c
@@ -0,0 +1,72 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* u_path.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 <libft.h>
+#include <stddef.h>
+#include <dirent.h>
+#include <limits.h>
+
+#include "s_struct.h"
+#include "u_vars.h"
+
+static void u_get_fullpath(char fullpath[],
+ const char p_path[],
+ const char d_name[],
+ size_t dstsize)
+{
+ const size_t path_len = ft_strlen(p_path);
+ const size_t name_len = ft_strlen(d_name);
+
+ fullpath[0] = C_NUL;
+ if (path_len + name_len < dstsize)
+ {
+ (void)ft_memcpy(fullpath, p_path, path_len);
+ *(fullpath + (path_len)) = '/';
+ ft_memcpy(fullpath + path_len + 1, d_name, name_len);
+ *(fullpath + (path_len + name_len + 1)) = '\0';
+ }
+}
+
+uint8_t u_search_in_path(char fullpath[],
+ const char com[],
+ size_t dstsize,
+ t_msh *msh)
+{
+ struct dirent *ent;
+ char tmp[ARG_MAX];
+ char *tok_path;
+ DIR *dir;
+
+ if (u_get_var_value(tmp, "$PATH", ARG_MAX, msh) != 0)
+ return (1);
+ tok_path = ft_strtok(tmp, ":");
+ while (tok_path != NULL)
+ {
+ if ((dir = opendir(tok_path)) != NULL)
+ {
+ while ((ent = readdir(dir)) != NULL)
+ {
+ if (ft_strncmp(com, ent->d_name, ft_strlen(com) + 1) == 0)
+ {
+ u_get_fullpath(fullpath, tok_path, ent->d_name, dstsize);
+ closedir(dir);
+ if (fullpath[0] == C_NUL)
+ return (1);
+ return (0);
+ }
+ }
+ closedir(dir);
+ }
+ tok_path = ft_strtok(NULL, ":");
+ }
+ return (1);
+}