~ubuntu-branches/ubuntu/saucy/datefudge/saucy

2 by Robert Luberda
* New maintainer (closes: #429467).
1
/* vim:ts=4:sts=4:sw=4:et:cindent
1 by Matthias Urlichs
* Updated maintainer email.
2
 * datefudge.c: Twist system date back N seconds
3
 *
4
 * Copyright (C) 2001-2003, Matthias Urlichs <smurf@noris.de>
5 by Robert Luberda
* datefudge.{c,man}: Add the `--static' option for static dates.
5
 * Copyright (C) 2008-2011, Robert Luberda <robert@debian.org>
2 by Robert Luberda
* New maintainer (closes: #429467).
6
 *
1 by Matthias Urlichs
* Updated maintainer email.
7
 */
8
#define _GNU_SOURCE
9
10
#include <sys/file.h>
11
#include <sys/stat.h>
12
#include <fcntl.h>
13
#include <stdio.h>
14
#include <stdlib.h>
15
#include <dlfcn.h>
16
#include <assert.h>
17
#include <features.h>
18
#include <unistd.h>
19
#include <time.h>
20
#include <sys/time.h>
21
5 by Robert Luberda
* datefudge.{c,man}: Add the `--static' option for static dates.
22
static int fudge = 0;
23
static int dostatic = 0;
1 by Matthias Urlichs
* Updated maintainer email.
24
2 by Robert Luberda
* New maintainer (closes: #429467).
25
static void init_fudge (void) {
26
    const char *fud;
27
28
    if(fudge)return;
29
30
    fud = getenv("DATEFUDGE");
31
    if(fud == NULL) return;
32
    fudge = atoi(fud);
5 by Robert Luberda
* datefudge.{c,man}: Add the `--static' option for static dates.
33
    dostatic = getenv("DATEFUDGE_DOSTATIC") != NULL;
34
}
35
36
static void set_fudge(time_t *seconds)
37
{
38
    if (!seconds)
39
        return;
40
41
    init_fudge();
42
43
    if (dostatic)
44
        *seconds = fudge;
45
    else
46
        *seconds -= fudge;
1 by Matthias Urlichs
* Updated maintainer email.
47
}
48
49
/*
50
 * This won't work...
51
 *
52
text_set_element (__libc_subinit, init_logger);
53
 */
54
55
time_t time(time_t *x) {
2 by Robert Luberda
* New maintainer (closes: #429467).
56
    static time_t (*libc_time)(time_t *) = NULL;
57
    time_t res;
1 by Matthias Urlichs
* Updated maintainer email.
58
2 by Robert Luberda
* New maintainer (closes: #429467).
59
    if(!libc_time)
60
        libc_time = (typeof(libc_time))dlsym (RTLD_NEXT, __func__);
61
    res = (*libc_time)(x);
5 by Robert Luberda
* datefudge.{c,man}: Add the `--static' option for static dates.
62
    set_fudge(x);
63
    set_fudge(&res);
2 by Robert Luberda
* New maintainer (closes: #429467).
64
    return res;
1 by Matthias Urlichs
* Updated maintainer email.
65
}
66
67
int __gettimeofday(struct timeval *x, struct timezone *y) {
2 by Robert Luberda
* New maintainer (closes: #429467).
68
    static int (*libc_gettimeofday)(struct timeval *, struct timezone *) = NULL;
69
    int res;
70
71
    if(!libc_gettimeofday)
72
        libc_gettimeofday = (typeof(libc_gettimeofday))dlsym (RTLD_NEXT, __func__);
73
    res = (*libc_gettimeofday)(x,y);
74
    if(res) return res;
5 by Robert Luberda
* datefudge.{c,man}: Add the `--static' option for static dates.
75
    set_fudge(&x->tv_sec);
2 by Robert Luberda
* New maintainer (closes: #429467).
76
    return 0;
77
}
78
79
int gettimeofday(struct timeval *x, struct timezone *y) { 
80
    return __gettimeofday(x,y); 
81
}
82
4 by Robert Luberda
* datefudge.c: don't override clock_gettime(2) on GNU/Hurd - it seems
83
#ifndef __GNU__
2 by Robert Luberda
* New maintainer (closes: #429467).
84
85
int clock_gettime(clockid_t x, struct timespec *y) {
86
    static int (*libc_clock_gettime)(clockid_t, struct timespec*);
87
    int res;
88
89
    if (!libc_clock_gettime)
90
        libc_clock_gettime =  (typeof(libc_clock_gettime))dlsym (RTLD_NEXT, __func__);
91
    res = (*libc_clock_gettime)(x,y);
92
    if (res || CLOCK_REALTIME != x) return res;
5 by Robert Luberda
* datefudge.{c,man}: Add the `--static' option for static dates.
93
    set_fudge(&y->tv_sec);
2 by Robert Luberda
* New maintainer (closes: #429467).
94
    return 0;
95
}
96
4 by Robert Luberda
* datefudge.c: don't override clock_gettime(2) on GNU/Hurd - it seems
97
#endif