Modifiche e chiarimenti
[gapil.git] / sources / sleep1.c
1 /* sleep1.c
2  * 
3  * Copyright (C) 2002 Simone Piccardi
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at
8  * your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /****************************************************************
20  *
21  * Program sleep1.c: 
22  * Example of a broken implementation for sleep
23  *
24  * Author: Simone Piccardi
25  * Mar. 2002
26  *
27  ****************************************************************/
28 /* 
29  * Include needed headers
30  */
31 #include <unistd.h>      /* unix standard library */
32
33 unsigned int sleep(unsigned int seconds)
34 {
35 /* 
36  * Variables definition  
37  */
38     signandler_t prev_handler;
39
40     if ((prev_handler = signal(SIGALRM, alarm_hand)) == SIG_ERR) {
41         printf("Cannot set handler for alarm\n");
42         exit(1);
43     }
44     alarm(second);
45     pause(); 
46     /* restore previous signal handler */
47     signal(SIGALRM, prev_handler);
48     /* remove alarm, return remaining time */
49     return alarm(0);
50 }
51 void alarm_hand(int sig) {
52     /* check if the signal is the right one */
53     if (sig != SIGALRM) { /* if not exit with error */
54         printf("Something wrong, handler for SIGALRM\n");
55         exit(1);
56     } else {    /* do nothing, just interrupt pause */
57         return;
58     }
59 }