introduce and use close_on_exec_on(fd). -50 bytes.
diff --git a/shell/ash.c b/shell/ash.c
index 183911c..9d8b83c 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -3469,7 +3469,7 @@
close(ofd);
if (fd < 0)
goto out;
- fcntl(fd, F_SETFD, FD_CLOEXEC);
+ close_on_exec_on(fd);
do { /* while we are in the background */
pgrp = tcgetpgrp(fd);
if (pgrp < 0) {
@@ -8830,7 +8830,7 @@
static void
setinputfd(int fd, int push)
{
- fcntl(fd, F_SETFD, FD_CLOEXEC);
+ close_on_exec_on(fd);
if (push) {
pushfile();
parsefile->buf = 0;
diff --git a/shell/hush.c b/shell/hush.c
index 5f9f2c5..9c70556 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -760,9 +760,11 @@
static int builtin_cd(char **argv)
{
const char *newdir;
- if (argv[1] == NULL)
+ if (argv[1] == NULL) {
+ // bash does nothing (exitcode 0) if HOME is ""; if it's unset,
+ // bash says "bash: cd: HOME not set" and does nothing (exitcode 1)
newdir = getenv("HOME") ? : "/";
- else
+ } else
newdir = argv[1];
if (chdir(newdir)) {
printf("cd: %s: %s\n", newdir, strerror(errno));
@@ -3629,7 +3631,7 @@
saved_task_pgrp = shell_pgrp = getpgrp();
debug_printf_jobs("saved_task_pgrp=%d\n", saved_task_pgrp);
- fcntl(interactive_fd, F_SETFD, FD_CLOEXEC);
+ close_on_exec_on(interactive_fd);
/* If we were ran as 'hush &',
* sleep until we are in the foreground. */
diff --git a/shell/lash.c b/shell/lash.c
index 5ba490f..af3a8f8 100644
--- a/shell/lash.c
+++ b/shell/lash.c
@@ -576,7 +576,7 @@
if (openfd != redir->fd) {
if (squirrel && redir->fd < 3) {
squirrel[redir->fd] = dup(redir->fd);
- fcntl(squirrel[redir->fd], F_SETFD, FD_CLOEXEC);
+ close_on_exec_on(squirrel[redir->fd]);
}
dup2(openfd, redir->fd);
close(openfd);