xref: /freebsd/contrib/openbsm/compat/clock_gettime.h (revision b626f5a73a48f44a31a200291b141e1da408a2ff)
1bc168a6cSRobert Watson /*-
2bc168a6cSRobert Watson  * Copyright (c) 2006 Robert N. M. Watson
3bc168a6cSRobert Watson  * All rights reserved.
4bc168a6cSRobert Watson  *
5bc168a6cSRobert Watson  * Redistribution and use in source and binary forms, with or without
6bc168a6cSRobert Watson  * modification, are permitted provided that the following conditions
7bc168a6cSRobert Watson  * are met:
8bc168a6cSRobert Watson  * 1. Redistributions of source code must retain the above copyright
9bc168a6cSRobert Watson  *    notice, this list of conditions and the following disclaimer.
10bc168a6cSRobert Watson  * 2. Redistributions in binary form must reproduce the above copyright
11bc168a6cSRobert Watson  *    notice, this list of conditions and the following disclaimer in the
12bc168a6cSRobert Watson  *    documentation and/or other materials provided with the distribution.
13bc168a6cSRobert Watson  *
14bc168a6cSRobert Watson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15bc168a6cSRobert Watson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16bc168a6cSRobert Watson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17bc168a6cSRobert Watson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18bc168a6cSRobert Watson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19bc168a6cSRobert Watson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20bc168a6cSRobert Watson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21bc168a6cSRobert Watson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22bc168a6cSRobert Watson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23bc168a6cSRobert Watson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24bc168a6cSRobert Watson  * SUCH DAMAGE.
25bc168a6cSRobert Watson  */
26bc168a6cSRobert Watson 
27bc168a6cSRobert Watson /*
28bc168a6cSRobert Watson  * Compatibility routines for clock_gettime(CLOCK_REALTIME, ...) for systems
29bc168a6cSRobert Watson  * that don't have it.  We don't use clockid_t in order to avoid conflicts
30bc168a6cSRobert Watson  * with the native OS if it has one but not clock_gettime().  We also assume
31bc168a6cSRobert Watson  * that the sys/time.h include has already happened at this point, so we have
32bc168a6cSRobert Watson  * access to gettimeofday().
33bc168a6cSRobert Watson  */
34bc168a6cSRobert Watson #include <errno.h>
35bc168a6cSRobert Watson 
36bc168a6cSRobert Watson #define	CLOCK_REALTIME	0x2d4e1588
37bc168a6cSRobert Watson 
38bc168a6cSRobert Watson static inline int
clock_gettime(int clock_id,struct timespec * ts)39bc168a6cSRobert Watson clock_gettime(int clock_id, struct timespec *ts)
40bc168a6cSRobert Watson {
41bc168a6cSRobert Watson 	struct timeval tv;
42bc168a6cSRobert Watson 
43bc168a6cSRobert Watson 	if (clock_id != CLOCK_REALTIME) {
44bc168a6cSRobert Watson 		errno = EINVAL;
45bc168a6cSRobert Watson 		return (-1);
46bc168a6cSRobert Watson 	}
47bc168a6cSRobert Watson 	if (gettimeofday(&tv, NULL) < 0)
48bc168a6cSRobert Watson 		return (-1);
49bc168a6cSRobert Watson 	ts->tv_sec = tv.tv_sec;
50bc168a6cSRobert Watson 	ts->tv_nsec = tv.tv_usec * 1000;
51bc168a6cSRobert Watson 	return (0);
52bc168a6cSRobert Watson }
53