aboutsummaryrefslogtreecommitdiff
path: root/src/path.c
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-09-15 13:38:22 +0200
committerCharles Cabergs <me@cacharle.xyz>2020-09-15 13:38:22 +0200
commit170d0b74fa725196bca0fa549520d0d8bfa07576 (patch)
treeff4a689541a9f08616d169ba68c9ac8bed523c19 /src/path.c
parenta2a51b77a5534978f30f2788f7ed9cbe4fda3089 (diff)
downloadminishell-170d0b74fa725196bca0fa549520d0d8bfa07576.tar.gz
minishell-170d0b74fa725196bca0fa549520d0d8bfa07576.tar.bz2
minishell-170d0b74fa725196bca0fa549520d0d8bfa07576.zip
Fixing most of the bugs in path
Diffstat (limited to 'src/path.c')
-rw-r--r--src/path.c48
1 files changed, 36 insertions, 12 deletions
diff --git a/src/path.c b/src/path.c
index 442389b..4e912ba 100644
--- a/src/path.c
+++ b/src/path.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/27 15:51:01 by cacharle #+# #+# */
-/* Updated: 2020/09/14 20:44:39 by charles ### ########.fr */
+/* Updated: 2020/09/15 13:36:34 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -17,6 +17,31 @@
#include "minishell.h"
+int path_check(char exec_path[PATH_MAX + 1], bool in_path)
+{
+ struct stat statbuf;
+ /* char cwd[PATH_MAX + 1]; */
+
+ if (stat(exec_path, &statbuf) == -1)
+ return (errorf_ret(127, "%s: %s\n", exec_path, strerror(errno)));
+ if (S_ISDIR(statbuf.st_mode))
+ return (errorf_ret(126, "%s: Is a directory\n", exec_path));
+ /* if (S_ISLNK(statbuf.st_mode)) */
+ /* return (errorf_ret(127, "%s: command not found\n", exec_path)); */
+ if (!in_path && !(statbuf.st_mode & 0444))
+ return (errorf_ret(126, "%s: %s\n", exec_path, strerror(EACCES)));
+ /* if (*exec_path != '/') */
+ /* { */
+ /* getcwd(cwd, PATH_MAX + 1); */
+ /* ft_strcat(cwd, "/"); */
+ /* ft_strcat(cwd, exec_path); */
+ /* ft_strcpy(exec_path, cwd); */
+ /* } */
+ return (0);
+
+
+}
+
static bool st_dir_search(char *dirname, char *exec_name, char exec_path[PATH_MAX + 1])
{
DIR *dir;
@@ -38,7 +63,7 @@ static bool st_dir_search(char *dirname, char *exec_name, char exec_path[PATH_M
return (false);
}
-bool path_search(t_env env, char *exec_name, char exec_path[PATH_MAX + 1])
+int path_search(t_env env, char *exec_name, char exec_path[PATH_MAX + 1], bool print)
{
char *current_dir;
char *collon;
@@ -47,29 +72,28 @@ bool path_search(t_env env, char *exec_name, char exec_path[PATH_MAX + 1])
if (ft_strchr(exec_name, '/') != NULL)
{
ft_strcpy(exec_path, exec_name);
- return (true);
+ return (path_check(exec_path, false));
}
- /* printf("==========\n"); */
if ((current_dir = env_search(env, "PATH")) == NULL)
return (st_dir_search(getcwd(cwd, PATH_MAX + 1), exec_name, exec_path));
- /* printf("%s\n", current_dir); */
while ((collon = ft_strchr(current_dir, ':')) != NULL)
{
*collon = '\0';
- /* printf("%s\n", current_dir); */
if (*current_dir == '\0')
current_dir = getcwd(cwd, PATH_MAX + 1);
if (st_dir_search(current_dir, exec_name, exec_path))
- return (true);
+ {
+ *collon = ':';
+ return (path_check(exec_path, true));
+ }
*collon = ':';
current_dir = collon + 1;
}
- /* printf("> %s\n", current_dir); */
-
if (*current_dir == '\0')
current_dir = getcwd(cwd, PATH_MAX + 1);
if (st_dir_search(current_dir, exec_name, exec_path))
- return (true);
- /* printf("yo\n"); */
- return (false);
+ return (path_check(exec_path, true));
+ if (print)
+ errorf("%s: command not found\n", exec_name);
+ return (127);
}