saved changes before repartition

This commit is contained in:
2025-09-12 12:41:24 +02:00
parent 8158998fbb
commit 161ac6b69d
26 changed files with 528 additions and 150 deletions

View File

@@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* command_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/05 19:52:37 by sede-san #+# #+# */
/* Updated: 2025/08/20 22:15:22 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int is_absolutepath(
char const *cmd)
{
return (cmd[0] == '/');
}
/**
* @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.
*/
int is_local(
char const *cmd)
{
return (cmd[0] == '.' && cmd[1] == '/');
}

View File

@@ -6,15 +6,49 @@
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/01 16:24:47 by sede-san #+# #+# */
/* Updated: 2025/08/03 23:39:07 by sede-san ### ########.fr */
/* Updated: 2025/08/05 20:21:27 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
// int exec_cmd(
// t_cmd cmd,
// t_minishell *minishell)
// {
int exec_builtin(
t_cmd *cmd,
char **envp)
{
if (ft_strncmp(cmd->argv[0], "cd\0", 3) == 0)
return (cd_builtin(cmd->argc, (char const **)cmd->argv));
else if (ft_strncmp(cmd->argv[0], "echo\0", 5) == 0)
return (echo_builtin(cmd->argc, (char const **)cmd->argv));
// else if (ft_strncmp(cmd->argv[0], "env\0", 4) == 0)
// return (env_builtin(cmd->argc, (char const **)cmd->argv, envp));
// else if (ft_strncmp(cmd->argv[0], "exit\0", 5) == 0)
// return (exit_builtin(cmd->argc, (char const **)cmd->argv));
else if (ft_strncmp(cmd->argv[0], "export\0", 7) == 0)
return (export_builtin(cmd->argc, (char const **)cmd->argv, envp));
else if ((ft_strncmp(cmd->argv[0], "env\0", 4) == 0 && cmd->argc == 1)
|| ft_strncmp(cmd->argv[0], "printenv\0", 9) == 0)
return (printenv_builtin(
cmd->argc, (char const **)cmd->argv, (char const **)envp));
else if (ft_strncmp(cmd->argv[0], "pwd\0", 4) == 0)
return (pwd_builtin(cmd->argc, (char const **)cmd->argv));
else if (ft_strncmp(cmd->argv[0], "unset\0", 6) == 0)
return (unset_builtin(cmd->argc, (char const **)cmd->argv, envp));
return (1);
}
int exec_cmd(
t_cmd *cmd,
char **envp)
{
int exit_code;
pid_t pid;
exit_code = 0;
pid = fork();
if (pid == 0)
exit_code = execve(cmd->argv[0], cmd->argv, envp);
waitpid(pid, NULL, 0);
return (exit_code);
}
// }

View File

@@ -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);