From 922090060f68111835ebedd4f6c3995f5d7ff32d Mon Sep 17 00:00:00 2001 From: Alessandro Mauri Date: Thu, 23 Apr 2020 18:56:54 +0200 Subject: [PATCH] use switch for fork() save two local variables and improve readability by a bit --- macrod.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/macrod.c b/macrod.c index 9c2d85d..08bb5dc 100644 --- a/macrod.c +++ b/macrod.c @@ -265,7 +265,6 @@ int pressBufferRemove (struct pressed_buffer *pb, unsigned short key) return 0; } } - return 1; } @@ -283,14 +282,18 @@ void die (void) void execCommand (const char *path) { - pid_t child_pid = fork(); - if (child_pid == -1) die(); - // TODO: communication between parent and child about process status/errors/etc - if (!child_pid) { - /* we are the child */ - int ret = 0; - ret = execl(path, path, (char *) NULL); - if (ret != 0) + switch (fork()) { + case -1: + fprintf(stderr, "Could not fork: %s", strerror(errno)); + break; + case 0: + /* we are the child */ + if(execl(path, path, (char *) NULL) != 0) + /* execl only returns if an error occured, so we exit + * otherwise we duplicate the process */ exit(-1); + /* we shouldn't be here */ + break; } + // TODO: communication between parent and child about process status/errors/etc }