* _fpending.c: Include <config.h> unconditionally, since we no
[gnulib.git] / lib / settime.c
1 /* settime -- set the system clock
2
3    Copyright (C) 2002, 2004, 2005, 2006 Free Software Foundation, Inc.
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, or (at your option)
8    any later version.
9
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.
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 Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 /* Written by Paul Eggert.  */
20
21 #include <config.h>
22
23 #include "timespec.h"
24
25 #include <unistd.h>
26
27 #include <errno.h>
28
29 /* Some systems don't have ENOSYS.  */
30 #ifndef ENOSYS
31 # ifdef ENOTSUP
32 #  define ENOSYS ENOTSUP
33 # else
34 /* Some systems don't have ENOTSUP either.  */
35 #  define ENOSYS EINVAL
36 # endif
37 #endif
38
39 /* Set the system time.  */
40
41 int
42 settime (struct timespec const *ts)
43 {
44 #if defined CLOCK_REALTIME && HAVE_CLOCK_SETTIME
45   {
46     int r = clock_settime (CLOCK_REALTIME, ts);
47     if (r == 0 || errno == EPERM)
48       return r;
49   }
50 #endif
51
52 #if HAVE_SETTIMEOFDAY
53   {
54     struct timeval tv;
55
56     tv.tv_sec = ts->tv_sec;
57     tv.tv_usec = ts->tv_nsec / 1000;
58     return settimeofday (&tv, 0);
59   }
60 #elif HAVE_STIME
61   /* This fails to compile on OSF1 V5.1, due to stime requiring
62      a `long int*' and tv_sec is `int'.  But that system does provide
63      settimeofday.  */
64   return stime (&ts->tv_sec);
65 #else
66   errno = ENOSYS;
67   return -1;
68 #endif
69 }