cctools
sort_dir.h File Reference

Go to the source code of this file.

Functions

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

Detailed Description

Obtain a sorted directory listing. The prototype of 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);

Function Documentation

◆ sort_dir()

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.

◆ sort_dir_free()

void sort_dir_free ( char **  list)

Free a sorted directory listing.

Parameters
listThe list to be freed.