Skip to content

Commit

Permalink
allow handlers to write to stdout and stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Sep 15, 2022
1 parent 5cbff58 commit 37953fe
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,4 @@ additional documentation and more details are available in `man gplaces`. type `
## Statistic
Language|files|blank|comment|code
:-------|-------:|-------:|-------:|-------:
C|1|244|58|1128
C|1|244|58|1126
16 changes: 7 additions & 9 deletions gplaces.c
Original file line number Diff line number Diff line change
Expand Up @@ -424,17 +424,15 @@ static pid_t start_handler(const char *handler, const char *filename, Selector *
command[l] = '\0';

if ((pid = fork()) == 0) {
if ((fd = open("/dev/null", O_RDWR)) != -1) {
if (stdin == -1) dup2(fd, STDIN_FILENO);
else {
dup2(stdin, STDIN_FILENO);
close(stdin);
}
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
if (stdin == -1) {
if ((fd = open("/dev/null", O_RDWR)) == -1) exit(EXIT_FAILURE);
dup2(fd, STDIN_FILENO);
close(fd);
execl("/bin/sh", "sh", "-c", command, (char *)NULL);
} else {
dup2(stdin, STDIN_FILENO);
close(stdin);
}
execl("/bin/sh", "sh", "-c", command, (char *)NULL);
exit(EXIT_FAILURE);
} else if (pid < 0) error(0, "could not execute `%s`", command);
return pid;
Expand Down

0 comments on commit 37953fe

Please sign in to comment.