commit 070aed7ca1012bdc50a705005a19c01f842f72d9
parent f8cd81701c9ca4b161748ea198c9e5e73bd51895
Author: Henry Wilson <henry@henryandlizzy.uk>
Date: Fri, 26 Aug 2022 23:41:16 +0100
glob: add glob example
Hopefully this is similar to the UNIX v6 glob command
Diffstat:
1 file changed, 28 insertions(+), 0 deletions(-)
diff --git a/src/glob.c b/src/glob.c
@@ -0,0 +1,28 @@
+#include <glob.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static int cmpstringp(const void *p1, const void *p2)
+{
+ return strcmp(* (char * const *) p1, * (char * const *) p2);
+}
+
+int main(int argc, char* argv[])
+{
+ int flags = GLOB_NOSORT;
+ glob_t results = {0};
+
+ for (int i = 1; i < argc; ++i)
+ {
+ puts(argv[i]);
+ glob(argv[i], flags, NULL, &results);
+ flags |= GLOB_APPEND;
+ }
+
+ qsort(results.gl_pathv, results.gl_pathc, sizeof(char *), cmpstringp);
+
+ for (size_t i = 0; i < results.gl_pathc; ++i)
+ puts(results.gl_pathv[i]);
+
+}