adjtime.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* Copyright (C) 1995-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #include <errno.h>
  15. #include <limits.h>
  16. #include <sys/time.h>
  17. #include <sys/timex.h>
  18. #define MAX_SEC (INT_MAX / 1000000L - 2)
  19. #define MIN_SEC (INT_MIN / 1000000L + 2)
  20. #ifndef MOD_OFFSET
  21. #define modes mode
  22. #endif
  23. #ifndef TIMEVAL
  24. #define TIMEVAL timeval
  25. #endif
  26. #ifndef TIMEX
  27. #define TIMEX timex
  28. #endif
  29. #ifndef ADJTIME
  30. #define ADJTIME __adjtime
  31. #endif
  32. #ifndef ADJTIMEX
  33. #define NO_LOCAL_ADJTIME
  34. #define ADJTIMEX(x) __adjtimex (x)
  35. #endif
  36. #ifndef LINKAGE
  37. #define LINKAGE
  38. #endif
  39. LINKAGE int
  40. ADJTIME (const struct TIMEVAL *itv, struct TIMEVAL *otv)
  41. {
  42. struct TIMEX tntx;
  43. if (itv)
  44. {
  45. struct TIMEVAL tmp;
  46. /* We will do some check here. */
  47. tmp.tv_sec = itv->tv_sec + itv->tv_usec / 1000000L;
  48. tmp.tv_usec = itv->tv_usec % 1000000L;
  49. if (tmp.tv_sec > MAX_SEC || tmp.tv_sec < MIN_SEC)
  50. return INLINE_SYSCALL_ERROR_RETURN_VALUE (EINVAL);
  51. tntx.offset = tmp.tv_usec + tmp.tv_sec * 1000000L;
  52. tntx.modes = ADJ_OFFSET_SINGLESHOT;
  53. }
  54. else
  55. tntx.modes = ADJ_OFFSET_SS_READ;
  56. if (__glibc_unlikely (ADJTIMEX (&tntx) < 0))
  57. return -1;
  58. if (otv)
  59. {
  60. if (tntx.offset < 0)
  61. {
  62. otv->tv_usec = -(-tntx.offset % 1000000);
  63. otv->tv_sec = -(-tntx.offset / 1000000);
  64. }
  65. else
  66. {
  67. otv->tv_usec = tntx.offset % 1000000;
  68. otv->tv_sec = tntx.offset / 1000000;
  69. }
  70. }
  71. return 0;
  72. }
  73. #ifdef NO_LOCAL_ADJTIME
  74. weak_alias (__adjtime, adjtime)
  75. #endif