summaryrefslogtreecommitdiffstats
path: root/cdbget.c
diff options
context:
space:
mode:
Diffstat (limited to 'cdbget.c')
-rw-r--r--cdbget.c67
1 files changed, 67 insertions, 0 deletions
diff --git a/cdbget.c b/cdbget.c
new file mode 100644
index 0000000..1c76fe2
--- /dev/null
+++ b/cdbget.c
@@ -0,0 +1,67 @@
+#include "exit.h"
+#include "scan.h"
+#include "str.h"
+#include "buffer.h"
+#include "strerr.h"
+#include "cdb.h"
+
+#define FATAL "cdbget: fatal: "
+
+void die_read(void)
+{
+ strerr_die2sys(111,FATAL,"unable to read input: ");
+}
+void die_write(void)
+{
+ strerr_die2sys(111,FATAL,"unable to write output: ");
+}
+void die_usage(void)
+{
+ strerr_die1x(111,"cdbget: usage: cdbget key [skip]");
+}
+
+static struct cdb c;
+char buf[1024];
+
+main(int argc,char **argv)
+{
+ char *key;
+ int r;
+ uint32 pos;
+ uint32 len;
+ unsigned long u = 0;
+
+ if (!*argv) die_usage();
+
+ if (!*++argv) die_usage();
+ key = *argv;
+
+ if (*++argv) {
+ scan_ulong(*argv,&u);
+ }
+
+ cdb_init(&c,0);
+ cdb_findstart(&c);
+
+ for (;;) {
+ r = cdb_findnext(&c,key,str_len(key));
+ if (r == -1) die_read();
+ if (!r) _exit(100);
+ if (!u) break;
+ --u;
+ }
+
+ pos = cdb_datapos(&c);
+ len = cdb_datalen(&c);
+
+ while (len > 0) {
+ r = sizeof buf;
+ if (r > len) r = len;
+ if (cdb_read(&c,buf,r,pos) == -1) die_read();
+ if (buffer_put(buffer_1small,buf,r) == -1) die_write();
+ pos += r;
+ len -= r;
+ }
+ if (buffer_flush(buffer_1small) == -1) die_write();
+ _exit(0);
+}