Skip to content

Commit

Permalink
Merge pull request #72 from tacomea/tacomea/builtin
Browse files Browse the repository at this point in the history
builtinコマンド `exit`を追加
  • Loading branch information
takumihara authored Oct 11, 2021
2 parents 632e39f + 79dd4f3 commit 48ba711
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ BUILTIN_PATH = builtin
SRC_PATHS = $(CURRENT_PATH) $(EXPANDER_PATH) $(BUILTIN_PATH) $(EXECUTE_PATH) $(PARSER_PATH) $(LEXER_PATH) $(AST_PATH) $(UTILS_PATH)
SRCS = $(foreach path, $(SRC_PATHS), $(wildcard $(path)/*.c))

VPATH = $(CURRENT_PATH):$(BUILTIN_PATH):$(EXECUTE_PATH):$(PARSER_PATH):$(LEXER_PATH):$(AST_PATH):$(UTILS_PATH)
VPATH = $(CURRENT_PATH):$(BUILTIN_PATH):$(EXECUTE_PATH):$(PARSER_PATH):$(LEXER_PATH):$(AST_PATH):$(UTILS_PATH):$(EXPANDER_PATH)

OBJDIR = ./obj
OBJS = $(addprefix $(OBJDIR)/, $(notdir $(SRCS:.c=.o)))
Expand Down
4 changes: 4 additions & 0 deletions builtin/builtin.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

#include "../libft/libft.h"
#include "../utils/utils.h"
#include "../execute/execute.h"

int cd(int argc, char **argv);
int pwd();
int builtin_exit(int argc, char **argv, int last_exit_status);

#endif //BUILTIN_H
23 changes: 23 additions & 0 deletions builtin/builtin_exit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "builtin.h"

int builtin_exit(int argc, char **argv, int last_exit_status)
{
int num;

ft_putendl_fd("exit", STDERR_FILENO);
if (argc == 1)
exit(last_exit_status);
if (argc == 2)
{
if (!atoi_strict(argv[1], &num))
{
ft_putendl_fd("minishell: exit: ", STDERR_FILENO);
ft_putendl_fd(argv[1], STDERR_FILENO);
ft_putendl_fd(": numeric argument required", STDERR_FILENO);
return (EXIT_FAILURE);
}
exit((unsigned char)num); //todo: free?
}
ft_putendl_fd("minishell: exit: too many arguments", STDERR_FILENO);
return (EXIT_FAILURE);
}
8 changes: 8 additions & 0 deletions execute/execute_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ bool execute_builtin(t_executor *e, int argc, char **argv, bool islast)
pwd(argc, argv);
return (true);
}
else if (!ft_strcmp(argv[0], "exit"))
{
if (islast)
e->exit_status = builtin_exit(argc, argv, e->exit_status);
else
builtin_exit(argc, argv, e->exit_status);
return (true);
}
return (false);
}

Expand Down
4 changes: 4 additions & 0 deletions libft/ft_isdigit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
int ft_isdigit(int c)
{
return ('0' <= c && c <= '9');
}
10 changes: 3 additions & 7 deletions minishell.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,9 @@ int minishell(char *line)
while (1)
{
line = readline("minishell> ");
// line can be NULL when Ctrl+d
if (!line || ft_strncmp(line, "exit", 4) == 0)
{
free(line);
ft_putendl_fd("exit", STDERR_FILENO);
if (!line)
break ;
}
// line can be NULL when Ctrl+d
t_token *token = lex(line);
t_ast_node *node = parse(token);
if (!node)
Expand All @@ -63,7 +59,7 @@ int minishell(char *line)
add_history(line);
free(line);
}
return (0);
return (EXIT_SUCCESS);
}

int main(int argc, char **argv)
Expand Down
3 changes: 3 additions & 0 deletions test/cases/exit.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exit 0
exit 1
exit -1
29 changes: 29 additions & 0 deletions utils/atoi_strict.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "utils.h"

bool atoi_strict(const char *str, int *num)
{
long long converted;
int sign;

converted = 0;
sign = 1;
while (('\t' <= *str && *str <= '\r') || *str == ' ')
str++;
if ((*str == '+' || *str == '-') && *str++ == '-')
sign = -1;
if (*str == '\0')
return (false);
while (ft_isdigit(*str))
{
if ((converted * 10 + *str - '0') / 10 != converted)
return (false);
converted = converted * 10 + *str - '0';
str++;
}
if (*str == '\0')
{
*num = (int)(sign * converted);
return (true);
}
return (false);
}
1 change: 1 addition & 0 deletions utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ char *strappend(char *dst, const char *src, size_t l);
char *ft_strndup(const char *str, size_t size);
void free_2d_array(void ***array);
char **split_by_delims(char const *str, const char *delims);
bool atoi_strict(const char *str, int *num);

# endif //UTILS_H

0 comments on commit 48ba711

Please sign in to comment.