X-Git-Url: https://gapil.gnulinux.it/gitweb/?p=gapil.git;a=blobdiff_plain;f=listati%2Fsleep_danger.c;fp=listati%2Fsleep_danger.c;h=1f7fb2152153292a8cff09cdac176cde089a1d0e;hp=0000000000000000000000000000000000000000;hb=bdf6e88eeb9b3aef06d57930ec8b89083639e56d;hpb=cf60963212306540ce7485ed7c86405e143a3352 diff --git a/listati/sleep_danger.c b/listati/sleep_danger.c new file mode 100644 index 0000000..1f7fb21 --- /dev/null +++ b/listati/sleep_danger.c @@ -0,0 +1,25 @@ +void alarm_hand(int sig) { + /* check if the signal is the right one */ + if (sig != SIGALRM) { /* if not exit with error */ + printf("Something wrong, handler for SIGALRM\n"); + exit(1); + } else { /* do nothing, just interrupt pause */ + return; + } +} +unsigned int sleep(unsigned int seconds) +{ + sighandler_t prev_handler; + /* install and check new handler */ + if ((prev_handler = signal(SIGALRM, alarm_hand)) == SIG_ERR) { + printf("Cannot set handler for alarm\n"); + exit(-1); + } + /* set alarm and go to sleep */ + alarm(seconds); + pause(); + /* restore previous signal handler */ + signal(SIGALRM, prev_handler); + /* return remaining time */ + return alarm(0); +}