[hackers] [sinit] Use switch for fork() || FRIGN

From: <git_AT_suckless.org>
Date: Fri, 23 Sep 2016 10:33:30 +0200 (CEST)

commit 731f65fc82afcd474f4c682f9f0069e5c80c86dd
Author: FRIGN <dev_AT_frign.de>
AuthorDate: Fri Sep 23 09:37:59 2016 +0200
Commit: sin <sin_AT_2f30.org>
CommitDate: Fri Sep 23 09:33:22 2016 +0100

    Use switch for fork()
    
    This saves us one local variable and 2 lines of code, while improving
    readability by using the switch-style we are used to from other suckless
    projects.
    
    We are allowed to check against -1, as POSIX clearly mandates for the
    RETURN VALUE:
    
    "Upon successful completion, fork() shall return 0 to the child process
    and shall return the process ID of the child process to the parent
    process. Both processes shall continue to execute from the fork()
    function. Otherwise, -1 shall be returned to the parent process, no
    child process shall be created, and errno shall be set to indicate the
    error."
    [http://pubs.opengroup.org/onlinepubs/9699919799/functions/fork.html]
    
    This way, checking against < 0 was overdoing it and it's sufficient to
    compare against -1, justifying the switch statement here.

diff --git a/sinit.c b/sinit.c
index e338f35..93f9925 100644
--- a/sinit.c
+++ b/sinit.c
_AT_@ -74,16 +74,14 @@ sigreboot(void)
 static void
 spawn(char *const argv[])
 {
- pid_t pid;
-
- pid = fork();
- if (pid < 0) {
- perror("fork");
- } else if (pid == 0) {
+ switch (fork()) {
+ case 0:
                 sigprocmask(SIG_UNBLOCK, &set, NULL);
                 setsid();
                 execvp(argv[0], argv);
                 perror("execvp");
                 _exit(1);
+ case -1:
+ perror("fork");
         }
 }
Received on Fri Sep 23 2016 - 10:33:30 CEST

This archive was generated by hypermail 2.3.0 : Fri Sep 23 2016 - 10:36:16 CEST