summaryrefslogtreecommitdiff
path: root/contrib/getdents_c
diff options
context:
space:
mode:
authorJakob Unterwurzacher2020-05-24 23:28:23 +0200
committerJakob Unterwurzacher2020-05-24 23:29:59 +0200
commitb3350f0ebbd6cf4215b85a6c126595b822cb0bc0 (patch)
treebcb91b4037e8f35043988da1c4f8e4dcd1f8b49a /contrib/getdents_c
parentc7a9425e1b153b53bb1b6af6d6aabba6dea0b2c6 (diff)
contrib: add getdents_c
Same thing like contrib/getdents, but written in C.
Diffstat (limited to 'contrib/getdents_c')
-rw-r--r--contrib/getdents_c/.gitignore1
-rw-r--r--contrib/getdents_c/Makefile2
-rw-r--r--contrib/getdents_c/getdents.c39
3 files changed, 42 insertions, 0 deletions
diff --git a/contrib/getdents_c/.gitignore b/contrib/getdents_c/.gitignore
new file mode 100644
index 0000000..2f94993
--- /dev/null
+++ b/contrib/getdents_c/.gitignore
@@ -0,0 +1 @@
+/getdents_c
diff --git a/contrib/getdents_c/Makefile b/contrib/getdents_c/Makefile
new file mode 100644
index 0000000..95e47dc
--- /dev/null
+++ b/contrib/getdents_c/Makefile
@@ -0,0 +1,2 @@
+getdents_c: *.c Makefile
+ gcc getdents.c -o getdents_c
diff --git a/contrib/getdents_c/getdents.c b/contrib/getdents_c/getdents.c
new file mode 100644
index 0000000..98c2346
--- /dev/null
+++ b/contrib/getdents_c/getdents.c
@@ -0,0 +1,39 @@
+// See ../getdents/getdents.go for some info on why
+// this exists.
+
+#include <fcntl.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <errno.h>
+
+int main(int argc, char *argv[])
+{
+ if(argc < 2) {
+ printf("Usage: %s PATH\n", argv[0]);
+ printf("Run getdents(2) on PATH\n");
+ exit(1);
+ }
+
+ const char *path = argv[1];
+ int fd = open(path, O_RDONLY);
+ if (fd == -1) {
+ perror("open");
+ exit(1);
+ }
+
+ char tmp[10000];
+ int sum = 0;
+ for ( ; ; ) {
+ int n = syscall(SYS_getdents64, fd, tmp, sizeof(tmp));
+ printf("getdents64 fd%d: n=%d, errno=%d\n", fd, n, errno);
+ if (n <= 0) {
+ printf("total %d bytes\n", sum);
+ break;
+ }
+ sum += n;
+ }
+}