Skip to content

[FIX] Print exit msg to stderr#270

Merged
LeaYeh merged 2 commits intomainfrom
fix-exit-stderr
Mar 22, 2024
Merged

[FIX] Print exit msg to stderr#270
LeaYeh merged 2 commits intomainfrom
fix-exit-stderr

Conversation

@itislu
Copy link
Collaborator

@itislu itislu commented Mar 21, 2024

  • Also call the exit builtin when receiving EOF - bash does it the same way.

I tested it in bash like this and the exit message gets output to the file:

bash-5.1$ exit 2>/file

I also looked at the source code of bash and found the following lines that confirm that the exit builtin prints to stderr:

int
exit_builtin (list)
     WORD_LIST *list;
{
  CHECK_HELPOPT (list);

  if (interactive)
    {
      fprintf (stderr, login_shell ? _("logout\n") : "exit\n");
      fflush (stderr);
    }

  return (exit_or_logout (list));
}

https://github.com/bminor/bash/blob/f3b6bd19457e260b65d11f2712ec3da56cef463f/builtins/exit.def#L64

And when receiving EOF via Ctrl+D bash just calls the exit builtin:

/* If we have EOF as the only input unit, this user wants to leave
   the shell.  If the shell is not interactive, then just leave.
   Otherwise, if ignoreeof is set, and we haven't done this the
   required number of times in a row, print a message. */
static void
handle_eof_input_unit ()
{
  if (interactive)
    {
      /* shell.c may use this to decide whether or not to write out the
	 history, among other things.  We use it only for error reporting
	 in this file. */
      if (EOF_Reached)
	EOF_Reached = 0;

      /* If the user wants to "ignore" eof, then let her do so, kind of. */
      if (ignoreeof)
	{
	  if (eof_encountered < eof_encountered_limit)
	    {
	      fprintf (stderr, _("Use \"%s\" to leave the shell.\n"),
		       login_shell ? "logout" : "exit");
	      eof_encountered++;
	      /* Reset the parsing state. */
	      last_read_token = current_token = '\n';
	      /* Reset the prompt string to be $PS1. */
	      prompt_string_pointer = (char **)NULL;
	      prompt_again (0);
	      return;
	    }
	}

      /* In this case EOF should exit the shell.  Do it now. */
      reset_parser ();

      last_shell_builtin = this_shell_builtin;
      this_shell_builtin = exit_builtin;
      exit_builtin ((WORD_LIST *)NULL);
    }
  else
    {
      /* We don't write history files, etc., for non-interactive shells. */
      EOF_Reached = 1;
    }
}

https://github.com/bminor/bash/blob/f3b6bd19457e260b65d11f2712ec3da56cef463f/y.tab.c#L8689

@itislu itislu added the bug Something isn't working label Mar 21, 2024
@itislu itislu added this to the Builtins milestone Mar 21, 2024
@itislu itislu linked an issue Mar 21, 2024 that may be closed by this pull request
@LeaYeh LeaYeh merged commit ec17929 into main Mar 22, 2024
@LeaYeh LeaYeh deleted the fix-exit-stderr branch March 22, 2024 07:57
itislu added a commit to LeaYeh/42_minishell_tester that referenced this pull request Jul 23, 2024
The exit message should always get printed to stderr, bash does it too (see `exit 2>/dev/null`).
For more information, see LeaYeh/minishell#270
itislu added a commit to LeaYeh/42_minishell_tester that referenced this pull request Jul 23, 2024
The exit message should always get printed to stderr, bash does it too (see `exit 2>/dev/null`).
For more information, see LeaYeh/minishell#270
itislu added a commit to LeaYeh/42_minishell_tester that referenced this pull request Jul 23, 2024
The exit message should always get printed to stderr, bash does it too (see `exit 2>/dev/null`).
For more information, see LeaYeh/minishell#270
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[BUG] exit message should get printed to STDERR

2 participants