summaryrefslogtreecommitdiffstats
path: root/openpgp-ffi
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2019-03-22 16:19:39 +0100
committerJustus Winter <justus@sequoia-pgp.org>2019-03-22 16:19:39 +0100
commit6a9e2743d149fecd5d7706b273dc69a45fe25ea2 (patch)
treef1d3d40d578467416659d96cee6a2a68b9c31889 /openpgp-ffi
parent1d35557a8f7866141f84b2764287f384a8e12872 (diff)
openpgp-ffi: Replace glibc's error function.
- To make the tests and examples more portable, provide our own roughly compatible replacement for glibc's error(3).
Diffstat (limited to 'openpgp-ffi')
-rw-r--r--openpgp-ffi/examples/armor.c10
-rw-r--r--openpgp-ffi/examples/decrypt-with.c11
-rw-r--r--openpgp-ffi/examples/encrypt-for.c12
-rw-r--r--openpgp-ffi/examples/example.c11
-rw-r--r--openpgp-ffi/examples/immutable-reference-demo.c10
-rw-r--r--openpgp-ffi/examples/parser.c12
-rw-r--r--openpgp-ffi/examples/reader.c11
-rw-r--r--openpgp-ffi/examples/type-safety-demo.c10
-rw-r--r--openpgp-ffi/examples/use-after-free-demo.c10
-rw-r--r--openpgp-ffi/tests/c-tests.rs28
10 files changed, 110 insertions, 15 deletions
diff --git a/openpgp-ffi/examples/armor.c b/openpgp-ffi/examples/armor.c
index 42395811..49ed533c 100644
--- a/openpgp-ffi/examples/armor.c
+++ b/openpgp-ffi/examples/armor.c
@@ -1,6 +1,14 @@
#define _GNU_SOURCE
#include <assert.h>
-#include <error.h>
+/* Roughly glibc compatible error reporting. */
+#define error(S, E, F, ...) do { \
+ fprintf (stderr, (F), __VA_ARGS__); \
+ int s = (S), e = (E); \
+ if (e) { fprintf (stderr, ": %s", strerror (e)); } \
+ fprintf (stderr, "\n"); \
+ fflush (stderr); \
+ if (s) { exit (s); } \
+ } while (0)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
diff --git a/openpgp-ffi/examples/decrypt-with.c b/openpgp-ffi/examples/decrypt-with.c
index ae5e871d..d30b2d52 100644
--- a/openpgp-ffi/examples/decrypt-with.c
+++ b/openpgp-ffi/examples/decrypt-with.c
@@ -3,10 +3,19 @@
#define _GNU_SOURCE
#include <assert.h>
-#include <error.h>
+/* Roughly glibc compatible error reporting. */
+#define error(S, E, F, ...) do { \
+ fprintf (stderr, (F), __VA_ARGS__); \
+ int s = (S), e = (E); \
+ if (e) { fprintf (stderr, ": %s", strerror (e)); } \
+ fprintf (stderr, "\n"); \
+ fflush (stderr); \
+ if (s) { exit (s); } \
+ } while (0)
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <sequoia/openpgp.h>
diff --git a/openpgp-ffi/examples/encrypt-for.c b/openpgp-ffi/examples/encrypt-for.c
index 4a92d023..de2abd64 100644
--- a/openpgp-ffi/examples/encrypt-for.c
+++ b/openpgp-ffi/examples/encrypt-for.c
@@ -2,9 +2,19 @@
encrypt a file. */
#define _GNU_SOURCE
-#include <error.h>
+/* Roughly glibc compatible error reporting. */
+#define error(S, E, F, ...) do { \
+ fprintf (stderr, (F), __VA_ARGS__); \
+ int s = (S), e = (E); \
+ if (e) { fprintf (stderr, ": %s", strerror (e)); } \
+ fprintf (stderr, "\n"); \
+ fflush (stderr); \
+ if (s) { exit (s); } \
+ } while (0)
#include <errno.h>
#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
#include <sequoia/openpgp.h>
diff --git a/openpgp-ffi/examples/example.c b/openpgp-ffi/examples/example.c
index 46a45529..3c561761 100644
--- a/openpgp-ffi/examples/example.c
+++ b/openpgp-ffi/examples/example.c
@@ -1,8 +1,17 @@
#define _GNU_SOURCE
#include <errno.h>
-#include <error.h>
+/* Roughly glibc compatible error reporting. */
+#define error(S, E, F, ...) do { \
+ fprintf (stderr, (F), __VA_ARGS__); \
+ int s = (S), e = (E); \
+ if (e) { fprintf (stderr, ": %s", strerror (e)); } \
+ fprintf (stderr, "\n"); \
+ fflush (stderr); \
+ if (s) { exit (s); } \
+ } while (0)
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <sequoia/openpgp.h>
diff --git a/openpgp-ffi/examples/immutable-reference-demo.c b/openpgp-ffi/examples/immutable-reference-demo.c
index a0ada27b..47efbdd6 100644
--- a/openpgp-ffi/examples/immutable-reference-demo.c
+++ b/openpgp-ffi/examples/immutable-reference-demo.c
@@ -1,6 +1,14 @@
#define _GNU_SOURCE
#include <errno.h>
-#include <error.h>
+/* Roughly glibc compatible error reporting. */
+#define error(S, E, F, ...) do { \
+ fprintf (stderr, (F), __VA_ARGS__); \
+ int s = (S), e = (E); \
+ if (e) { fprintf (stderr, ": %s", strerror (e)); } \
+ fprintf (stderr, "\n"); \
+ fflush (stderr); \
+ if (s) { exit (s); } \
+ } while (0)
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
diff --git a/openpgp-ffi/examples/parser.c b/openpgp-ffi/examples/parser.c
index 5ded2de2..dc404fec 100644
--- a/openpgp-ffi/examples/parser.c
+++ b/openpgp-ffi/examples/parser.c
@@ -2,10 +2,20 @@
* also serves as a simple benchmark. */
#define _GNU_SOURCE
-#include <error.h>
+/* Roughly glibc compatible error reporting. */
+#define error(S, E, F, ...) do { \
+ fprintf (stderr, (F), __VA_ARGS__); \
+ int s = (S), e = (E); \
+ if (e) { fprintf (stderr, ": %s", strerror (e)); } \
+ fprintf (stderr, "\n"); \
+ fflush (stderr); \
+ if (s) { exit (s); } \
+ } while (0)
#include <errno.h>
#include <stdio.h>
#include <time.h>
+#include <string.h>
+#include <stdlib.h>
#include <sequoia/openpgp.h>
diff --git a/openpgp-ffi/examples/reader.c b/openpgp-ffi/examples/reader.c
index b0273ebb..13c47f69 100644
--- a/openpgp-ffi/examples/reader.c
+++ b/openpgp-ffi/examples/reader.c
@@ -1,8 +1,17 @@
#define _GNU_SOURCE
-#include <error.h>
+/* Roughly glibc compatible error reporting. */
+#define error(S, E, F, ...) do { \
+ fprintf (stderr, (F), __VA_ARGS__); \
+ int s = (S), e = (E); \
+ if (e) { fprintf (stderr, ": %s", strerror (e)); } \
+ fprintf (stderr, "\n"); \
+ fflush (stderr); \
+ if (s) { exit (s); } \
+ } while (0)
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <sequoia/openpgp.h>
diff --git a/openpgp-ffi/examples/type-safety-demo.c b/openpgp-ffi/examples/type-safety-demo.c
index ad4870e2..d2bc523e 100644
--- a/openpgp-ffi/examples/type-safety-demo.c
+++ b/openpgp-ffi/examples/type-safety-demo.c
@@ -1,6 +1,14 @@
#define _GNU_SOURCE
#include <errno.h>
-#include <error.h>
+/* Roughly glibc compatible error reporting. */
+#define error(S, E, F, ...) do { \
+ fprintf (stderr, (F), __VA_ARGS__); \
+ int s = (S), e = (E); \
+ if (e) { fprintf (stderr, ": %s", strerror (e)); } \
+ fprintf (stderr, "\n"); \
+ fflush (stderr); \
+ if (s) { exit (s); } \
+ } while (0)
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
diff --git a/openpgp-ffi/examples/use-after-free-demo.c b/openpgp-ffi/examples/use-after-free-demo.c
index e64a3c1f..30863c81 100644
--- a/openpgp-ffi/examples/use-after-free-demo.c
+++ b/openpgp-ffi/examples/use-after-free-demo.c
@@ -1,6 +1,14 @@
#define _GNU_SOURCE
#include <errno.h>
-#include <error.h>
+/* Roughly glibc compatible error reporting. */
+#define error(S, E, F, ...) do { \
+ fprintf (stderr, (F), __VA_ARGS__); \
+ int s = (S), e = (E); \
+ if (e) { fprintf (stderr, ": %s", strerror (e)); } \
+ fprintf (stderr, "\n"); \
+ fflush (stderr); \
+ if (s) { exit (s); } \
+ } while (0)
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
diff --git a/openpgp-ffi/tests/c-tests.rs b/openpgp-ffi/tests/c-tests.rs
index 37c00231..6a2d04e0 100644
--- a/openpgp-ffi/tests/c-tests.rs
+++ b/openpgp-ffi/tests/c-tests.rs
@@ -262,9 +262,7 @@ fn run(ldpath: &Path, exe: &Path) -> io::Result<()> {
/// Wraps the code in a main function if none exists.
fn wrap_with_main(test: &mut Vec<String>, offset: usize) {
- if has_main(test) {
- return;
- }
+ let needs_wrapping = ! has_main(test);
let mut last_include = 0;
for (n, line) in test.iter().enumerate() {
@@ -273,10 +271,28 @@ fn wrap_with_main(test: &mut Vec<String>, offset: usize) {
}
}
- test.insert(last_include + 1, "int main() {".into());
- test.insert(last_include + 2, format!("#line {}", last_include + 1 + offset));
+ test.insert(last_include + 1,
+ "#define error(S, E, F, ...) do { \\\n\
+ fprintf (stderr, (F), __VA_ARGS__); \\\n\
+ int s = (S), e = (E); \\\n\
+ if (e) { fprintf (stderr, \": %s\", strerror (e)); } \\\n\
+ fprintf (stderr, \"\\n\"); \\\n\
+ fflush (stderr); \\\n\
+ if (s) { exit (s); } \\\n\
+ } while (0)".into());
+
+ if needs_wrapping {
+ test.insert(last_include + 1, "int main() {".into());
+ }
+ test.insert(last_include + 2, format!("#line {}", last_include + offset
+ + if needs_wrapping {1} else {0}));
let last = test.len();
- test.insert(last, "}".into());
+ if needs_wrapping {
+ test.insert(last, "}".into());
+ }
+ test.insert(0, "#include <string.h>".into());
+ test.insert(0, "#include <stdlib.h>".into());
+ test.insert(0, "#include <stdio.h>".into());
test.insert(0, "#define _GNU_SOURCE".into());
}