File: init.c

package info (click to toggle)
haskell-hashable 1.4.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 200 kB
  • sloc: haskell: 1,199; ansic: 77; makefile: 7
file content (39 lines) | stat: -rw-r--r-- 868 bytes parent folder | download | duplicates (2)
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
#include <stdint.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

uint64_t hs_hashable_init() {

    /* if there is /dev/urandom, read from it */
    FILE *urandom = fopen("/dev/urandom", "r");
    if (urandom) {
        uint64_t result = 0;
        size_t r = fread(&result, sizeof(uint64_t), 1, urandom);
        fclose(urandom);

        if (r == 1) {
            return result;
        } else {
            return 0xfeed1000;
        }

    } else {
        /* time of day */
        struct timeval tp = {0, 0};
        gettimeofday(&tp, NULL);

        /* cputime */
        clock_t c = clock();

        /* process id */
        pid_t p = getpid();

        return ((uint64_t) tp.tv_sec)
            ^ ((uint64_t) tp.tv_usec)
            ^ ((uint64_t) c << 16)
            ^ ((uint64_t) p << 32);
    }
}