-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from tacomea/tacomea/builtin
builtinコマンド `exit`を追加
- Loading branch information
Showing
9 changed files
with
76 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
int ft_isdigit(int c) | ||
{ | ||
return ('0' <= c && c <= '9'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
exit 0 | ||
exit 1 | ||
exit -1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters