]> git.ozlabs.org Git - ccan/blob - ccan/tdb/_info
compiler, list, noerr, sparse_bsearch, str, str_talloc, stringmap, talloc_link, tdb...
[ccan] / ccan / tdb / _info
1 #include <string.h>
2 #include <stdio.h>
3
4 /**
5  * tdb - The trivial (transactional) database
6  *
7  * The tdb module provides an efficient keyword data mapping (usually
8  * within a file).  It supports transactions, so the contents of the
9  * database is reliable even across crashes.
10  *
11  * Example:
12  *      #include <ccan/tdb/tdb.h>
13  *      #include <ccan/str/str.h>
14  *      #include <err.h>
15  *      #include <stdio.h>
16  *      
17  *      static void usage(const char *argv0)
18  *      {
19  *              errx(1, "Usage: %s fetch <dbfile> <key>\n"
20  *                   "OR %s store <dbfile> <key> <data>", argv0, argv0);
21  *      }
22  *      
23  *      int main(int argc, char *argv[])
24  *      {
25  *              struct tdb_context *tdb;
26  *              TDB_DATA key, value;
27  *      
28  *              if (argc < 4)
29  *                      usage(argv[0]);
30  *      
31  *              tdb = tdb_open(argv[2], 1024, TDB_DEFAULT, O_CREAT|O_RDWR,
32  *                              0600);
33  *              if (!tdb)
34  *                      err(1, "Opening %s", argv[2]);
35  *      
36  *              key.dptr = (void *)argv[3];
37  *              key.dsize = strlen(argv[3]);
38  *      
39  *              if (streq(argv[1], "fetch")) {
40  *                      if (argc != 4)
41  *                              usage(argv[0]);
42  *                      value = tdb_fetch(tdb, key);
43  *                      if (!value.dptr)
44  *                              errx(1, "fetch %s: %s",
45  *                                   argv[3], tdb_errorstr(tdb));
46  *                      printf("%.*s\n", value.dsize, (char *)value.dptr);
47  *                      free(value.dptr);
48  *              } else if (streq(argv[1], "store")) {
49  *                      if (argc != 5)
50  *                              usage(argv[0]);
51  *                      value.dptr = (void *)argv[4];
52  *                      value.dsize = strlen(argv[4]);
53  *                      if (tdb_store(tdb, key, value, 0) != 0)
54  *                              errx(1, "store %s: %s",
55  *                                   argv[3], tdb_errorstr(tdb));
56  *              } else
57  *                      usage(argv[0]);
58  *      
59  *              return 0;
60  *      }
61  *
62  * Maintainer: Rusty Russell <rusty@rustcorp.com.au>
63  *
64  * Author: Andrew Tridgell, Jeremy Allison, Rusty Russell
65  *
66  * Licence: LGPLv3 (or later)
67  *
68  * Fails: valgrind-tests // valgrind breaks fcntl locks.
69  */
70 int main(int argc, char *argv[])
71 {
72         if (argc != 2)
73                 return 1;
74
75         if (strcmp(argv[1], "depends") == 0) {
76                 printf("ccan/compiler\n");
77                 printf("ccan/tally\n");
78                 return 0;
79         }
80
81         return 1;
82 }