Merge branch 'main' of github.com:mdev9/minishell
This commit is contained in:
124
exec.c
124
exec.c
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/07 14:12:49 by tomoron #+# #+# */
|
/* Created: 2024/02/07 14:12:49 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/02/13 16:23:01 by marde-vr ### ########.fr */
|
/* Updated: 2024/02/16 14:11:39 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -21,7 +21,7 @@ int exec_builtin(t_cmd *parsed_cmd, t_env *env)
|
|||||||
else if (!ft_strcmp(parsed_cmd->token, "env"))
|
else if (!ft_strcmp(parsed_cmd->token, "env"))
|
||||||
g_return_code = print_env(env);
|
g_return_code = print_env(env);
|
||||||
else if (!ft_strcmp(parsed_cmd->token, "exit"))
|
else if (!ft_strcmp(parsed_cmd->token, "exit"))
|
||||||
ft_exit(parsed_cmd, env);
|
exit_bt(parsed_cmd, env);
|
||||||
else if (!ft_strcmp(parsed_cmd->token, "pwd"))
|
else if (!ft_strcmp(parsed_cmd->token, "pwd"))
|
||||||
g_return_code = pwd();
|
g_return_code = pwd();
|
||||||
else
|
else
|
||||||
@ -29,9 +29,127 @@ int exec_builtin(t_cmd *parsed_cmd, t_env *env)
|
|||||||
return (STDOUT_FILENO);
|
return (STDOUT_FILENO);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ft_exit(t_cmd *parsed_cmd, t_env *env, int exit_code)
|
||||||
|
{
|
||||||
|
free_cmd(parsed_cmd);
|
||||||
|
free_env(env);
|
||||||
|
exit(exit_code);
|
||||||
|
}
|
||||||
|
|
||||||
|
int get_args_count(t_cmd *parsed_cmd)
|
||||||
|
{
|
||||||
|
int count;
|
||||||
|
t_cmd *cur_cmd;
|
||||||
|
|
||||||
|
count = 0;
|
||||||
|
cur_cmd = parsed_cmd;
|
||||||
|
while (cur_cmd->next != 0 && cur_cmd->type == ARG)
|
||||||
|
{
|
||||||
|
cur_cmd = cur_cmd->next;
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
if (cur_cmd->type == ARG)
|
||||||
|
count++;
|
||||||
|
return (count);
|
||||||
|
}
|
||||||
|
|
||||||
|
char **split_paths_from_env(t_env *env)
|
||||||
|
{
|
||||||
|
t_env *cur_env_var;
|
||||||
|
int path_in_envp;
|
||||||
|
|
||||||
|
path_in_envp = 0;
|
||||||
|
cur_env_var = env;
|
||||||
|
while (cur_env_var->next != 0)
|
||||||
|
{
|
||||||
|
if (!ft_strcmp(cur_env_var->name, "PATH"))
|
||||||
|
{
|
||||||
|
path_in_envp = 1;
|
||||||
|
break ;
|
||||||
|
}
|
||||||
|
cur_env_var = cur_env_var->next;
|
||||||
|
}
|
||||||
|
if (!path_in_envp)
|
||||||
|
{
|
||||||
|
// error: PATH not found
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
return (ft_split(cur_env_var->value, ':'));
|
||||||
|
}
|
||||||
|
|
||||||
|
void find_cmd_path(t_cmd *cmd, t_env *env, char **paths, int *found)
|
||||||
|
{
|
||||||
|
char *tmp;
|
||||||
|
char *path;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (paths[i])
|
||||||
|
{
|
||||||
|
path = paths[i];
|
||||||
|
tmp = ft_strjoin(path, "/");
|
||||||
|
if (!tmp)
|
||||||
|
ft_exit(cmd, env, 1);
|
||||||
|
path = ft_strjoin(tmp, cmd->token);
|
||||||
|
if (!path)
|
||||||
|
ft_exit(cmd, env, 1);
|
||||||
|
free(tmp);
|
||||||
|
if (access(path, X_OK) != -1)
|
||||||
|
{
|
||||||
|
*found = 1;
|
||||||
|
free(cmd->token);
|
||||||
|
cmd->token = path;
|
||||||
|
break ;
|
||||||
|
}
|
||||||
|
free(path);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void get_cmd_path(t_cmd *cmd, t_env *env)
|
||||||
|
{
|
||||||
|
char **paths;
|
||||||
|
int found;
|
||||||
|
|
||||||
|
found = 0;
|
||||||
|
if (access(cmd->token, X_OK != -1))
|
||||||
|
found = 1;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
paths = split_paths_from_env(env);
|
||||||
|
if (!paths)
|
||||||
|
ft_exit(cmd, env, 1);
|
||||||
|
find_cmd_path(cmd, env, paths, &found);
|
||||||
|
}
|
||||||
|
if (!found)
|
||||||
|
{
|
||||||
|
// error message: cmd->token command not found
|
||||||
|
free(cmd->token);
|
||||||
|
cmd->token = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void exec_command(t_cmd *parsed_cmd, t_env *env)
|
void exec_command(t_cmd *parsed_cmd, t_env *env)
|
||||||
{
|
{
|
||||||
|
t_cmd *cur_cmd;
|
||||||
|
int args_count;
|
||||||
|
char **cmd_args;
|
||||||
|
int i;
|
||||||
|
|
||||||
if (!parsed_cmd || exec_builtin(parsed_cmd, env))
|
if (!parsed_cmd || exec_builtin(parsed_cmd, env))
|
||||||
return ;
|
return ;
|
||||||
ft_printf("not a builtin\n");
|
cur_cmd = parsed_cmd;
|
||||||
|
args_count = get_args_count(parsed_cmd);
|
||||||
|
cmd_args = calloc(args_count, sizeof(char *));
|
||||||
|
if (!cmd_args)
|
||||||
|
ft_exit(parsed_cmd, env, 1);
|
||||||
|
i = 0;
|
||||||
|
while (i < args_count)
|
||||||
|
{
|
||||||
|
cmd_args[i] = cur_cmd->token;
|
||||||
|
cur_cmd = cur_cmd->next;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
get_cmd_path(parsed_cmd, env);
|
||||||
|
execve(parsed_cmd->token, cmd_args, env_to_char_tab(env));
|
||||||
}
|
}
|
||||||
|
4
exit.c
4
exit.c
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/07 16:04:11 by tomoron #+# #+# */
|
/* Created: 2024/02/07 16:04:11 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/02/14 12:44:03 by tomoron ### ########.fr */
|
/* Updated: 2024/02/16 13:07:01 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -19,7 +19,7 @@ void print_numeric_arg_err(char *arg)
|
|||||||
ft_putstr_fd(": numeric argument required\n", 2);
|
ft_putstr_fd(": numeric argument required\n", 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ft_exit(t_cmd *args, t_env *env)
|
void exit_bt(t_cmd *args, t_env *env)
|
||||||
{
|
{
|
||||||
t_cmd *start;
|
t_cmd *start;
|
||||||
int exit_code;
|
int exit_code;
|
||||||
|
@ -3,10 +3,10 @@
|
|||||||
# ::: :::::::: #
|
# ::: :::::::: #
|
||||||
# Makefile :+: :+: :+: #
|
# Makefile :+: :+: :+: #
|
||||||
# +:+ +:+ +:+ #
|
# +:+ +:+ +:+ #
|
||||||
# By: tomoron <marvin@42.fr> +#+ +:+ +#+ #
|
# By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2023/11/04 08:03:00 by tomoron #+# #+# #
|
# Created: 2023/11/04 08:03:00 by tomoron #+# #+# #
|
||||||
# Updated: 2023/11/15 14:42:45 by tomoron ### ########.fr #
|
# Updated: 2024/02/16 14:22:14 by marde-vr ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
@ -25,7 +25,9 @@ SRCS = ft_protected_atoi.c\
|
|||||||
ft_putchar.c\
|
ft_putchar.c\
|
||||||
ft_putstr.c\
|
ft_putstr.c\
|
||||||
ft_strlen.c\
|
ft_strlen.c\
|
||||||
ft_write_str_part.c
|
ft_write_str_part.c\
|
||||||
|
ft_printf_fd.c\
|
||||||
|
ft_printf_fd_utils.c
|
||||||
|
|
||||||
OBJS = $(SRCS:.c=.o)
|
OBJS = $(SRCS:.c=.o)
|
||||||
|
|
||||||
|
@ -3,10 +3,10 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_printf.h :+: :+: :+: */
|
/* ft_printf.h :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/11/02 23:42:00 by tomoron #+# #+# */
|
/* Created: 2023/11/02 23:42:00 by tomoron #+# #+# */
|
||||||
/* Updated: 2023/11/15 14:31:34 by tomoron ### ########.fr */
|
/* Updated: 2024/02/16 14:27:27 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -43,4 +43,16 @@ size_t ft_print_ptr(void *ptr, t_flags flags);
|
|||||||
size_t ft_print_str(char *s, t_flags flags);
|
size_t ft_print_str(char *s, t_flags flags);
|
||||||
void ft_put_lu_nbr(long unsigned int nb);
|
void ft_put_lu_nbr(long unsigned int nb);
|
||||||
|
|
||||||
|
//PRINTF_FD
|
||||||
|
|
||||||
|
int putchar_fd(int fd, const char c);
|
||||||
|
int putstr_fd(int fd, const char *s);
|
||||||
|
int putptr_fd(int fd, long unsigned int ptr);
|
||||||
|
int putnbr_fd(int fd, long nbr);
|
||||||
|
int puthexa_fd(int fd, unsigned int nbr, char c);
|
||||||
|
int puthexa_ptr_fd(int fd, long unsigned int ptr);
|
||||||
|
|
||||||
|
int ft_printf_fd(int fd, const char *s, ...);
|
||||||
|
int ft_print_arg(int fd, va_list args_lst, char c);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
81
libft/ft_printf/ft_printf_fd.c
Normal file
81
libft/ft_printf/ft_printf_fd.c
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_printf_fd.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/11/05 20:51:52 by marde-vr #+# #+# */
|
||||||
|
/* Updated: 2024/02/16 14:25:45 by marde-vr ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
#include "ft_printf.h"
|
||||||
|
|
||||||
|
int putchar_fd(int fd, char c)
|
||||||
|
{
|
||||||
|
write(fd, &c, 1);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_print_arg(int fd, va_list args_lst, char c)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
|
||||||
|
n = 0;
|
||||||
|
if (c == 'c')
|
||||||
|
n += putchar_fd(fd, va_arg(args_lst, int));
|
||||||
|
else if (c == 's')
|
||||||
|
n += putstr_fd(fd, va_arg(args_lst, char *));
|
||||||
|
else if (c == 'p')
|
||||||
|
n += putptr_fd(fd, va_arg(args_lst, long unsigned int));
|
||||||
|
else if (c == 'd' || c == 'i')
|
||||||
|
n += putnbr_fd(fd, va_arg(args_lst, int));
|
||||||
|
else if (c == 'u')
|
||||||
|
n += putnbr_fd(fd, va_arg(args_lst, unsigned int));
|
||||||
|
else if (c == 'x' || c == 'X')
|
||||||
|
n += puthexa_fd(fd, va_arg(args_lst, unsigned int), c);
|
||||||
|
else if (c == '\0')
|
||||||
|
return (-1);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
n += putchar_fd(fd, '%');
|
||||||
|
}
|
||||||
|
return (n);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_conversion_is_handled(char c)
|
||||||
|
{
|
||||||
|
if ((c == 'c' || c == 's' || c == 'p' || c == 'd' || c == 'i'
|
||||||
|
|| c == 'u' || c == 'x' || c == 'X' || c == '%'))
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_printf_fd(int fd, const char *s, ...)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
int i;
|
||||||
|
va_list args_lst;
|
||||||
|
|
||||||
|
n = 0;
|
||||||
|
i = 0;
|
||||||
|
va_start(args_lst, s);
|
||||||
|
while (s[i])
|
||||||
|
{
|
||||||
|
if (s[i] == '%' && ft_conversion_is_handled(s[i + 1]))
|
||||||
|
{
|
||||||
|
n += ft_print_arg(fd, args_lst, s[i + 1]);
|
||||||
|
if (s[i + 1] != '\0')
|
||||||
|
i += 2;
|
||||||
|
else
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
n += putchar_fd(fd, s[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
va_end(args_lst);
|
||||||
|
return (n);
|
||||||
|
}
|
90
libft/ft_printf/ft_printf_fd_utils.c
Normal file
90
libft/ft_printf/ft_printf_fd_utils.c
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_printf_fd_utils.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/11/06 21:06:24 by marde-vr #+# #+# */
|
||||||
|
/* Updated: 2024/02/16 14:28:23 by marde-vr ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "ft_printf.h"
|
||||||
|
|
||||||
|
int putstr_fd(int fd, const char *s)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
if (!s)
|
||||||
|
{
|
||||||
|
write(fd, &"(null)", 6);
|
||||||
|
return (6);
|
||||||
|
}
|
||||||
|
while (s[i])
|
||||||
|
{
|
||||||
|
write(fd, &s[i], 1);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
|
||||||
|
int putnbr_fd(int fd, long nb)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
|
||||||
|
n = 0;
|
||||||
|
if (nb < 0)
|
||||||
|
{
|
||||||
|
write(fd, "-", 1);
|
||||||
|
nb = nb * -1;
|
||||||
|
n++;
|
||||||
|
}
|
||||||
|
if (nb > 9)
|
||||||
|
{
|
||||||
|
n += putnbr_fd(fd, nb / 10);
|
||||||
|
}
|
||||||
|
write(fd, &(char){nb % 10 + 48}, 1);
|
||||||
|
return (n + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int puthexa_ptr_fd(int fd, unsigned long nbr)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
|
||||||
|
n = 0;
|
||||||
|
if (nbr > 15)
|
||||||
|
n += puthexa_ptr_fd(fd, nbr / 16);
|
||||||
|
write(fd, &"0123456789abcdef"[nbr % 16], 1);
|
||||||
|
return (n + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int putptr_fd(int fd, long unsigned int ptr)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
|
||||||
|
if (!ptr)
|
||||||
|
{
|
||||||
|
write(fd, &"(nil)", 5);
|
||||||
|
return (5);
|
||||||
|
}
|
||||||
|
putstr_fd(fd, "0x");
|
||||||
|
n = 2;
|
||||||
|
n += puthexa_ptr_fd(fd, ptr);
|
||||||
|
return (n);
|
||||||
|
}
|
||||||
|
|
||||||
|
int puthexa_fd(int fd, unsigned int nbr, char c)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
|
||||||
|
n = 0;
|
||||||
|
if (nbr > 15)
|
||||||
|
n += puthexa_fd(fd, nbr / 16, c);
|
||||||
|
if (c == 'X')
|
||||||
|
write(fd, &"0123456789ABCDEF"[nbr % 16], 1);
|
||||||
|
if (c == 'x')
|
||||||
|
write(fd, &"0123456789abcdef"[nbr % 16], 1);
|
||||||
|
return (n + 1);
|
||||||
|
}
|
Reference in New Issue
Block a user