summaryrefslogtreecommitdiffstats
path: root/tests/test_id.c
blob: 326ad3ff0e400a2a1c37af442d112c1d4ef8cafa (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
#include <stdlib.h>
#include <sys/time.h>
#include <stdio.h>
#include <time.h>

/* one provided by Aaaron Wiebe based on perl's hashing algorithm
 * (so probably pretty generic). Not for excessively large strings!
 */
#if defined(__clang__)
#pragma GCC diagnostic ignored "-Wunknown-attributes"
#endif
static unsigned __attribute__((nonnull(1))) int
#if defined(__clang__)
__attribute__((no_sanitize("unsigned-integer-overflow")))
#endif
hash_from_string(void *k)
{
	char *rkey = (char*) k;
	unsigned hashval = 1;

	while (*rkey)
		hashval = hashval * 33 + *rkey++;

	return hashval;
}

int main(int argc, char *argv[])
{
	struct timeval tv;
	struct timezone tz;
	gettimeofday(&tv, &tz);
	if(argc != 2) {
		fprintf(stderr, "usage: test_id test-file-name\n");
		exit(1);
	}
	printf("%06ld_%04.4x", tv.tv_usec, hash_from_string(argv[1]));

	return 0;
}