adjtime.c 1.1 KB

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