aboutsummaryrefslogtreecommitdiff
path: root/src/get_cmd_path.c
blob: 713c397bc6b732f2376763ebcb083e0cbe3c3ffa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   get_cmd_path.c                                     :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2024/12/17 19:19:59 by chuhlig           #+#    #+#             */
/*   Updated: 2025/01/22 00:01:48 by chuhlig          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "libft.h"
#include "minishell.h"
#include <errno.h>
#include <sys/errno.h>
#include <sys/stat.h>
#include <sys/unistd.h>
#include <unistd.h>

static char	*get_simple_cmd_path(char *cmd, int *return_code);
static char	*get_absolute_cmd_path(char *cmd, t_env *env, int *return_code);
static char	*find_in_path(char *cmd, t_env *env, int *return_code);
char		**get_split_path(t_env *env);
static int	is_directory(char *path);

char	*get_cmd_path(char *cmd, t_env *env, int *return_code)
{
	if (cmd[0] == '/')
		return (get_simple_cmd_path(cmd, return_code));
	else if (ft_strchr(cmd, '/'))
		return (get_absolute_cmd_path(cmd, env, return_code));
	else
		return (find_in_path(cmd, env, return_code));
}

static char	*get_absolute_cmd_path(char *cmd, t_env *env, int *return_code)
{
	char	*cur_dir;
	char	*result;

	cur_dir = ft_strjoin(env_get(env, "PWD"), "/");
	if (!cur_dir)
		return (NULL);
	result = ft_strjoin(cur_dir, cmd);
	free(cur_dir);
	if (!result)
		return (error(ENOENT, cmd, 127, return_code));
	if (access(result, F_OK) == -1)
	{
		free(result);
		return (error(ENOENT, cmd, 127, return_code));
	}
	if (access(result, X_OK) == -1)
	{
		free(result);
		return (error(EACCES, cmd, 126, return_code));
	}
	if (is_directory(cmd))
		return (error(EISDIR, cmd, 126, return_code));
	return (result);
}

void free_split_path(char **path)
{
    char **tmp = path;
    while (*tmp)
    {
        free(*tmp);
        tmp++;
    }
    free(path);
}

char **get_split_path(t_env *env)
{
    char *path_env;
    char **split_path;

    path_env = env_get(env, "PATH");
    if (!path_env)
        return (NULL);
    split_path = ft_split(path_env, ':');
    free(path_env);
    return (split_path);
}

static char	*find_in_path(char *cmd, t_env *env, int *return_code)
{
    char	*cur_path;
    char	*cmd_path;
    char	**path;
    char	**path_start; // To keep track of the start of the path array

    path = get_split_path(env);
    path_start = path; // Save the start of the path array
    cmd_path = NULL;
    while (*path)
    {
        if (cmd_path)
            free(cmd_path);
        cur_path = ft_strjoin(*path, "/");
        if (!cur_path)
        {
            free_split_path(path_start); // Free the entire path array
            return (NULL);
        }
        cmd_path = ft_strjoin(cur_path, cmd);
        free(cur_path);
        if (!cmd_path)
        {
            free_split_path(path_start); // Free the entire path array
            return (NULL);
        }
        if (access(cmd_path, X_OK) != -1)
        {
            free_split_path(path_start); // Free the entire path array
            return (cmd_path);
        }
        path++;
    }
    free_split_path(path_start); // Free the entire path array
    *return_code = 127;
    command_not_found_error(cmd);
    return (NULL);
}

static char	*get_simple_cmd_path(char *cmd, int *return_code)
{
	char	*result;

	result = ft_strdup(cmd);
	if (!result)
		return (NULL);
	if (access(result, F_OK) == -1)
	{
		free(result);
		return (error(ENOENT, cmd, 127, return_code));
	}
	if (access(result, X_OK) == -1)
	{
		free(result);
		return (error(EACCES, cmd, 126, return_code));
	}
	if (is_directory(cmd))
	{
		free(result);
		return (error(EISDIR, cmd, 126, return_code));
	}
	return (result);
}

static int	is_directory(char *path)
{
	struct stat	path_stat;

	stat(path, &path_stat);
	if ((path_stat.st_mode & S_IFMT) == S_IFDIR)
		return (1);
	else
		return (0);
}