clock_gettime.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * clock_gettime() for uClibc
  3. *
  4. * Copyright (C) 2003 by Justus Pendleton <uc@ryoohki.net>
  5. * Copyright (C) 2005 by Peter Kjellerstedt <pkj@axis.com>
  6. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  7. *
  8. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  9. */
  10. #include <sys/syscall.h>
  11. #include <time.h>
  12. #ifdef __VDSO_SUPPORT__
  13. #include "ldso.h"
  14. #endif
  15. #if defined(__UCLIBC_USE_TIME64__) && defined(__NR_clock_gettime64)
  16. #include "internal/time64_helpers.h"
  17. int __libc_clock_gettime(clockid_t clock_id, struct timespec *tp)
  18. {
  19. struct __ts64_struct __ts64;
  20. int __ret = INLINE_SYSCALL(clock_gettime64, 2, clock_id, &__ts64);
  21. if (tp) {
  22. tp->tv_sec = __ts64.tv_sec;
  23. tp->tv_nsec = __ts64.tv_nsec;
  24. }
  25. return __ret;
  26. }
  27. #elif defined(__NR_clock_gettime)
  28. int __libc_clock_gettime(clockid_t clock_id, struct timespec *tp)
  29. {
  30. return INLINE_SYSCALL(clock_gettime, 2, clock_id, tp);
  31. }
  32. #else
  33. # include <sys/time.h>
  34. int __libc_clock_gettime(clockid_t clock_id, struct timespec* tp)
  35. {
  36. struct timeval tv;
  37. int retval = -1;
  38. switch (clock_id) {
  39. case CLOCK_REALTIME:
  40. /* In Linux, gettimeofday fails only on bad parameter.
  41. * We know that here parameter isn't bad.
  42. */
  43. gettimeofday(&tv, NULL);
  44. TIMEVAL_TO_TIMESPEC(&tv, tp);
  45. retval = 0;
  46. break;
  47. default:
  48. errno = EINVAL;
  49. break;
  50. }
  51. return retval;
  52. }
  53. #endif
  54. int clock_gettime(clockid_t clock_id, struct timespec *tp)
  55. {
  56. #if defined(__VDSO_SUPPORT__) && defined(ARCH_VDSO_CLOCK_GETTIME)
  57. return ARCH_VDSO_CLOCK_GETTIME(clock_id, tp);
  58. #else
  59. return __libc_clock_gettime(clock_id, tp);
  60. #endif
  61. }