* MODULES.html.sh (Support for systems lacking POSIX:2001): New
[gnulib.git] / lib / settime.c
1 /* settime -- set the system clock
2
3    Copyright (C) 2002, 2004, 2005, 2006, 2007 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 Paul Eggert.  */
21
22 #include <config.h>
23
24 #include "timespec.h"
25
26 #include <sys/time.h>
27 #include <unistd.h>
28
29 #include <errno.h>
30
31 /* Some systems don't have ENOSYS.  */
32 #ifndef ENOSYS
33 # ifdef ENOTSUP
34 #  define ENOSYS ENOTSUP
35 # else
36 /* Some systems don't have ENOTSUP either.  */
37 #  define ENOSYS EINVAL
38 # endif
39 #endif
40
41 /* Set the system time.  */
42
43 int
44 settime (struct timespec const *ts)
45 {
46 #if defined CLOCK_REALTIME && HAVE_CLOCK_SETTIME
47   {
48     int r = clock_settime (CLOCK_REALTIME, ts);
49     if (r == 0 || errno == EPERM)
50       return r;
51   }
52 #endif
53
54 #if HAVE_SETTIMEOFDAY
55   {
56     struct timeval tv;
57
58     tv.tv_sec = ts->tv_sec;
59     tv.tv_usec = ts->tv_nsec / 1000;
60     return settimeofday (&tv, 0);
61   }
62 #elif HAVE_STIME
63   /* This fails to compile on OSF1 V5.1, due to stime requiring
64      a `long int*' and tv_sec is `int'.  But that system does provide
65      settimeofday.  */
66   return stime (&ts->tv_sec);
67 #else
68   errno = ENOSYS;
69   return -1;
70 #endif
71 }