2011-05-08  Yaakov Selkowitz  <yselkowitz@users.sourceforge.net>

	* times.cc (settimeofday): Set errno to EINVAL if tv.tv_nsec is
	invalid, and to EPERM if SetSystemTime fails.  Return -1 in case
	of failure, all for compatibility with BSD and Linux.
	(clock_settime): New function.
	* cygwin.din (clock_settime): Export.
	* posix.sgml (std-susv4): Add clock_settime.
	Move clock_setres from here...
	(std-deprec): ... to here.
	(std-notes): Correct limitation of clock_setres to only CLOCK_REALTIME.
	Add limitation of clock_settime to only CLOCK_REALTIME.
	* include/cygwin/version.h (CYGWIN_VERSION_API_MINOR): Bump.

Index: cygwin.din
===================================================================
RCS file: /cvs/src/src/winsup/cygwin/cygwin.din,v
retrieving revision 1.239
diff -u -r1.239 cygwin.din
--- cygwin.din	6 May 2011 18:53:21 -0000	1.239
+++ cygwin.din	8 May 2011 09:02:14 -0000
@@ -220,6 +220,7 @@
 clock_getres SIGFE
 clock_gettime SIGFE
 clock_setres SIGFE
+clock_settime SIGFE
 clog NOSIGFE
 clogf NOSIGFE
 close SIGFE
Index: posix.sgml
===================================================================
RCS file: /cvs/src/src/winsup/cygwin/posix.sgml,v
retrieving revision 1.61
diff -u -r1.61 posix.sgml
--- posix.sgml	6 May 2011 18:53:21 -0000	1.61
+++ posix.sgml	8 May 2011 22:19:22 -0000
@@ -92,7 +92,7 @@
     clock
     clock_getres		(see chapter "Implementation Notes")
     clock_gettime		(see chapter "Implementation Notes")
-    clock_setres		(see chapter "Implementation Notes")
+    clock_settime		(see chapter "Implementation Notes")
     clog
     clogf
     close
@@ -1214,6 +1214,7 @@
     bcmp			(POSIX.1-2001, SUSv3)
     bcopy			(SUSv3)
     bzero			(SUSv3)
+    clock_setres		(QNX, VxWorks) (see chapter "Implementation Notes")
     cuserid			(POSIX.1-1988, SUSv2)
     ecvt			(SUSv3)
     endutent			(XPG2)
@@ -1439,9 +1440,9 @@
 related function calls.  A real chroot functionality is not supported by
 Windows however.</para>
 
-<para><function>clock_getres</function>, <function>clock_gettime</function>
-and <function>clock_setres</function> only support CLOCK_REALTIME and
-CLOCK_MONOTONIC for now.</para>
+<para><function>clock_getres</function> and <function>clock_gettime</function>
+only support CLOCK_REALTIME and CLOCK_MONOTONIC for now.  <function>clock_setres</function>
+and <function>clock_settime</function> only support CLOCK_REALTIME.</para>
 
 <para>BSD file locks created via <function>flock</function> are not
 propagated to the parent process and sibling processes.  The locks are
Index: times.cc
===================================================================
RCS file: /cvs/src/src/winsup/cygwin/times.cc,v
retrieving revision 1.107
diff -u -r1.107 times.cc
--- times.cc	2 May 2011 15:28:35 -0000	1.107
+++ times.cc	8 May 2011 17:55:34 -0000
@@ -111,6 +111,12 @@
 
   tz = tz;			/* silence warning about unused variable */
 
+  if (tv->tv_usec < 0 || tv->tv_usec >= 1000000)
+    {
+      set_errno (EINVAL);
+      return -1;
+    }
+
   ptm = gmtime (&tv->tv_sec);
   st.wYear	   = ptm->tm_year + 1900;
   st.wMonth	   = ptm->tm_mon + 1;
@@ -121,11 +127,14 @@
   st.wSecond       = ptm->tm_sec;
   st.wMilliseconds = tv->tv_usec / 1000;
 
-  res = !SetSystemTime (&st);
+  res = -!SetSystemTime (&st);
   gtod.reset ();
 
   syscall_printf ("%d = settimeofday (%x, %x)", res, tv, tz);
 
+  if (res != 0)
+    set_errno (EPERM);
+
   return res;
 }
 
@@ -614,6 +623,23 @@
   return 0;
 }
 
+extern "C" int
+clock_settime (clockid_t clk_id, const struct timespec *tp)
+{
+  struct timeval tv;
+
+  if (clk_id != CLOCK_REALTIME)
+    {
+      set_errno (EINVAL);
+      return -1;
+    }
+
+  tv.tv_sec = tp->tv_sec;
+  tv.tv_usec = tp->tv_nsec / 1000;
+
+  return settimeofday (&tv, NULL);
+}
+
 static DWORD minperiod;	// FIXME: Maintain period after a fork.
 
 LONGLONG
Index: include/cygwin/version.h
===================================================================
RCS file: /cvs/src/src/winsup/cygwin/include/cygwin/version.h,v
retrieving revision 1.344
diff -u -r1.344 version.h
--- include/cygwin/version.h	6 May 2011 18:53:21 -0000	1.344
+++ include/cygwin/version.h	8 May 2011 09:02:15 -0000
@@ -409,12 +409,13 @@
 	   pthread_getattr_np.
       242: Export psiginfo, psignal, sys_siglist.
       243: Export sysinfo.
+      244: Export clock_settime.
      */
 
      /* Note that we forgot to bump the api for ualarm, strtoll, strtoull */
 
 #define CYGWIN_VERSION_API_MAJOR 0
-#define CYGWIN_VERSION_API_MINOR 243
+#define CYGWIN_VERSION_API_MINOR 244
 
      /* There is also a compatibity version number associated with the
 	shared memory regions.  It is incremented when incompatible
