Skip to content

Commit

Permalink
only close stdin when we read arguments from it
Browse files Browse the repository at this point in the history
  • Loading branch information
leahneukirchen committed Feb 10, 2016
1 parent 86fa5ec commit dfb94ad
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
11 changes: 10 additions & 1 deletion tests
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/sh
printf '1..38\n'
printf '1..40\n'

set -e

Expand Down Expand Up @@ -215,6 +215,15 @@ a
123
EOF

check_output 'should close stdin when arguments were read from it' 'necho a b c | $XE -s "sed q"' <<EOF
EOF

check_output 'should not close stdin when arguments were read from command line' 'yes | $XE -a -s "sed q" -- 1 2 3' <<EOF
y
y
y
EOF

printf '# limit checks, expecting maximal POSIX limits available\n'

check_output 'argscap check' 'head -c 17711 /dev/zero | tr "\0" "\012" | $XE -N0 -s "echo \$#"' <<EOF
Expand Down
16 changes: 9 additions & 7 deletions xe.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,16 @@ run()
char iter[32];
snprintf(iter, sizeof iter, "%ld", iterations);
setenv("ITER", iter, 1);
// redirect stdin to /dev/null
int fd = open("/dev/null", O_RDONLY);
if (fd >= 0) {
if (dup2(fd, 0) != 0)
exit(1);
close(fd);
execvp(args[0], args);
// redirect stdin to /dev/null when we read arguments from it
if (!(aflag || Aflag)) {
int fd = open("/dev/null", O_RDONLY);
if (fd >= 0) {
if (dup2(fd, 0) != 0)
exit(1);
close(fd);
}
}
execvp(args[0], args);
fprintf(stderr, "xe: %s: %s\n", args[0], strerror(errno));
exit(errno == ENOENT ? 127 : 126);
}
Expand Down

0 comments on commit dfb94ad

Please sign in to comment.