add copyright/no-warranty comment
[coreutils.git] / lib / nanosleep.c
blob1f17c09cf51eb11404e6c8d2e1a2fea6ca7fd337
1 /* Provide a replacement for the POSIX nanosleep function.
2 Copyright (C) 1999, 2000 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* written by Jim Meyering */
20 #include <config.h>
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <signal.h>
25 #include <errno.h>
26 #ifndef errno
27 extern int errno;
28 #endif
30 #if HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
34 #include "nanosleep.h"
36 static int suspended;
37 int first_call = 1;
39 /* Handle SIGCONT. */
41 static void
42 sighandler (int sig)
44 suspended = 1;
47 /* FIXME: comment */
49 static void
50 my_usleep (const struct timespec *ts_delay)
52 struct timeval tv_delay;
53 tv_delay.tv_sec = ts_delay->tv_sec;
54 tv_delay.tv_usec = ts_delay->tv_nsec / 1000;
55 select (0, (void *) 0, (void *) 0, (void *) 0, &tv_delay);
58 /* FIXME: comment */
60 int
61 nanosleep (const struct timespec *requested_delay,
62 struct timespec *remaining_delay)
64 #ifdef SA_INTERRUPT
65 struct sigaction oldact, newact;
66 #endif
68 suspended = 0;
70 /* set up sig handler */
71 if (first_call)
73 #ifdef SA_INTERRUPT
74 newact.sa_handler = sighandler;
75 sigemptyset (&newact.sa_mask);
76 newact.sa_flags = 0;
78 sigaction (SIGCONT, NULL, &oldact);
79 if (oldact.sa_handler != SIG_IGN)
80 sigaction (SIGCONT, &newact, NULL);
81 #else
82 if (signal (SIGCONT, SIG_IGN) != SIG_IGN)
83 signal (SIGCONT, sighandler);
84 #endif
85 first_call = 0;
88 my_usleep (requested_delay);
90 if (suspended)
92 /* Calculate time remaining. */
93 /* FIXME: the code in sleep doesn't use this, so there's no
94 rush to implement it. */
96 errno = EINTR;
99 /* FIXME: Restore sig handler? */
101 return suspended;