Skip to content
This repository has been archived by the owner on Nov 30, 2024. It is now read-only.

Commit

Permalink
fix exit codes to match bash on 42 systems
Browse files Browse the repository at this point in the history
  • Loading branch information
mcombeau committed Nov 11, 2022
1 parent e566005 commit 488286c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
21 changes: 16 additions & 5 deletions sources/builtins/cd_builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: mcombeau <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/17 19:03:08 by mcombeau #+# #+# */
/* Updated: 2022/11/05 11:38:53 by mcombeau ### ########.fr */
/* Updated: 2022/11/11 14:22:23 by mcombeau ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -34,6 +34,20 @@ static void update_wds(t_data *data, char *wd)
free_ptr(wd);
}

/* chdir_errno_mod:
* chdir can sometimes set errno to ESTALE ("Stale file handle")
* when a parent directory is removed on some systems. This is due
* to the inode table entry being recycled. This is a fix to display
* "no such file or directory" error instead.
*/
static bool chdir_errno_mod(char *path)
{
if (errno == ESTALE)
errno = ENOENT;
errmsg_cmd("cd", path, strerror(errno), errno);
return (false);
}

/* change_dir:
* Changes the current working directory and updates the
* OLDPWD environment variable.
Expand All @@ -47,10 +61,7 @@ static bool change_dir(t_data *data, char *path)

ret = NULL;
if (chdir(path) != 0)
{
errmsg_cmd("cd", path, strerror(errno), errno);
return (false);
}
return (chdir_errno_mod(path));
ret = getcwd(cwd, PATH_MAX);
if (!ret)
{
Expand Down
6 changes: 3 additions & 3 deletions sources/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: mcombeau <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/17 17:08:47 by mcombeau #+# #+# */
/* Updated: 2022/11/07 17:17:18 by mcombeau ### ########.fr */
/* Updated: 2022/11/11 13:57:52 by mcombeau ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -49,7 +49,7 @@ void minishell_interactive(t_data *data)
if (parse_user_input(data) == true)
g_last_exit_code = execute(data);
else
g_last_exit_code = 2;
g_last_exit_code = 1;
free_data(data, false);
}
}
Expand Down Expand Up @@ -80,7 +80,7 @@ void minishell_noninteractive(t_data *data, char *arg)
if (parse_user_input(data) == true)
g_last_exit_code = execute(data);
else
g_last_exit_code = 2;
g_last_exit_code = 1;
i++;
free_data(data, false);
}
Expand Down

0 comments on commit 488286c

Please sign in to comment.