adjtime.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #define adjtimex __adjtimex
  2. #include <limits.h>
  3. #include <sys/time.h>
  4. #include <sys/timex.h>
  5. #include <errno.h>
  6. #define MAX_SEC (LONG_MAX / 1000000L - 2)
  7. #define MIN_SEC (LONG_MIN / 1000000L + 2)
  8. #ifndef MOD_OFFSET
  9. #define modes mode
  10. #endif
  11. int
  12. adjtime(const struct timeval * itv, struct timeval * otv)
  13. {
  14. struct timex tntx;
  15. if (itv)
  16. {
  17. struct timeval tmp;
  18. /* We will do some check here. */
  19. tmp.tv_sec = itv->tv_sec + itv->tv_usec / 1000000L;
  20. tmp.tv_usec = itv->tv_usec % 1000000L;
  21. if (tmp.tv_sec > MAX_SEC || tmp.tv_sec < MIN_SEC)
  22. {
  23. __set_errno(EINVAL);
  24. return -1;
  25. }
  26. tntx.offset = tmp.tv_usec + tmp.tv_sec * 1000000L;
  27. tntx.modes = ADJ_OFFSET_SINGLESHOT;
  28. }
  29. else
  30. {
  31. tntx.modes = 0;
  32. }
  33. if (adjtimex(&tntx) < 0) return -1;
  34. if (otv) {
  35. if (tntx.offset < 0)
  36. {
  37. otv->tv_usec = -(-tntx.offset % 1000000);
  38. otv->tv_sec = -(-tntx.offset / 1000000);
  39. }
  40. else
  41. {
  42. otv->tv_usec = tntx.offset % 1000000;
  43. otv->tv_sec = tntx.offset / 1000000;
  44. }
  45. }
  46. return 0;
  47. }