summaryrefslogtreecommitdiff
path: root/libc/sysdeps/linux/common/settimeofday.c
blob: 75d18ffc206c60032c926e3678eb2e0a235d6156 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
 * settimeofday() for uClibc
 *
 * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
 *
 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
 */

#include <sys/syscall.h>

#ifdef __USE_BSD
# include <sys/time.h>
# include <time.h>

int settimeofday(const struct timeval *tv, const struct timezone *tz)
{
	if (!tv)
		return 0;

	struct timespec __ts = {
		.tv_sec = tv->tv_sec,
		.tv_nsec = tv->tv_usec * 1000
	};

	return clock_settime(CLOCK_REALTIME, &__ts);
}

# elif defined __USE_SVID && defined __NR_stime
#  define __need_NULL
#  include <stddef.h>
#  include <errno.h>
#  include <time.h>
int settimeofday(const struct timeval *tv, const struct timezone *tz)
{
	time_t when;

	if (tv == NULL) {
		__set_errno(EINVAL);
		return -1;
	}

	if (tz != NULL || tv->tv_usec % 1000000 != 0) {
		__set_errno(ENOSYS);
		return -1;
	}

	when = tv->tv_sec + (tv->tv_usec / 1000000);
	return stime(&when);
}
# endif
# if defined __NR_settimeofday || defined(__UCLIBC_USE_TIME64__) || (defined __USE_SVID && defined __NR_stime)
libc_hidden_def(settimeofday)
# endif