blob: 1e80871040741f8d57e66bb7ced1a550d64eab45 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
3 *
4 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
5 */
6
7#include <limits.h>
8#include <sys/time.h>
9#include <sys/timex.h>
10#include <errno.h>
11
12
13#define MAX_SEC (LONG_MAX / 1000000L - 2)
14#define MIN_SEC (LONG_MIN / 1000000L + 2)
15
16#ifndef MOD_OFFSET
17#define modes mode
18#endif
19
20int
21adjtime(const struct timeval * itv, struct timeval * otv)
22{
23 struct timex tntx;
24
25 if (itv)
26 {
27 struct timeval tmp;
28
29 /* We will do some check here. */
30 tmp.tv_sec = itv->tv_sec + itv->tv_usec / 1000000L;
31 tmp.tv_usec = itv->tv_usec % 1000000L;
32 if (tmp.tv_sec > MAX_SEC || tmp.tv_sec < MIN_SEC)
33 {
34 __set_errno(EINVAL);
35 return -1;
36 }
37 tntx.offset = tmp.tv_usec + tmp.tv_sec * 1000000L;
38 tntx.modes = ADJ_OFFSET_SINGLESHOT;
39 }
40 else
41 {
42 tntx.modes = 0;
43 }
44 if (adjtimex(&tntx) < 0) return -1;
45 if (otv) {
46 if (tntx.offset < 0)
47 {
48 otv->tv_usec = -(-tntx.offset % 1000000);
49 otv->tv_sec = -(-tntx.offset / 1000000);
50 }
51 else
52 {
53 otv->tv_usec = tntx.offset % 1000000;
54 otv->tv_sec = tntx.offset / 1000000;
55 }
56 }
57 return 0;
58}