1 /* this is placed into the public domain */
2 
3 #include <stdio.h>
4 #include "cdb.h"
5 
6 /* basically this is cdb_dump by djb, with a few changes to print the
7  * utftpd configuration file format
8  */
9 
format(void)10 static void format(void)
11 {
12   fputs("cdbdump: fatal: bad database format\n",stderr);
13   exit(1);
14 }
15 
readerror(void)16 static void readerror(void)
17 {
18   if (ferror(stdin)) { perror("cdbdump: fatal: unable to read"); exit(111); }
19   format();
20 }
21 
main(void)22 int main(void)
23 {
24   uint32 eod;
25   uint32 pos;
26   uint32 klen;
27   uint32 dlen;
28   unsigned char buf[8];
29   int i;
30   int c;
31 
32   if (fread(buf,1,4,stdin) < 4) readerror();
33   eod = cdb_unpack(buf);
34   for (i = 4;i < 2048;++i) if (getchar() == EOF) readerror();
35 
36   pos = 2048;
37   while (pos < eod) {
38     int eq_found=0;
39     if (eod - pos < 8) format();
40     pos += 8;
41     if (fread(buf,1,8,stdin) < 8) readerror();
42     klen = cdb_unpack(buf);
43     dlen = cdb_unpack(buf + 4);
44     if (eod - pos < klen) format();
45     pos += klen;
46     if (eod - pos < dlen) format();
47     pos += dlen;
48     fputs("client ",stdout);
49     while (klen) {
50       --klen;
51       c = getchar();
52       if (c == EOF) readerror();
53       putchar(c);
54     }
55     fputs(" {\n  ",stdout);
56     while (dlen) {
57       --dlen;
58       c = getchar();
59       if (c == EOF) readerror();
60 	  if (c=='\0') {
61     	fputs("\";\n  ",stdout);
62 		eq_found=0;
63 	  } else if (c=='=' && !eq_found) {
64         putchar(c);
65         putchar('"');
66 		eq_found=1;
67 	  } else
68         putchar(c);
69     }
70    	fputs("}\n",stdout);
71   }
72   return 0;
73 }
74