Home
       make code POSIX compliant - bsleep - Unnamed repository; edit this file 'description' to name the repository.
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
   DIR commit 34d8a2829e40b14f76b6b212cda8e1dada80b1b0
   DIR parent a3d47929ad00f036aabe8bd131d65f612260861d
  HTML Author: kroovy <me@kroovy.de>
       Date:   Fri, 21 Feb 2025 01:26:15 +0100
       
       make code POSIX compliant
       
       Diffstat:
         M bsleep.c                            |      37 +++++++++++++++++++------------
       
       1 file changed, 23 insertions(+), 14 deletions(-)
       ---
   DIR diff --git a/bsleep.c b/bsleep.c
       @@ -1,35 +1,44 @@
       +#define _POSIX_SOURCE
        #include <signal.h>
       +#include <stdint.h>
        #include <stdio.h>
        #include <stdlib.h>
       +#include <sys/types.h>
        #include <unistd.h>
        
        int
        main(void)
        {
       -        int i,in,pid;
       +        int i, in;
       +        pid_t pid;
       +
       +        if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
       +                perror("signal");
       +                exit(EXIT_FAILURE);
       +        }
                pid = fork();
        
       -        if (pid == 0) {
       +        switch (pid) {
       +        case -1:
       +                perror("fork");
       +                exit(EXIT_FAILURE);
       +        case 0:
                        /* child */
                        for (i=1;;i++) {
       -                        printf("%d ", i); fflush(stdout); /* comment out if you prefer silent */
       +                        printf("%d ", i); fflush(stdout);
                                sleep(1);
                        }
       -                return 0;
       -
       -        } else if (pid > 0) {
       +                exit(EXIT_SUCCESS);
       +        default:
                        /* parent */
       -                system ("/bin/stty raw");
       +                printf("Child is PID %jd\n", (intmax_t) pid);
       +                system("/bin/stty raw");
                        while ((in = getchar()) != 'b') {
                                printf("%c ", in);
                        }
       -                system ("/bin/stty cooked");
       +                kill(pid, SIGKILL);
       +                system("/bin/stty cooked");
                        printf("\n");
       -                kill(pid, SIGKILL); /* kill child */
       -                return 0;
       -
       -        } else {
       -                return -1;
       +                exit(EXIT_SUCCESS);
                }
       -        return 0;
        }