comparison cdbdump.c @ 0:2cb46628feec

from cdb-0.75 per http://cr.yp.to/cdb.html, retrieved 2025-01-03, contents appear to have been create on 2000-02-19
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Wed, 29 Jan 2025 10:50:34 +0000
parents
children 3bfef1f22a97
comparison
equal deleted inserted replaced
-1:000000000000 0:2cb46628feec
1 #include "uint32.h"
2 #include "fmt.h"
3 #include "buffer.h"
4 #include "strerr.h"
5
6 #define FATAL "cdbdump: fatal: "
7
8 void die_write(void)
9 {
10 strerr_die2sys(111,FATAL,"unable to write output: ");
11 }
12 void put(char *buf,unsigned int len)
13 {
14 if (buffer_put(buffer_1,buf,len) == -1) die_write();
15 }
16 void putflush(void)
17 {
18 if (buffer_flush(buffer_1) == -1) die_write();
19 }
20
21 uint32 pos = 0;
22
23 void get(char *buf,unsigned int len)
24 {
25 int r;
26 while (len > 0) {
27 r = buffer_get(buffer_0,buf,len);
28 if (r == -1)
29 strerr_die2sys(111,FATAL,"unable to read input: ");
30 if (r == 0)
31 strerr_die2x(111,FATAL,"unable to read input: truncated file");
32 pos += r;
33 buf += r;
34 len -= r;
35 }
36 }
37
38 char buf[512];
39
40 void copy(uint32 len)
41 {
42 unsigned int x;
43
44 while (len) {
45 x = sizeof buf;
46 if (len < x) x = len;
47 get(buf,x);
48 put(buf,x);
49 len -= x;
50 }
51 }
52
53 void getnum(uint32 *num)
54 {
55 get(buf,4);
56 uint32_unpack(buf,num);
57 }
58
59 char strnum[FMT_ULONG];
60
61 main()
62 {
63 uint32 eod;
64 uint32 klen;
65 uint32 dlen;
66
67 getnum(&eod);
68 while (pos < 2048) getnum(&dlen);
69
70 while (pos < eod) {
71 getnum(&klen);
72 getnum(&dlen);
73 put("+",1); put(strnum,fmt_ulong(strnum,klen));
74 put(",",1); put(strnum,fmt_ulong(strnum,dlen));
75 put(":",1); copy(klen);
76 put("->",2); copy(dlen);
77 put("\n",1);
78 }
79
80 put("\n",1);
81 putflush();
82 _exit(0);
83 }