summaryrefslogtreecommitdiffstats
path: root/02_exercise/prog.c
blob: 17b727a38e309a92b2f61ec6d5ff51083eeb3a85 (plain) (blame)
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

static unsigned int hash(const void* key, size_t size) {
	const char* ptr = key;
	unsigned int hval;
	
	for (hval = 0x811c9dc5u; size --> 0; ++ptr) {
		hval ^= *ptr;
		hval *= 0x1000193u;
	}
	
	return hval;
}

static inline int roll(int sides) {
	return rand() / (RAND_MAX + 1.0) * sides;
}

int main(int argc, const char* argv[]) {
	int rc, delay;
	time_t now = time(0);
	
	/* initialize the random number generator */
	srand(hash(&now, sizeof(now)));
	
	/* determine exit code and time delay */
	switch (argc) {
	default:
		delay = atoi(argv[1]);
		rc = atoi(argv[2]);
		break;
		
	case 2:
		delay = atoi(argv[1]);
		rc = roll(2) - 1;
		break;
		
	case 1:
		delay = roll(10);
		rc = roll(2) - 1;
		break;
	}
	
	printf("Delay: %ds, Exit Code: %d\n", delay, rc);
	sleep(delay);
	
	return rc;
}