sys_stat: Fix 'implicit declaration of function' warning on OS/2 kLIBC.
[gnulib.git] / lib / xnanosleep.c
blob86b6e6dc676a7ce081dd1624c6f5fbc5c255a8b2
1 /* xnanosleep.c -- a more convenient interface to nanosleep
3 Copyright (C) 2002-2007, 2009-2019 Free Software Foundation, Inc.
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 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Mostly written (for sleep.c) by Paul Eggert.
19 Factored out (creating this file) by Jim Meyering. */
21 #include <config.h>
23 #include "xnanosleep.h"
25 #include <timespec.h>
27 #include <errno.h>
28 #include <time.h>
30 /* Sleep until the time (call it WAKE_UP_TIME) specified as
31 SECONDS seconds after the time this function is called.
32 SECONDS must be non-negative. If SECONDS is so large that
33 it is not representable as a 'struct timespec', then use
34 the maximum value for that interval. Return -1 on failure
35 (setting errno), 0 on success. */
37 int
38 xnanosleep (double seconds)
40 struct timespec ts_sleep = dtotimespec (seconds);
42 for (;;)
44 /* Linux-2.6.8.1's nanosleep returns -1, but doesn't set errno
45 when resumed after being suspended. Earlier versions would
46 set errno to EINTR. nanosleep from linux-2.6.10, as well as
47 implementations by (all?) other vendors, doesn't return -1
48 in that case; either it continues sleeping (if time remains)
49 or it returns zero (if the wake-up time has passed). */
50 errno = 0;
51 if (nanosleep (&ts_sleep, NULL) == 0)
52 break;
53 if (errno != EINTR && errno != 0)
54 return -1;
57 return 0;