[PATCH] Prevent wordexp from modyfying terminal parameters

Moulding, Dan - 0663 - MITLL dmoulding@gmail.com
Thu Jul 22 22:29:00 GMT 2010

I have found that calling wordexp can, as an unfortunate side effect, change
the controlling terminal's parameters. I tracked the problem down to the way
wordexp invokes Bash to perform word expansion: stdout and stderr are
redirected to a pipe, but stdin is left alone. It seems that if Bash sees that
stdin is connected to a terminal, then Bash will immediately reset the
terminal's parameters (presumably to some state that Bash considers sane).

The below patch is my first stab at fixing the problem. This makes wordexp also
redirect stdin to the pipe before invoking Bash. It thereby prevents Bash from
seeing it is connected to a terminal and avoids the reset of the controlling
terminal's parameters.

2010-07-22  Dan Moulding  <dmoulding@ll.mit.edu>

	* libc/posix/wordexp.c (wordexp): Prevent wordexp from modifying
	terminal parameters.

Index: newlib/libc/posix/wordexp.c
RCS file: /cvs/src/src/newlib/libc/posix/wordexp.c,v
retrieving revision 1.1
diff -u -p -r1.1 wordexp.c
--- newlib/libc/posix/wordexp.c	31 Oct 2008 21:03:41 -0000	1.1
+++ newlib/libc/posix/wordexp.c	22 Jul 2010 16:01:23 -0000
@@ -159,7 +159,6 @@ wordexp(const char *words, wordexp_t *pw
       /* In child process. */
       /* Close read end of child's pipe. */
-      close(fd[0]);
       /* Pipe standard output to parent process via fd. */
@@ -178,6 +177,14 @@ wordexp(const char *words, wordexp_t *pw
+      /* Pipe standard input from parent process via fd. */
+      if (fd[0] != STDIN_FILENO)
+        {
+	  dup2(fd[0], STDIN_FILENO);
+          /* fd[0] no longer required. */
+	  close(fd[0]);
+	}
       if (flags & WRDE_NOCMD)
         execl("/bin/bash", "bash", "--protected", "--wordexp", words, (char *)0);

More information about the Newlib mailing list