From 3d66fbe9a9b09bbbb1c5c9c6d2e19cc965196f14 Mon Sep 17 00:00:00 2001 From: Etienne Rey-bethbeder Date: Fri, 24 Feb 2023 12:46:02 +0100 Subject: [PATCH] =?UTF-8?q?NIQUE=20TA=20GROSSE=20M=C3=88RE=20LA=20SALOPE?= =?UTF-8?q?=20PUTE=20PUTE=20-Camille=20Chauvet=202004/2023?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 4 ++-- builtins/exit.c | 46 +++++++++++++++++++++++++++++++++++++++ builtins/export.c | 4 ++-- minishell.h | 10 +++++---- utils/ft_atoi_check.c | 50 +++++++++++++++++++++++++++++++++++++++++++ utils/utils.h | 1 + 6 files changed, 107 insertions(+), 8 deletions(-) create mode 100644 builtins/exit.c create mode 100644 utils/ft_atoi_check.c diff --git a/Makefile b/Makefile index 7b48749..2456888 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ -UTILS_SRC = utils/ft_is_in_quote.c utils/ft_strncpy.c utils/ft_strreplace.c utils/ft_strnchr.c utils/ft_split_quoted.c utils/ft_strshift.c utils/ft_quote_remover.c utils/ft_str_is_empty.c -SRCS = ${UTILS_SRC} main.c file.c infile.c outfile.c heredoc.c syntatics.c cmd.c cmds.c env.c env2.c env3.c execution.c spacer.c env_fill.c builtins/echo.c builtins/pwd.c builtins/export.c builtins/env.c builtins/cd.c +UTILS_SRC = utils/ft_is_in_quote.c utils/ft_strncpy.c utils/ft_strreplace.c utils/ft_strnchr.c utils/ft_split_quoted.c utils/ft_strshift.c utils/ft_quote_remover.c utils/ft_str_is_empty.c utils/ft_atoi_check.c +SRCS = ${UTILS_SRC} main.c file.c infile.c outfile.c heredoc.c syntatics.c cmd.c cmds.c env.c env2.c env3.c execution.c spacer.c env_fill.c builtins/echo.c builtins/pwd.c builtins/export.c builtins/env.c builtins/cd.c builtins/exit.c OBJS = ${SRCS:.c=.o} diff --git a/builtins/exit.c b/builtins/exit.c new file mode 100644 index 0000000..688f3a6 --- /dev/null +++ b/builtins/exit.c @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/24 10:17:59 by erey-bet #+# #+# */ +/* Updated: 2023/02/24 12:44:05 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../minishell.h" + +static int error(int err, char *reason, char *problem, int fd) +{ + write(fd, "bash: exit: ", 12); + if (problem != NULL) + { + ft_putstr_fd(problem, fd); + write(fd, ": ", 2); + } + ft_putstr_fd(reason, fd); + write(fd, "\n", 1); + return (err); +} + +int ft_exit(char **args, int fd) +{ + int i; + + if (args[0] == NULL) + return (0); + i = -1; + if (ft_atoi_check(args[0]) == 0) + return (error(2, "numeric argument required", args[0], fd)); + if (args[1] != NULL) + return (error(-1, "too many arguments", NULL, fd)); + return (0); +} + +int main(int argc, char *argv[]) +{ + (void)argc; + return(ft_exit(argv + 1, 1)); +} diff --git a/builtins/export.c b/builtins/export.c index fa00097..92d6520 100644 --- a/builtins/export.c +++ b/builtins/export.c @@ -6,13 +6,13 @@ /* By: cchauvet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/07/21 08:21:05 by erey-bet #+# #+# */ +/* Updated: 2022/12/08 16:37:19 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "utils.h" + +static int ft_isspace(char c) +{ + if (c == ' ' || c == '\f' + ||c == '\n' || c == '\r' || c == '\t' || c == '\v') + return (1); + return (0); +} + +int ft_atoi_check(const char *nptr) +{ + long result; + int sign; + + while (ft_isspace(*nptr)) + nptr++; + sign = 1; + if (*nptr == '+' || *nptr == '-') + { + if (*nptr == '-') + sign = -1; + nptr++; + } + result = 0; + while (*nptr >= '0' && *nptr <= '9') + { + result = result * 10 + *nptr++ - '0'; + if ((result > 2147483647 && sign == 1) + || (result > 2147483648 && sign == -1)) + return (0); + } + if (*nptr--) + return (0); + if (*nptr == '-' || *nptr == '+') + return (0); + return (1); +} diff --git a/utils/utils.h b/utils/utils.h index 6481706..2a12d81 100644 --- a/utils/utils.h +++ b/utils/utils.h @@ -25,5 +25,6 @@ int ft_str_is_empty(const char *str); char **ft_split_quoted(const char *s, char c); void ft_strshift(char *str, int shift); char *ft_quote_remover(char *str); +int ft_atoi_check(const char *nptr); #endif