1168777Srwatson/*-
2168777Srwatson * Copyright (c) 2006 Robert N. M. Watson
3168777Srwatson * All rights reserved.
4168777Srwatson *
5168777Srwatson * Redistribution and use in source and binary forms, with or without
6168777Srwatson * modification, are permitted provided that the following conditions
7168777Srwatson * are met:
8168777Srwatson * 1. Redistributions of source code must retain the above copyright
9168777Srwatson *    notice, this list of conditions and the following disclaimer.
10168777Srwatson * 2. Redistributions in binary form must reproduce the above copyright
11168777Srwatson *    notice, this list of conditions and the following disclaimer in the
12168777Srwatson *    documentation and/or other materials provided with the distribution.
13168777Srwatson *
14168777Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15168777Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16168777Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17168777Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18168777Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19168777Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20168777Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21168777Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22168777Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23168777Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24168777Srwatson * SUCH DAMAGE.
25168777Srwatson *
26185573Srwatson * $P4: //depot/projects/trustedbsd/openbsm/compat/clock_gettime.h#3 $
27168777Srwatson */
28168777Srwatson
29168777Srwatson/*
30168777Srwatson * Compatibility routines for clock_gettime(CLOCK_REALTIME, ...) for systems
31168777Srwatson * that don't have it.  We don't use clockid_t in order to avoid conflicts
32168777Srwatson * with the native OS if it has one but not clock_gettime().  We also assume
33168777Srwatson * that the sys/time.h include has already happened at this point, so we have
34168777Srwatson * access to gettimeofday().
35168777Srwatson */
36168777Srwatson#include <errno.h>
37168777Srwatson
38168777Srwatson#define	CLOCK_REALTIME	0x2d4e1588
39168777Srwatson
40168777Srwatsonstatic inline int
41168777Srwatsonclock_gettime(int clock_id, struct timespec *ts)
42168777Srwatson{
43168777Srwatson	struct timeval tv;
44168777Srwatson
45168777Srwatson	if (clock_id != CLOCK_REALTIME) {
46168777Srwatson		errno = EINVAL;
47168777Srwatson		return (-1);
48168777Srwatson	}
49168777Srwatson	if (gettimeofday(&tv, NULL) < 0)
50168777Srwatson		return (-1);
51168777Srwatson	ts->tv_sec = tv.tv_sec;
52168777Srwatson	ts->tv_nsec = tv.tv_usec * 1000;
53168777Srwatson	return (0);
54168777Srwatson}
55