adjtime.c 596 B

123456789101112131415161718192021222324252627
  1. #define _GNU_SOURCE
  2. #include <sys/time.h>
  3. #include <sys/timex.h>
  4. #include <errno.h>
  5. #include "syscall.h"
  6. int adjtime(const struct timeval *in, struct timeval *out)
  7. {
  8. struct timex tx = { 0 };
  9. if (in) {
  10. if (in->tv_sec > 1000 || in->tv_usec > 1000000000) {
  11. errno = EINVAL;
  12. return -1;
  13. }
  14. tx.offset = in->tv_sec*1000000 + in->tv_usec;
  15. tx.modes = ADJ_OFFSET_SINGLESHOT;
  16. }
  17. if (syscall(SYS_adjtimex, &tx) < 0) return -1;
  18. if (out) {
  19. out->tv_sec = tx.offset / 1000000;
  20. if ((out->tv_usec = tx.offset % 1000000) < 0) {
  21. out->tv_sec--;
  22. out->tv_usec += 1000000;
  23. }
  24. }
  25. return 0;
  26. }