Versione funzionante di un CGI che produce PNG di codici a barre.
[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 #define _GNU_SOURCE
32 #include <unistd.h>      /* unix standard library */
33 #include <signal.h>      /* signal standard library */
34
35 void alarm_hand(int sig) {
36     /* check if the signal is the right one */
37     if (sig != SIGALRM) { /* if not exit with error */
38         printf("Something wrong, handler for SIGALRM\n");
39         exit(1);
40     } else {    /* do nothing, just interrupt pause */
41         return;
42     }
43 }
44 unsigned int sleep(unsigned int seconds)
45 {
46     sighandler_t prev_handler;
47     /* install and check new handler */
48     if ((prev_handler = signal(SIGALRM, alarm_hand)) == SIG_ERR) {
49         printf("Cannot set handler for alarm\n"); 
50         exit(-1);
51     }
52     /* set alarm and go to sleep */
53     alarm(seconds); 
54     pause(); 
55     /* restore previous signal handler */
56     signal(SIGALRM, prev_handler);
57     /* return remaining time */
58     return alarm(0);
59 }