aboutsummaryrefslogtreecommitdiff
path: root/src/eval/exec.c
blob: fb97bdc2aa6d405e83ce279b9f2d2b889ef4d9f7 (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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   exec.c                                             :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: charles <charles.cabergs@gmail.com>        +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/04/01 17:06:11 by charles           #+#    #+#             */
/*   Updated: 2020/07/18 09:39:53 by charles          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

/*
** \file   exec.c
** \brief  Executable name and path
*/

#include "eval.h"

/*
** \brief             Check if executable name is already a path
** \param exec_name   Executable name
** \return            True if valid
*/

/* bool	exec_is_path(char *exec_name) */
/* { */
/* 	return (ft_strncmp(exec_name, "../", 3) == 0 */
/* 			|| ft_strncmp(exec_name, "./", 2) == 0 */
/* 			|| ft_strncmp(exec_name, "/", 1) == 0); */
/* } */

/*
** \brief             Check if executable path is valid
** \param exec_path   Executable path
** \return            True if valid
*/

bool	exec_is_valid(char *exec_path)
{
	struct stat	statbuf;

	if (stat(exec_path, &statbuf) != 0)
		return (false);
	if (!S_ISREG(statbuf.st_mode)) // also need to manage link
		return (false);
	// could test permission but probably handled by execve
	return (true);
}

/*
** \brief              Search executable name in path
** \param path         Path hash table
** \param path_var     Path environment string in case we need to update path
** \param exec_name    Executable name to search
** \return             Executable path or NULL if not found or path update error
*/

char	*exec_search_path(t_path path, char *path_var, char *exec_name)
{
	char			*exec_path;

	if (ft_strchr(exec_name, '/') != NULL) // TODO test recursive link
		return (exec_name);
	// TODO if PATH contain empty path, consider current directory files as cmd
	if ((exec_path = ft_htget(path, exec_name)) == NULL)
	{
		if (path_update(path, path_var) == NULL) // optimise by not updating not changed path in ht
			return (NULL);   // FIXME need to distiguish between malloc error and cmd not found error
		if ((exec_path = ft_htget(path, exec_name)) == NULL)
			return (NULL);
	}
	return (exec_path);
}