C from Rust: Reference C programs
authorMartin Pitt <martin@piware.de>
Fri, 27 Aug 2021 05:09:50 +0000 (07:09 +0200)
committerMartin Pitt <martin@piware.de>
Fri, 27 Aug 2021 05:58:07 +0000 (07:58 +0200)
.gitignore
call-c-from-rust/Makefile [new file with mode: 0644]
call-c-from-rust/c-langinfo.c [new file with mode: 0644]
call-c-from-rust/c-mounts.c [new file with mode: 0644]

index fa8d85ac52f19959d6fc9942c265708b4b3c2b04..5c030314fde38609d0fa5ac3ffdedca12d63adae 100644 (file)
@@ -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 (file)
index 0000000..583e2f2
--- /dev/null
@@ -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 (file)
index 0000000..dacb763
--- /dev/null
@@ -0,0 +1,12 @@
+#include <langinfo.h>
+#include <locale.h>
+#include <stdio.h>
+
+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 (file)
index 0000000..0896239
--- /dev/null
@@ -0,0 +1,31 @@
+#include <stdio.h>
+#include <err.h>
+#include <string.h>
+#include <libmount.h>
+
+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;
+}