Skip to content

Commit

Permalink
Use _Exit
Browse files Browse the repository at this point in the history
The shell used to call the exit function to terminate itself, but this
function has the overhead of examining the atexit functions and flushing
open streams. We now use the _Exit function to skip the redundant
operations.
  • Loading branch information
magicant committed Oct 20, 2024
1 parent e7823dd commit 7728c0b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -1630,7 +1630,7 @@ wchar_t *exec_command_substitution(const embedcmd_T *cmdsub)
xclose(pipefd[PIPE_IN]);
if (pipefd[PIPE_OUT] != STDOUT_FILENO) { /* connect the pipe */
if (xdup2(pipefd[PIPE_OUT], STDOUT_FILENO) < 0)
exit(Exit_NOEXEC);
_Exit(Exit_NOEXEC);
xclose(pipefd[PIPE_OUT]);
}

Expand Down Expand Up @@ -2299,7 +2299,7 @@ int exec_builtin_2(int argc, void **argv, const wchar_t *as, bool clear)
free(mbssaveargv0);
error1:
if (posixly_correct || !is_interactive_now)
exit(err);
_Exit(err);
return err;
}

Expand Down
6 changes: 3 additions & 3 deletions redir.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* Yash: yet another shell */
/* redir.c: manages file descriptors and provides functions for redirections */
/* (C) 2007-2022 magicant */
/* (C) 2007-2024 magicant */

/* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -864,14 +864,14 @@ int open_process_redirection(const embedcmd_T *command, redirtype_T type)
xclose(pipefd[PIPE_IN]);
if (pipefd[PIPE_OUT] != STDOUT_FILENO) {
if (xdup2(pipefd[PIPE_OUT], STDOUT_FILENO) < 0)
exit(Exit_NOEXEC);
_Exit(Exit_NOEXEC);
xclose(pipefd[PIPE_OUT]);
}
} else {
xclose(pipefd[PIPE_OUT]);
if (pipefd[PIPE_IN] != STDIN_FILENO) {
if (xdup2(pipefd[PIPE_IN], STDIN_FILENO) < 0)
exit(Exit_NOEXEC);
_Exit(Exit_NOEXEC);
xclose(pipefd[PIPE_IN]);
}
}
Expand Down
14 changes: 7 additions & 7 deletions yash.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* Yash: yet another shell */
/* yash.c: basic functions of the shell */
/* (C) 2007-2022 magicant */
/* (C) 2007-2024 magicant */

/* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -154,13 +154,13 @@ int main(int argc, char **argv)

int optresult = parse_shell_options(argc, wargv, &options);
if (optresult != Exit_SUCCESS)
exit(optresult);
_Exit(optresult);
if (options.version)
print_version();
if (options.help)
print_help();
if (options.version || options.help)
exit(yash_error_message_count == 0 ? Exit_SUCCESS : Exit_FAILURE);
_Exit(yash_error_message_count == 0 ? Exit_SUCCESS : Exit_FAILURE);

init_variables();

Expand All @@ -171,14 +171,14 @@ int main(int argc, char **argv)
const char *inputname;

if (shopt_cmdline && shopt_stdin)
exit(mutually_exclusive_option_error(L'c', L's'));
_Exit(mutually_exclusive_option_error(L'c', L's'));

if (shopt_cmdline) {
input.command = wargv[xoptind++];
if (input.command == NULL) {
xerror(0, Ngt("the -c option is specified "
"but no command is given"));
exit(Exit_ERROR);
_Exit(Exit_ERROR);
}
if (xoptind < argc) {
inputname = argv[xoptind];
Expand All @@ -204,7 +204,7 @@ int main(int argc, char **argv)
if (input.fd < 0) {
int errno_ = errno;
xerror(errno_, Ngt("cannot open file `%s'"), inputname);
exit(errno_ == ENOENT ? Exit_NOTFOUND : Exit_NOEXEC);
_Exit(errno_ == ENOENT ? Exit_NOTFOUND : Exit_NOEXEC);
}
}
}
Expand Down Expand Up @@ -389,7 +389,7 @@ void exit_shell_with_status(int status)
#if YASH_ENABLE_HISTORY
finalize_history();
#endif
exit(exitstatus);
_Exit(exitstatus);
}

/* Prints the help message to the standard output. */
Expand Down

0 comments on commit 7728c0b

Please sign in to comment.