From 225c6b2420e01dca1f3a570d3ed664b825cf883c Mon Sep 17 00:00:00 2001 From: Martin Pitt Date: Fri, 27 Aug 2021 07:09:50 +0200 Subject: [PATCH] C from Rust: Reference C programs --- .gitignore | 5 +++++ call-c-from-rust/Makefile | 21 +++++++++++++++++++++ call-c-from-rust/c-langinfo.c | 12 ++++++++++++ call-c-from-rust/c-mounts.c | 31 +++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+) create mode 100644 call-c-from-rust/Makefile create mode 100644 call-c-from-rust/c-langinfo.c create mode 100644 call-c-from-rust/c-mounts.c diff --git a/.gitignore b/.gitignore index fa8d85a..5c03031 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,7 @@ +*.o + Cargo.lock target + +c-langinfo +c-mounts diff --git a/call-c-from-rust/Makefile b/call-c-from-rust/Makefile new file mode 100644 index 0000000..583e2f2 --- /dev/null +++ b/call-c-from-rust/Makefile @@ -0,0 +1,21 @@ +CFLAGS += $(shell pkg-config --cflags mount) +LIBMOUNT = $(shell pkg-config --libs mount) + +all: c-mounts c-langinfo + +c-mounts: c-mounts.o + $(CC) -Wall -o $@ $^ $(LIBMOUNT) + +c-langinfo: c-langinfo.o + $(CC) -Wall -o $@ $^ + +clean: + rm -f c-mounts c-langinfo *.o + +run: all + LANG=en_US.UTF-8 ./c-langinfo + LANG=en_GB.UTF-8 ./c-langinfo + LANG=en_IE.UTF-8 ./c-langinfo + ./c-mounts + +.PHONY: clean run diff --git a/call-c-from-rust/c-langinfo.c b/call-c-from-rust/c-langinfo.c new file mode 100644 index 0000000..dacb763 --- /dev/null +++ b/call-c-from-rust/c-langinfo.c @@ -0,0 +1,12 @@ +#include +#include +#include + +int main() +{ + printf("locale: %s\n", setlocale (LC_ALL, "")); + printf("codeset: %s\n", nl_langinfo(CODESET)); + printf("date/time format: %s\n", nl_langinfo(D_T_FMT)); + printf("currency: %s\n", nl_langinfo(CRNCYSTR)); + return 0; +} diff --git a/call-c-from-rust/c-mounts.c b/call-c-from-rust/c-mounts.c new file mode 100644 index 0000000..0896239 --- /dev/null +++ b/call-c-from-rust/c-mounts.c @@ -0,0 +1,31 @@ +#include +#include +#include +#include + +int +main() +{ + int r; + const char *version; + + r = mnt_get_library_version(&version); + if (r < 0) + errx(1, "failed to get lib versino: %s", strerror (-r)); + printf("libmount version: %s\n", version); + + struct libmnt_table *table = mnt_new_table(); + r = mnt_table_parse_mtab (table, NULL); + if (r < 0) + errx(1, "failed to parse mtab: %s", strerror (-r)); + + struct libmnt_fs *fs; + r = mnt_table_first_fs(table, &fs); + if (r < 0) + errx(1, "failed to find first fs: %s", strerror (-r)); + printf("first fs type: %s source: %s target: %s\n", mnt_fs_get_fstype(fs), mnt_fs_get_source(fs), mnt_fs_get_target(fs)); + mnt_free_fs(fs); + + mnt_free_table(table); + return 0; +} -- 2.39.2