summaryrefslogtreecommitdiffstats
path: root/src/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util.c')
-rw-r--r--src/util.c106
1 files changed, 106 insertions, 0 deletions
diff --git a/src/util.c b/src/util.c
new file mode 100644
index 0000000..91954dd
--- /dev/null
+++ b/src/util.c
@@ -0,0 +1,106 @@
+#include <stdio.h>
+#include <assert.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <string.h>
+#include <errno.h>
+#include <limits.h>
+#include <unistd.h>
+#include "util.h"
+
+void statistics(DB *db) {
+ DB_BTREE_STAT *statp;
+ int ret;
+
+ assert(db);
+
+ if ((ret = db->stat(db, &statp, 0)) != 0) {
+ db->err(db, ret, "DB->stat");
+ return;
+ }
+
+ printf("Database contains %lu records\n", (long unsigned) statp->bt_ndata);
+ free(statp);
+}
+
+char* normalize_path(char *s) {
+ char *l, *p, *d;
+
+ // deletes /./ and //
+
+ if (*s == '/')
+ l = p = d = s+1;
+ else
+ l = p = d = s;
+
+ for (; *p; p++) {
+ if (*p == '/') {
+
+ if (l-p == 0) {
+ l++;
+ continue;
+ }
+
+ if (p-l == 1 && *l == '.') {
+ l += 2;
+ continue;
+ }
+
+ while (l <= p)
+ *(d++) = *(l++);
+ }
+ }
+
+ while (l <= p)
+ *(d++) = *(l++);
+
+ return s;
+}
+
+void rotdash(void) {
+ static const char dashes[] = /* ".oOo"; */ "|/-\\";
+ const static char *d = dashes;
+
+ if (isatty(fileno(stderr))) {
+ fprintf(stderr, "%c\b", *d);
+
+ d++;
+ if (!*d)
+ d = dashes;
+ }
+}
+
+const char* get_attached_filename(const char *path, const char *fn) {
+ static char npath[PATH_MAX];
+ struct stat st;
+
+ if (stat(path, &st) < 0) {
+ if (errno == ENOENT)
+ return path;
+
+ fprintf(stderr, "stat(%s) failed: %s\n", path, strerror(errno));
+ return NULL;
+ }
+
+ if (S_ISREG(st.st_mode))
+ return path;
+
+ if (S_ISDIR(st.st_mode)) {
+ snprintf(npath, sizeof(npath), "%s/.syrep", path);
+ mkdir(npath, 0777);
+ snprintf(npath, sizeof(npath), "%s/.syrep/%s", path, fn);
+ return npath;
+ }
+
+ fprintf(stderr, "<%s> is not a valid syrep snapshot\n", path);
+ return NULL;
+}
+
+int isdirectory(const char *path) {
+ struct stat st;
+
+ if (stat(path, &st) < 0)
+ return -1;
+
+ return !!S_ISDIR(st.st_mode);
+}