diff options
author | Justus Winter <justus@pep-project.org> | 2017-11-23 14:34:45 +0100 |
---|---|---|
committer | Justus Winter <justus@pep-project.org> | 2017-11-23 14:36:36 +0100 |
commit | 9a5725fe55eb5e49cee09712364ac7b7dcefde51 (patch) | |
tree | 4f0b3b5e86a02236a06c503e8adcc03fc6fc1d7e /examples | |
parent | 8cdab3036cfd2111fd3719abc38ab334bc72ebdc (diff) |
Add a foreign function interface.
- For now, we keep the ffi in this crate, later on we may want to
move it to sequoia-ffi.
- Example code how to use the library from C is added as well.
Diffstat (limited to 'examples')
-rw-r--r-- | examples/Makefile | 3 | ||||
-rw-r--r-- | examples/example.c | 44 |
2 files changed, 47 insertions, 0 deletions
diff --git a/examples/Makefile b/examples/Makefile new file mode 100644 index 00000000..59007930 --- /dev/null +++ b/examples/Makefile @@ -0,0 +1,3 @@ +CFLAGS = -I../src -O0 -g -Wall +LDFLAGS = -L../target/debug -lsequoia +all: example diff --git a/examples/example.c b/examples/example.c new file mode 100644 index 00000000..ae727b43 --- /dev/null +++ b/examples/example.c @@ -0,0 +1,44 @@ +#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_tpk *tpk; + + if (argc != 2) + error (1, 0, "Usage: %s <file>", argv[0]); + + 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); + munmap (b, st.st_size); + close (fd); + return 0; +} |