[hackers] [sbase] Use switch with fork() || FRIGN
commit a8bd21c0abe727d3292ecdb533cb308cac408072
Author: FRIGN <dev_AT_frign.de>
Date: Mon Mar 9 15:01:29 2015 +0100
Use switch with fork()
Allows dropping a local variable if the explicit PID is not needed
and it makes it clearer what happens.
Also, one should always strive for consistency for cases like these.
diff --git a/cron.c b/cron.c
index 88eb96f..b919c66 100644
--- a/cron.c
+++ b/cron.c
_AT_@ -102,20 +102,20 @@ runjob(char *cmd)
}
}
- pid = fork();
- if (pid < 0) {
+ switch ((pid = fork())) {
+ case -1:
logerr("error: failed to fork job: %s time: %s",
cmd, ctime(&t));
return;
- } else if (pid == 0) {
+ case 0:
setsid();
loginfo("run: %s pid: %d at %s",
cmd, getpid(), ctime(&t));
execl("/bin/sh", "/bin/sh", "-c", cmd, (char *)NULL);
- logerr("error: failed to execute job: %s time: %s",
+ logwarn("error: failed to execute job: %s time: %s",
cmd, ctime(&t));
_exit(1);
- } else {
+ default:
je = emalloc(sizeof(*je));
je->cmd = estrdup(cmd);
je->pid = pid;
diff --git a/tar.c b/tar.c
index d847c9d..9e00d87 100644
--- a/tar.c
+++ b/tar.c
_AT_@ -47,17 +47,16 @@ static char filtermode;
static FILE *
decomp(FILE *fp)
{
- pid_t pid;
int fds[2];
if (pipe(fds) < 0)
eprintf("pipe:");
- pid = fork();
- if (pid < 0) {
+ switch (fork()) {
+ case -1:
weprintf("fork:");
_exit(1);
- } else if (!pid) {
+ case 0:
dup2(fileno(fp), 0);
dup2(fds[1], 1);
close(fds[0]);
diff --git a/xargs.c b/xargs.c
index 983eeb2..322dcd5 100644
--- a/xargs.c
+++ b/xargs.c
_AT_@ -164,15 +164,13 @@ waitchld(void)
static void
spawn(void)
{
- pid_t pid;
int savederrno;
- pid = fork();
- if (pid < 0) {
+ switch (fork()) {
+ case -1:
weprintf("fork:");
_exit(1);
- }
- if (pid == 0) {
+ case 0:
execvp(*cmd, cmd);
savederrno = errno;
weprintf("execvp %s:", *cmd);
Received on Tue Mar 24 2015 - 23:54:13 CET
This archive was generated by hypermail 2.3.0
: Wed Mar 25 2015 - 00:12:09 CET