bprompt.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <db.h>
  4. #include <fcntl.h>
  5. #include <limits.h>
  6. #include <unistd.h>
  7. #include <err.h>
  8. #include <string.h>
  9. #include <sys/types.h>
  10. #include <malloc_np.h>
  11. #include <getopt.h>
  12. #include <sys/stat.h>
  13. #include "bcompare.h"
  14. /* yacc */
  15. int yyparse(void);
  16. extern int yyin;
  17. int btreeparse(const char *line);
  18. int do_quit = 0;
  19. /* MALLOC_CONF=stats_print:true,junk:true */
  20. static DB * btreedb;
  21. #define KEYDATA_MAX 64
  22. static struct option longopts[] = {
  23. { "db", required_argument, NULL, 'd' },
  24. { NULL, 0, NULL, 0 }
  25. };
  26. __attribute__((noreturn)) static int usage(void)
  27. {
  28. fputs("bprompt [--db filename]\n", stderr);
  29. exit (EXIT_SUCCESS);
  30. }
  31. /*
  32. * insert key/val pair in database
  33. *
  34. * @key: key id
  35. * @data: cargo
  36. *
  37. * return 0 on success, -1 otherwise
  38. */
  39. int dbinsert(size_t key, char *data)
  40. {
  41. DBT k = {};
  42. DBT d = {};
  43. int rc = 0;
  44. size_t l =strnlen(data, KEYDATA_MAX);
  45. k.data = &key;
  46. k.size = sizeof key;
  47. d.size = l + 1;
  48. d.data = malloc(KEYDATA_MAX);
  49. *((char *) mempcpy (d.data, data, l)) = '\0';
  50. rc = btreedb->put(btreedb, &k, &d, R_NOOVERWRITE);
  51. if (rc == -1) {
  52. warn("Failed to insert record %zu:", key);
  53. goto dbinsert_out;
  54. }
  55. if (rc) {
  56. warnx("This record can't be ovewritten.");
  57. rc = -1;
  58. goto dbinsert_out;
  59. }
  60. printf("Record inserted with key %zu.\n", key);
  61. dbinsert_out:
  62. free(d.data);
  63. return rc;
  64. }
  65. /*
  66. * delete key/val pair from database
  67. *
  68. * @key: key id
  69. *
  70. * return 0 on success, -1 otherwise
  71. */
  72. int dbdelete(size_t key)
  73. {
  74. DBT k = {};
  75. k.data = &key;
  76. k.size = sizeof key;
  77. int rc;
  78. rc = btreedb->del(btreedb, &k, 0);
  79. if (rc == -1) {
  80. warn("Failed to delete record %zu:", key);
  81. return -1;
  82. }
  83. if (rc) {
  84. warnx("record key %zu was not found.", key);
  85. return -1;
  86. }
  87. printf("Record %zu deleted.\n", key);
  88. return 0;
  89. }
  90. /*
  91. * display key/val pair from database
  92. *
  93. * @key: key id
  94. *
  95. * return 0 on success, -1 otherwise
  96. */
  97. int dbget(size_t key)
  98. {
  99. DBT k = {};
  100. DBT d = {};
  101. k.data = &key;
  102. k.size = sizeof key;
  103. int rc;
  104. rc = btreedb->get(btreedb, &k, &d, 0);
  105. if (rc == -1) {
  106. warn("Failed to grab record %zu.", key);
  107. return -1;
  108. }
  109. if (rc) {
  110. warnx("record %zu was not found.", key);
  111. return -1;
  112. }
  113. printf("-> %s.\n", (char *)d.data);
  114. return 0;
  115. }
  116. int main(int argc, char *argv[])
  117. {
  118. char *dbname = NULL;
  119. char buf[PATH_MAX];
  120. /* btree */
  121. BTREEINFO type = {};
  122. /* opt */
  123. int ch;
  124. while ((ch = getopt_long(argc, argv, "d:", longopts, NULL)) != -1) {
  125. switch (ch) {
  126. case 'd':
  127. dbname = optarg;
  128. break;
  129. case 0:
  130. /* long option */
  131. break;
  132. default:
  133. usage();
  134. }
  135. }
  136. if (optind != argc)
  137. usage();
  138. if (dbname == NULL) {
  139. const char *t = getenv("TMPDIR");
  140. if (t == NULL)
  141. t = "/tmp";
  142. (void)snprintf(buf, sizeof(buf), "%s/david", t);
  143. dbname = buf;
  144. }
  145. type.compare = compare;
  146. btreedb= dbopen(dbname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, DB_BTREE, &type);
  147. if (!btreedb)
  148. err(EXIT_FAILURE, "Database unreachable:");
  149. while (!do_quit) {
  150. yyparse();
  151. }
  152. btreedb->close(btreedb);
  153. printf("Data stored into %s.\n", dbname);
  154. }