summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrsiddharth <s@ricketyspace.net>2020-04-09 20:37:12 -0400
committerrsiddharth <s@ricketyspace.net>2020-04-17 20:56:37 -0400
commitd5cedc6f28a60301bad21a16fdb047fdb82ff883 (patch)
tree8cb7e49633c52250447843e06b34b51e2eca8669
parent3430edf9a8497ae4552d44b753a674da9167b577 (diff)
nserver: Add db.h
* nserver/.gitignore: Add nserver.db * nserver/Makefile ($(PROGRAMS)) (tests): Link gdbm. (clean): Remove nserver.db * nserver/src/db.c (db_init): New function. * nserver/src/db.h (db_init): New function declaration. * nserver/tests/db_tests.c: Tests for db.h
-rw-r--r--nserver/.gitignore1
-rw-r--r--nserver/Makefile5
-rw-r--r--nserver/src/db.c20
-rw-r--r--nserver/src/db.h11
-rw-r--r--nserver/tests/db_tests.c21
5 files changed, 56 insertions, 2 deletions
diff --git a/nserver/.gitignore b/nserver/.gitignore
index 7a18cd1..7a6be0c 100644
--- a/nserver/.gitignore
+++ b/nserver/.gitignore
@@ -1 +1,2 @@
tests/*_tests
+nserver.db \ No newline at end of file
diff --git a/nserver/Makefile b/nserver/Makefile
index 5c6bc9c..3ecfd8d 100644
--- a/nserver/Makefile
+++ b/nserver/Makefile
@@ -28,7 +28,7 @@ $(TARGET): build $(OBJECTS)
$(SO_TARGET): $(TARGET) $(OBJECTS)
$(CC) -shared -o $@ $(OBJECTS)
-$(PROGRAMS): LDLIBS += $(TARGET) -lm
+$(PROGRAMS): LDLIBS += $(TARGET) -lm -L/usr/local/lib -lgdbm
build:
@mkdir -p build
@@ -36,7 +36,7 @@ build:
# The Unit Tests
.PHONY: tests
-tests: LDLIBS += $(TARGET) -lm
+tests: LDLIBS += $(TARGET) -lm -L/usr/local/lib -lgdbm
tests: $(TESTS)
sh ./tests/runtests.sh
@@ -46,6 +46,7 @@ clean:
rm -f tests/tests.log
find . -name "*.gc" -exec rm {} \;
rm -rf `find . -name "*.dSYM" -print`
+ rm -rf nserver.db
# The Install
install: all
diff --git a/nserver/src/db.c b/nserver/src/db.c
new file mode 100644
index 0000000..315bf7b
--- /dev/null
+++ b/nserver/src/db.c
@@ -0,0 +1,20 @@
+#include <db.h>
+
+
+static const char *DB_FILE = "nserver.db";
+
+int db_init()
+{
+ // Create DB if it's not already created.
+ GDBM_FILE gf = gdbm_open(DB_FILE, 0, GDBM_WRCREAT,
+ S_IRUSR|S_IWUSR, NULL);
+ check(gf != NULL, "unable to init db");
+
+ // Close the DB.
+ int rc = gdbm_close(gf);
+ check(rc == 0, "error closing db after init");
+
+ return 0;
+ error:
+ return -1;
+}
diff --git a/nserver/src/db.h b/nserver/src/db.h
new file mode 100644
index 0000000..433f812
--- /dev/null
+++ b/nserver/src/db.h
@@ -0,0 +1,11 @@
+#ifndef _db_h
+#define _db_h
+
+#include <gdbm.h>
+#include <dbg.h>
+#include <sys/stat.h>
+
+int db_init();
+
+#endif
+
diff --git a/nserver/tests/db_tests.c b/nserver/tests/db_tests.c
new file mode 100644
index 0000000..f97f114
--- /dev/null
+++ b/nserver/tests/db_tests.c
@@ -0,0 +1,21 @@
+#include "minunit.h"
+#include <db.h>
+
+char *test_db_init()
+{
+ int rc = db_init();
+ mu_assert(rc == 0, "db init failed");
+
+ return NULL;
+}
+
+char *all_tests()
+{
+ mu_suite_start();
+
+ mu_run_test(test_db_init);
+
+ return NULL;
+}
+
+RUN_TESTS(all_tests);