Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions inc/invoke_cmd/execute_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* execute_cmd.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ttsubo <ttsubo@student.42.fr> +#+ +:+ +#+ */
/* By: dayano <dayano@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/21 21:39:18 by dayano #+# #+# */
/* Updated: 2025/05/04 13:15:50 by ttsubo ### ########.fr */
/* Updated: 2025/05/04 18:31:58 by dayano ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -15,6 +15,7 @@

# define PATH_PREFIX_LEN 5

# define INCORRECT_USAGE 2
# define CMD_FAILED_EXIT_STATUS 126
# define CMD_NOT_FOUND_EXIT_STATUS 127

Expand Down
5 changes: 3 additions & 2 deletions inc/invoke_cmd/invoke_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* invoke_command.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ttsubo <ttsubo@student.42.fr> +#+ +:+ +#+ */
/* By: dayano <dayano@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/20 21:21:13 by dayano #+# #+# */
/* Updated: 2025/05/04 12:37:03 by ttsubo ### ########.fr */
/* Updated: 2025/05/04 18:32:13 by dayano ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -24,6 +24,7 @@

int invoke_commands(t_cmd *cmd_head, t_minish *minish);
int exec_unit_builtin(t_cmd *cmd, t_minish *minish);
int execute_builtin(t_cmd *cmd, t_minish *minish);

// wait_pipeline.c
int wait_pipeline(t_cmd *cmd_head);
Expand Down
7 changes: 4 additions & 3 deletions lib/libft/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: ttsubo <ttsubo@student.42.fr> +#+ +:+ +#+ #
# By: dayano <dayano@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/11/06 10:03:51 by ttsubo #+# #+# #
# Updated: 2025/04/17 16:32:30 by ttsubo ### ########.fr #
# Updated: 2025/05/04 17:33:14 by dayano ### ########.fr #
# #
# **************************************************************************** #

Expand All @@ -33,7 +33,8 @@ SRCS = ft_atoi.c ft_isdigit.c ft_memmove.c ft_split.c ft_strlcpy.c
ft_max.c ft_min.c ft_abs.c ft_strnlen.c ft_strlen_until.c \
ft_lstnew.c ft_lstadd_front.c ft_lstsize.c ft_lstlast.c ft_lstadd_back.c \
ft_lstdelone.c ft_lstclear.c ft_lstiter.c ft_lstmap.c ft_strcmp.c \
ft_max.c ft_min.c ft_abs.c ft_strnlen.c ft_strlen_until.c ft_isspace.c
ft_max.c ft_min.c ft_abs.c ft_strnlen.c ft_strlen_until.c ft_isspace.c \
ft_strtoll.c ft_strtol.c

ifeq ($(MAKECMDGOALS), debug)
CC := gcc -Wall -Wextra -Werror -g
Expand Down
21 changes: 21 additions & 0 deletions lib/libft/ft_strtol.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtol.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dayano <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/07 20:43:11 by dayano #+# #+# */
/* Updated: 2025/05/04 17:29:50 by dayano ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"
#include <ctype.h>
#include <errno.h>
#include <limits.h>

long ft_strtol(const char *nptr, char **endptr, int base)
{
return ((long)ft_strtoll(nptr, endptr, base));
}
122 changes: 122 additions & 0 deletions lib/libft/ft_strtoll.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtoll.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dayano <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/09 21:02:46 by dayano #+# #+# */
/* Updated: 2025/05/04 17:30:03 by dayano ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"
#include <errno.h>
#include <limits.h>
#include <stddef.h>

static int parse_base_and_sign(const char **str, int base, int *sign)
{
*sign = 1;
if (**str == '-' || **str == '+')
{
if (**str == '-')
*sign = -1;
(*str)++;
}
if (base == 0)
{
if (**str == '0')
{
(*str)++;
if (**str == 'x' || **str == 'X')
{
(*str)++;
return (16);
}
return (8);
}
return (10);
}
if (base == 16 && **str == '0' && (*(*str + 1) == 'x' || *(*str
+ 1) == 'X'))
*str += 2;
return (base);
}

static int convert_digit(char c, int base)
{
const char *digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const char *p;

p = ft_strchr(digits, ft_toupper(c));
if (p && p - digits < base)
return (p - digits);
return (-1);
}

static void handle_overflow(const char **str, int base,
unsigned long long *result, int sign)
{
errno = ERANGE;
*result = (unsigned long long)LLONG_MAX + ((1 - sign) >> 1);
while (convert_digit(**str, base) >= 0)
(*str)++;
}

static long long process_digits(const char **str, int base, int sign,
char **endptr)
{
unsigned long long result;
unsigned long long cutoff;
int cutlim;
int digit;

result = 0;
cutoff = (unsigned long long)LLONG_MAX + ((1 - sign) >> 1);
cutlim = cutoff % base;
cutoff /= base;
while (1)
{
digit = convert_digit(**str, base);
if (digit < 0)
break ;
if (result > cutoff || (result == cutoff && digit > cutlim))
{
handle_overflow(str, base, &result, sign);
break ;
}
result = result * base + digit;
(*str)++;
}
if (endptr)
*endptr = (char *)*str;
return ((long long)sign * (long long)result);
}

long long ft_strtoll(const char *nptr, char **endptr, int base)
{
const char *str;
int sign;
long long result;

if (base < 0 || base == 1 || base > 36)
{
errno = EINVAL;
if (endptr)
*endptr = (char *)nptr;
return (0);
}
while (ft_isspace(*nptr))
nptr++;
str = nptr;
base = parse_base_and_sign(&str, base, &sign);
result = process_digits(&str, base, sign, endptr);
if (str == nptr)
{
if (endptr)
*endptr = (char *)nptr;
return (0);
}
return (result);
}
6 changes: 4 additions & 2 deletions lib/libft/libft.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ttsubo <ttsubo@student.42.fr> +#+ +:+ +#+ */
/* By: dayano <dayano@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/28 11:41:08 by ttsubo #+# #+# */
/* Updated: 2025/04/17 16:30:51 by ttsubo ### ########.fr */
/* Updated: 2025/05/04 17:34:18 by dayano ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -40,6 +40,8 @@ char *ft_strnstr(const char *big, const char *little,
size_t len);
char *ft_strrchr(const char *s, int c);
char *ft_strtrim(char const *s1, char const *set);
long ft_strtol(const char *nptr, char **endptr, int base);
long long ft_strtoll(const char *nptr, char **endptr, int base);
char *ft_substr(char const *s, unsigned int start, size_t len);
int ft_atoi(const char *str);
int ft_isalnum(int c);
Expand Down
19 changes: 16 additions & 3 deletions src/builtin/exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,31 @@
/* By: dayano <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/13 21:20:48 by dayano #+# #+# */
/* Updated: 2025/05/01 15:50:02 by dayano ### ########.fr */
/* Updated: 2025/05/04 18:34:59 by dayano ### ########.fr */
/* */
/* ************************************************************************** */

