saved changes before repartition
This commit is contained in:
@@ -6,14 +6,12 @@
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/08/01 16:40:35 by sede-san #+# #+# */
|
||||
/* Updated: 2025/08/04 00:28:53 by sede-san ### ########.fr */
|
||||
/* Updated: 2025/08/05 19:56:15 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
static int is_local(char const *cmd);
|
||||
static int is_builtin(char const *cmd);
|
||||
static char *resolve_cmd_path(char const *cmd, char const *path);
|
||||
|
||||
/**
|
||||
@@ -29,6 +27,8 @@ static char *resolve_cmd_path(char const *cmd, char const *path);
|
||||
* @return t_cmd The parsed command structure containing the command path,
|
||||
* argument vector, and argument count.
|
||||
*
|
||||
* @todo Manage double quote
|
||||
*
|
||||
* @note The string array cmd.argv must be freed after the command has been
|
||||
* executed.
|
||||
*/
|
||||
@@ -46,7 +46,8 @@ t_cmd parse_cmd(
|
||||
cmd.argc = 0;
|
||||
while (cmd.argv[cmd.argc])
|
||||
cmd.argc++;
|
||||
if (!is_local(cmd.argv[0]) && !is_builtin(cmd.argv[0]))
|
||||
if (!is_local(cmd.argv[0]) && !is_absolutepath(cmd.argv[0])
|
||||
&& !is_builtin(&cmd))
|
||||
{
|
||||
abs_path = resolve_cmd_path(cmd.argv[0], getenv("PATH"));
|
||||
if (abs_path)
|
||||
@@ -58,47 +59,6 @@ t_cmd parse_cmd(
|
||||
return (cmd);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Checks if the given command string refers to a local executable.
|
||||
*
|
||||
* This function determines whether the provided command string starts with
|
||||
* "./", which is a common convention for executing local files in Unix-like
|
||||
* systems.
|
||||
*
|
||||
* @param cmd The command string to check.
|
||||
*
|
||||
* @return 1 if the command starts with "./", 0 otherwise.
|
||||
*/
|
||||
static int is_local(
|
||||
char const *cmd)
|
||||
{
|
||||
return (cmd[0] == '.' && cmd[1] == '/');
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Checks if the given command is a shell builtin.
|
||||
*
|
||||
* This function compares the input command string against a list of known
|
||||
* shell builtin commands ("cd", "echo", "env", "exit", "export", "pwd",
|
||||
* "unset"). It returns a non-zero value if the command matches any of these
|
||||
* builtins, otherwise returns 0.
|
||||
*
|
||||
* @param cmd The command string to check.
|
||||
*
|
||||
* @return int Non-zero if the command is a builtin, 0 otherwise.
|
||||
*/
|
||||
static int is_builtin(
|
||||
char const *cmd)
|
||||
{
|
||||
return (!ft_strncmp(cmd, "cd\0", 3)
|
||||
|| !ft_strncmp(cmd, "echo\0", 5)
|
||||
|| !ft_strncmp(cmd, "env\0", 4)
|
||||
|| !ft_strncmp(cmd, "exit\0", 5)
|
||||
|| !ft_strncmp(cmd, "export\0", 7)
|
||||
|| !ft_strncmp(cmd, "pwd\0", 4)
|
||||
|| !ft_strncmp(cmd, "unset\0", 6));
|
||||
}
|
||||
|
||||
static char *resolve_cmd_path(char const *cmd, char const *path)
|
||||
{
|
||||
char *abs_path;
|
||||
@@ -111,8 +71,10 @@ static char *resolve_cmd_path(char const *cmd, char const *path)
|
||||
{
|
||||
abs_path = ft_strjoin_mul(
|
||||
3, (char *)path_splitted[i], "/", (char *)cmd);
|
||||
if (access(abs_path, F_OK) != -1)
|
||||
if (access(abs_path, X_OK) != -1)
|
||||
break ;
|
||||
free(abs_path);
|
||||
abs_path = NULL;
|
||||
i++;
|
||||
}
|
||||
ft_free_split((char **)path_splitted);
|
||||
|
||||
Reference in New Issue
Block a user