examples

Toy examples in single C files.
git clone git://henryandlizzy.uk/examples
Log | Files | Refs

glob.c (763B)


      1 #include <glob.h>
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 #include <string.h>
      5 
      6 int errfunc(char const* epath, int eerrno)
      7 {
      8 	fprintf(stderr, "glob: %s %s\n", epath, strerror(eerrno));
      9 	return 0;
     10 }
     11 
     12 static int cmpstringp(const void *p1, const void *p2)
     13 {
     14    return strcmp(* (char * const *) p1, * (char * const *) p2);
     15 }
     16 
     17 int main(int argc, char* argv[])
     18 {
     19     int flags = GLOB_NOSORT;
     20     glob_t results = {0};
     21 
     22     if (argc < 2)
     23         return 0;
     24 
     25     for (int i = 1; i < argc; ++i)
     26     {
     27         glob(argv[i], flags, errfunc, &results);
     28         flags |= GLOB_APPEND;
     29     }
     30 
     31     qsort(results.gl_pathv, results.gl_pathc, sizeof(char *), cmpstringp);
     32 
     33     for (size_t i = 0; i < results.gl_pathc; ++i)
     34         puts(results.gl_pathv[i]);
     35 
     36     globfree(&results);
     37 }