summaryrefslogtreecommitdiffstats
path: root/cdbget.c
blob: 1c76fe27d4e0806adce12c9f5b28f47116e58514 (plain)
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
56
57
58
59
60
61
62
63
64
65
66
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);
}