File: 027.c

package info (click to toggle)
cfengine3 3.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 20,256 kB
  • ctags: 9,613
  • sloc: ansic: 116,129; sh: 12,366; yacc: 1,088; makefile: 1,006; lex: 391; perl: 197; xml: 21; sed: 4
file content (55 lines) | stat: -rw-r--r-- 1,171 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define CF_HASHTABLESIZE 8192

#define HOURLY 12
main() {
    char s[] = "a";
    char c;
    int hash, box, minblocks;
    int period=HOURLY-1;
    char *boxes[HOURLY];
    for (c=0; c<HOURLY; c++) {
	boxes[c] = 0;
    }
    for (c='a';c <='z'; c++){
	*s = c;
	// The block algorithm is copied from evalfunction.c
	hash=OatHash(s);
	box = (int)(0.5 + period*hash/(double)CF_HASHTABLESIZE);
	minblocks = box % HOURLY;
	// Back to original code
	if (!boxes[minblocks]) {
	    boxes[minblocks] = strncpy((char *)malloc(2),s,2);
	}
    }
    printf("    \"ok\" xor => {\n");
    for (c=0; c<HOURLY; c++) {
	printf("\tsplayclass(\"%s\",\"hourly\"), # Box %d\n", boxes[c], c);
    }
    printf("\t};\n");
}

// This is copied from files_hashes.c
int OatHash(char *key)
       
{ unsigned int hashtablesize = CF_HASHTABLESIZE;
unsigned char *p = key;
unsigned h = 0;
int i, len = strlen(key);
	 
for ( i = 0; i < len; i++ )
    {       
    h += p[i];
    h += ( h << 10 );
    h ^= ( h >> 6 );
    }       
     
  h += ( h << 3 );
  h ^= ( h >> 11 );
  h += ( h << 15 );
     
  return (h & (hashtablesize-1));
}