summaryrefslogtreecommitdiffstats
path: root/ffi
diff options
context:
space:
mode:
authorJustus Winter <justus@pep-project.org>2017-12-14 11:26:28 +0100
committerJustus Winter <justus@pep-project.org>2017-12-14 12:12:16 +0100
commit42447a1bd58599643568b0e4885ca4fa4fe7aed9 (patch)
tree4d2574dadf4bdaba4edafe1a87ec36bcafa224af /ffi
parentf9ecc50244ec329461ae3b656551b1c2896a7539 (diff)
ffi: Move the examples.
- Move the 'example.c' to sequoia-ffi. - Fix and improve Makefile.
Diffstat (limited to 'ffi')
-rw-r--r--ffi/examples/Makefile12
-rw-r--r--ffi/examples/example.c50
2 files changed, 62 insertions, 0 deletions
diff --git a/ffi/examples/Makefile b/ffi/examples/Makefile
new file mode 100644
index 00000000..4f71c405
--- /dev/null
+++ b/ffi/examples/Makefile
@@ -0,0 +1,12 @@
+# Makefile for examples written in C.
+
+TARGETS = example dump keyserver
+CFLAGS = -I../src -O0 -g -Wall
+LDFLAGS = -L../../target/debug -lsequoia_ffi
+
+all: $(TARGETS)
+
+clean:
+ rm -f $(TARGETS)
+
+$(TARGETS): ../src/sequoia.h
diff --git a/ffi/examples/example.c b/ffi/examples/example.c
new file mode 100644
index 00000000..5b087c15
--- /dev/null
+++ b/ffi/examples/example.c
@@ -0,0 +1,50 @@
+#define _GNU_SOURCE
+#include <error.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <sequoia.h>
+
+int
+main (int argc, char **argv)
+{
+ struct stat st;
+ int fd;
+ char *b;
+ struct sq_context *ctx;
+ struct sq_tpk *tpk;
+
+ if (argc != 2)
+ error (1, 0, "Usage: %s <file>", argv[0]);
+
+ ctx = sq_context_new("org.sequoia-pgp.example");
+ if (ctx == NULL)
+ error (1, 0, "Initializing sequoia failed.");
+
+ if (stat (argv[1], &st))
+ error (1, errno, "%s", argv[1]);
+
+ fd = open (argv[1], O_RDONLY);
+ if (fd == -1)
+ error (1, errno, "%s", argv[1]);
+
+ b = mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
+ if (b == MAP_FAILED)
+ error (1, errno, "mmap");
+
+ tpk = sq_tpk_from_bytes (b, st.st_size);
+ if (tpk == NULL)
+ error (1, 0, "sq_tpk_from_bytes failed");
+
+ sq_tpk_dump (tpk);
+ sq_tpk_free (tpk);
+ sq_context_free (ctx);
+ munmap (b, st.st_size);
+ close (fd);
+ return 0;
+}