1 /*
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 
20 int
adjtime(const struct timeval * itv,struct timeval * otv)21 adjtime(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 }
59