adjtime.c 1.2 KB

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