#include "main.h"

int builtin_exit(int argc, char *argv[], t_minish *minish)
{
long status;
char *endptr;

(void)minish;
if (argc != 1)
if (argc > 2)
return (error_mes(argv[0], "too many arguments"), EXIT_FAILURE);
if (printf("exit\n") < 0)
return (perror("printf"), EXIT_FAILURE);
exit(EXIT_SUCCESS);
if (argc == 1)
exit(EXIT_SUCCESS);
errno = 0;
status = ft_strtol(argv[1], &endptr, 10);
if (errno != 0 || *endptr != '\0')
{
ft_putstr_fd("minishell: exit: ", STDERR_FILENO);
error_mes(argv[1], "numeric argument required");
exit(INCORRECT_USAGE);
}
exit((unsigned int)status);
}
4 changes: 2 additions & 2 deletions src/invoke_cmd/exec_pipeline.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dayano <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/21 13:14:15 by dayano #+# #+# */
/* Updated: 2025/05/01 16:51:13 by dayano ### ########.fr */
/* Updated: 2025/05/04 18:14:21 by dayano ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -57,7 +57,7 @@ static pid_t _fork_command(t_cmd *cmd, t_cmd *cmd_head, t_pipe_io *pipefds,
if ((cmd->next != NULL) && is_redirect(cmd->next))
redirect(cmd->next);
if (is_builtin(cmd))
exit(exec_unit_builtin(cmd, minish));
exit(execute_builtin(cmd, minish));
execute_cmd(cmd, minish);
print_error(cmd->argv[0]);
exit(EXIT_FAILURE);
Expand Down
6 changes: 3 additions & 3 deletions src/invoke_cmd/execute_cmd_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
/* By: dayano <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/21 21:37:20 by dayano #+# #+# */
/* Updated: 2025/04/25 20:50:51 by dayano ### ########.fr */
/* Updated: 2025/05/04 17:04:58 by dayano ### ########.fr */
/* */
/* ************************************************************************** */

#include "main.h"

void print_cmd_not_found(t_cmd *cmd)
{
ft_putstr_fd("minish: ", STDERR_FILENO);
ft_putstr_fd(cmd->argv[0], STDERR_FILENO);
ft_putstr_fd(": command not found", STDERR_FILENO);
ft_putstr_fd(": command not found\n", STDERR_FILENO);
cmd->status = CMD_NOT_FOUND_EXIT_STATUS;
exit(CMD_NOT_FOUND_EXIT_STATUS);
}

void free_str_array(char **str)
Expand Down
8 changes: 4 additions & 4 deletions src/invoke_cmd/invoke_command.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* invoke_command.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ttsubo <ttsubo@student.42.fr> +#+ +:+ +#+ */
/* By: dayano <dayano@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/20 21:19:33 by dayano #+# #+# */
/* Updated: 2025/05/01 17:07:37 by ttsubo ### ########.fr */
/* Updated: 2025/05/04 18:11:39 by dayano ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -35,7 +35,7 @@ static bool is_unit_builtin(t_cmd *cmd_head)
return (false);
}

static int _execute_builtin(t_cmd *cmd, t_minish *minish)
int execute_builtin(t_cmd *cmd, t_minish *minish)
{
static const t_builtin builtin_funcs[] = {
builtin_echo, builtin_pwd, builtin_exit, builtin_cd,
Expand Down Expand Up @@ -83,7 +83,7 @@ int exec_unit_builtin(t_cmd *cmd, t_minish *minish)
}
if (cmd->next && is_redirect(cmd->next))
redirect(cmd->next);
return (_execute_builtin(builtin_cmd, minish));
return (execute_builtin(builtin_cmd, minish));
}

/**
Expand Down