|
0
|
1 |
#include "uint32.h"
|
|
|
2 |
#include "fmt.h"
|
|
|
3 |
#include "buffer.h"
|
|
|
4 |
#include "strerr.h"
|
|
|
5 |
#include "seek.h"
|
|
|
6 |
#include "cdb.h"
|
|
|
7 |
|
|
|
8 |
#define FATAL "cdbstats: fatal: "
|
|
|
9 |
|
|
|
10 |
void die_read(void)
|
|
|
11 |
{
|
|
|
12 |
strerr_die2sys(111,FATAL,"unable to read input: ");
|
|
|
13 |
}
|
|
|
14 |
void die_readformat(void)
|
|
|
15 |
{
|
|
|
16 |
strerr_die2x(111,FATAL,"unable to read input: truncated file");
|
|
|
17 |
}
|
|
|
18 |
void die_write(void)
|
|
|
19 |
{
|
|
|
20 |
strerr_die2sys(111,FATAL,"unable to write output: ");
|
|
|
21 |
}
|
|
|
22 |
void put(char *buf,unsigned int len)
|
|
|
23 |
{
|
|
|
24 |
if (buffer_put(buffer_1small,buf,len) == -1) die_write();
|
|
|
25 |
}
|
|
|
26 |
void putflush(void)
|
|
|
27 |
{
|
|
|
28 |
if (buffer_flush(buffer_1small) == -1) die_write();
|
|
|
29 |
}
|
|
|
30 |
|
|
|
31 |
uint32 pos = 0;
|
|
|
32 |
|
|
|
33 |
void get(char *buf,unsigned int len)
|
|
|
34 |
{
|
|
|
35 |
int r;
|
|
|
36 |
while (len > 0) {
|
|
|
37 |
r = buffer_get(buffer_0,buf,len);
|
|
|
38 |
if (r == -1) die_read();
|
|
|
39 |
if (r == 0) die_readformat();
|
|
|
40 |
pos += r;
|
|
|
41 |
buf += r;
|
|
|
42 |
len -= r;
|
|
|
43 |
}
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
void getnum(uint32 *num)
|
|
|
47 |
{
|
|
|
48 |
char buf[4];
|
|
|
49 |
get(buf,4);
|
|
|
50 |
uint32_unpack(buf,num);
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
char strnum[FMT_ULONG];
|
|
|
54 |
|
|
|
55 |
void putnum(char *label,unsigned long count)
|
|
|
56 |
{
|
|
|
57 |
unsigned int i;
|
|
|
58 |
put(label,str_len(label));
|
|
|
59 |
for (i = fmt_ulong(0,count);i < 10;++i) put(" ",1);
|
|
|
60 |
put(strnum,fmt_ulong(strnum,count));
|
|
|
61 |
put("\n",1);
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
char key[1024];
|
|
|
65 |
|
|
|
66 |
static struct cdb c;
|
|
|
67 |
|
|
|
68 |
static unsigned long numrecords;
|
|
|
69 |
static unsigned long numd[11];
|
|
|
70 |
|
|
|
71 |
main()
|
|
|
72 |
{
|
|
|
73 |
uint32 eod;
|
|
|
74 |
uint32 klen;
|
|
|
75 |
uint32 dlen;
|
|
|
76 |
seek_pos rest;
|
|
|
77 |
int r;
|
|
|
78 |
|
|
|
79 |
cdb_init(&c,0);
|
|
|
80 |
|
|
|
81 |
getnum(&eod);
|
|
|
82 |
while (pos < 2048) getnum(&dlen);
|
|
|
83 |
|
|
|
84 |
while (pos < eod) {
|
|
|
85 |
getnum(&klen);
|
|
|
86 |
getnum(&dlen);
|
|
|
87 |
if (klen > sizeof key) {
|
|
|
88 |
while (klen) { get(key,1); --klen; }
|
|
|
89 |
}
|
|
|
90 |
else {
|
|
|
91 |
get(key,klen);
|
|
|
92 |
rest = seek_cur(0);
|
|
|
93 |
cdb_findstart(&c);
|
|
|
94 |
do {
|
|
|
95 |
switch(cdb_findnext(&c,key,klen)) {
|
|
|
96 |
case -1: die_read();
|
|
|
97 |
case 0: die_readformat();
|
|
|
98 |
}
|
|
|
99 |
} while (cdb_datapos(&c) != pos);
|
|
|
100 |
if (!c.loop) die_readformat();
|
|
|
101 |
++numrecords;
|
|
|
102 |
if (c.loop > 10)
|
|
|
103 |
++numd[10];
|
|
|
104 |
else
|
|
|
105 |
++numd[c.loop - 1];
|
|
|
106 |
if (seek_set(0,rest) == -1) die_read();
|
|
|
107 |
}
|
|
|
108 |
while (dlen) { get(key,1); --dlen; }
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
putnum("records ",numrecords);
|
|
|
112 |
putnum("d0 ",numd[0]);
|
|
|
113 |
putnum("d1 ",numd[1]);
|
|
|
114 |
putnum("d2 ",numd[2]);
|
|
|
115 |
putnum("d3 ",numd[3]);
|
|
|
116 |
putnum("d4 ",numd[4]);
|
|
|
117 |
putnum("d5 ",numd[5]);
|
|
|
118 |
putnum("d6 ",numd[6]);
|
|
|
119 |
putnum("d7 ",numd[7]);
|
|
|
120 |
putnum("d8 ",numd[8]);
|
|
|
121 |
putnum("d9 ",numd[9]);
|
|
|
122 |
putnum(">9 ",numd[10]);
|
|
|
123 |
putflush();
|
|
|
124 |
_exit(0);
|
|
|
125 |
}
|