cctools
Macros | Functions
sort_dir.h File Reference

Obtain a sorted directory listing. More...

Go to the source code of this file.

Macros

#define sort_dir_free   free
 Free a sorted directory listing. More...
 

Functions

int sort_dir (const char *dirname, char ***list, int(*sort)(const char *a, const char *b))
 Obtain a sorted directory listing. More...
 

Detailed Description

Obtain a sorted directory listing.

    The prototype of @ref sort_dir is a little scary, but it is easy to use.

For example, to sort a given directory alphabetically:

char **list;
int i;
sort_dir(dirname,&list,strcmp);
for(i=0;list[i];i++) printf("%s\n",list[i]);
sort_dir_free(list);

Macro Definition Documentation

#define sort_dir_free   free

Free a sorted directory listing.

Parameters
listThe list to be freed.

Function Documentation

int sort_dir ( const char *  dirname,
char ***  list,
int(*)(const char *a, const char *b)  sort 
)

Obtain a sorted directory listing.

Parameters
dirnameThe directory to list.
listA pointer to a doubly-indirect pointer, which will be filled with a list of strings. The final item will be null. This list must be freed with sort_dir_free.
sortA pointer to a function to compare two strings, which must have the same semantics as strcmp
Returns
True on success, false on failure, setting errno appropriately.