* regexec.c (group_nodes_into_DFAstates): Fix a buffer overrun
[gnulib.git] / lib / nanosleep.c
1 /* Provide a replacement for the POSIX nanosleep function.
2
3    Copyright (C) 1999, 2000, 2002, 2004, 2005, 2006 Free Software
4    Foundation, Inc.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software Foundation,
18    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19
20 /* written by Jim Meyering */
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 /* Undefine nanosleep here so any prototype is not redefined to be a
27    prototype for rpl_nanosleep. (they'd conflict e.g., on alpha-dec-osf3.2)  */
28 #undef nanosleep
29
30 #include <stdbool.h>
31 #include <stdio.h>
32 #if HAVE_SYS_SELECT_H
33 # include <sys/select.h>
34 #endif
35 #include <sys/types.h>
36 #include <signal.h>
37
38 #if TIME_WITH_SYS_TIME
39 # include <sys/time.h>
40 # include <time.h>
41 #else
42 # if HAVE_SYS_TIME_H
43 #  include <sys/time.h>
44 # else
45 #  include <time.h>
46 # endif
47 #endif
48
49 #include <errno.h>
50
51 #include <unistd.h>
52
53 #include "timespec.h"
54
55 /* Some systems (MSDOS) don't have SIGCONT.
56    Using SIGTERM here turns the signal-handling code below
57    into a no-op on such systems. */
58 #ifndef SIGCONT
59 # define SIGCONT SIGTERM
60 #endif
61
62 #if ! HAVE_SIGINTERRUPT
63 # define siginterrupt(sig, flag) /* empty */
64 #endif
65
66 static sig_atomic_t volatile suspended;
67
68 /* Handle SIGCONT. */
69
70 static void
71 sighandler (int sig)
72 {
73   suspended = 1;
74 }
75
76 /* Suspend execution for at least *TS_DELAY seconds.  */
77
78 static void
79 my_usleep (const struct timespec *ts_delay)
80 {
81   struct timeval tv_delay;
82   tv_delay.tv_sec = ts_delay->tv_sec;
83   tv_delay.tv_usec = (ts_delay->tv_nsec + 999) / 1000;
84   if (tv_delay.tv_usec == 1000000)
85     {
86       time_t t1 = tv_delay.tv_sec + 1;
87       if (t1 < tv_delay.tv_sec)
88         tv_delay.tv_usec = 1000000 - 1; /* close enough */
89       else
90         {
91           tv_delay.tv_sec = t1;
92           tv_delay.tv_usec = 0;
93         }
94     }
95   select (0, NULL, NULL, NULL, &tv_delay);
96 }
97
98 /* Suspend execution for at least *REQUESTED_DELAY seconds.  The
99    *REMAINING_DELAY part isn't implemented yet.  */
100
101 int
102 rpl_nanosleep (const struct timespec *requested_delay,
103                struct timespec *remaining_delay)
104 {
105   static bool initialized;
106
107   /* set up sig handler */
108   if (! initialized)
109     {
110 #ifdef SA_NOCLDSTOP
111       struct sigaction oldact, newact;
112       newact.sa_handler = sighandler;
113       sigemptyset (&newact.sa_mask);
114       newact.sa_flags = 0;
115
116       sigaction (SIGCONT, NULL, &oldact);
117       if (oldact.sa_handler != SIG_IGN)
118         sigaction (SIGCONT, &newact, NULL);
119 #else
120       if (signal (SIGCONT, SIG_IGN) != SIG_IGN)
121         {
122           signal (SIGCONT, sighandler);
123           siginterrupt (SIGCONT, 1);
124         }
125 #endif
126       initialized = true;
127     }
128
129   suspended = 0;
130
131   my_usleep (requested_delay);
132
133   if (suspended)
134     {
135       /* Calculate time remaining.  */
136       /* FIXME: the code in sleep doesn't use this, so there's no
137          rush to implement it.  */
138
139       errno = EINTR;
140     }
141
142   /* FIXME: Restore sig handler?  */
143
144   return suspended;
145 }