You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

faketime.c 572B

1234567891011121314151617181920212223242526272829303132333435
  1. #define _GNU_SOURCE
  2. #include <dlfcn.h>
  3. #include "time.h"
  4. #include "faketime.h"
  5. char frozen = 0;
  6. time_t frozen_time = 0;
  7. typedef time_t (*orig_time_f_type)(time_t*);
  8. orig_time_f_type orig_time = NULL;
  9. void _freeze(unsigned long time) {
  10. frozen_time = (time_t)time;
  11. frozen = 1;
  12. }
  13. void unfreeze() {
  14. frozen = 0;
  15. }
  16. time_t time(time_t *arg) {
  17. if (orig_time == NULL) {
  18. orig_time = (orig_time_f_type) dlsym(RTLD_NEXT, "time");
  19. }
  20. if (frozen) {
  21. if (arg) {
  22. *arg = frozen_time;
  23. }
  24. return frozen_time;
  25. } else {
  26. time_t time = orig_time(arg);
  27. return time;
  28. }
  29. }