summaryrefslogtreecommitdiff
path: root/tests/common/time_past.c
blob: d0eb741e8035942be0e3b150a998dc1c929405e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* 
 * gcc time_past.c -o time_past.so -shared -ldl 
 * LD_PRELOAD=./time_past.so PAST_DAYS=2 ./test
 */

#define _GNU_SOURCE
#include <dlfcn.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdarg.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <assert.h>

#include <stdio.h>
#include <stdlib.h>


typedef time_t (*time_type) (time_t *t);
static time_type next_time;

static int time_past = 0;
static char *past = NULL;

time_t time (time_t *t)
{
	time_t res;

	if (NULL == next_time)
	{
		next_time = dlsym (RTLD_NEXT, "time");
		assert (NULL != next_time);
	}
	if (NULL == past) {
		const char *past = getenv ("PAST_DAYS");
		if (NULL == past) {
			fputs ("No PAST_DAYS defined\n", stderr);
		}
		time_past = atoi (past);
	}

	res = next_time (t);
	res -= 24*60*60*time_past;

	if (NULL != t) {
		*t = res;
	}

	return res;
}