libite/dir.c
/**
 * dir - List all files of a certain type in the given directory.
 * @dir:   Base directory for dir operation.
 * @type:  File type suffix, e.g. ".cfg".
 * @filter: Optional file name filter.
 * @list:  Pointer to an array of file names.
 * @strip: Flag, if set dir() strips the file type.
 *
 * This function returns a @list of files, matching the @type suffix,
 * in the given directory @dir.
 *
 * The @list argument is a pointer to where to store the dynamically
 * allocated list of file names.  This list should be free'd by first
 * calling free() on each file name and then on the list itself.
 *
 * If @filter is not %NULL it will be called for each file found.  If
 * @filter returns non-zero the @file argument will be included in the
 * resulting @list.  If @filter returns zero for given @file it will
 * be discarded.
 *
 * If the @strip flag is set the resulting @list of files has their
 * file type stripped, including the dot.  So a match "config0.cfg"
 * would be returned as "config0".
 *
 * Returns:
 * Number of files in @list, zero if no matching files of @type.
 */
List all files of a certain type in the given directory.
@dir:   Base directory for dir operation.
@type:  File type suffix, e.g.

This function returns a @list of files, matching the @type suffix,
in the given directory @dir.

The @list argument is a pointer to where to store the dynamically
allocated list of file names.  This list should be free'd by first
calling free() on each file name and then on the list itself.

If @filter is not %NULL it will be called for each file found.  If
@filter returns non-zero the @file argument will be included in the
resulting @list.  If @filter returns zero for given @file it will
be discarded.

If the @strip flag is set the resulting @list of files has their
file type stripped, including the dot.  So a match "config0.cfg"
would be returned as "config0".

Number of files in @list, zero if no matching files of @type.
List all files of a certain type in the given directory . @dir : Base directory for dir operation . @type : File type suffix e . g . This function returns a @list of files matching the @type suffix in the given directory @dir . The @list argument is a pointer to where to store the dynamically allocated list of file names . This list should be free ' d by first calling free () on each file name and then on the list itself . If @filter is not %NULL it will be called for each file found . If @filter returns non - zero the @file argument will be included in the resulting @list . If @filter returns zero for given @file it will be discarded . If the @strip flag is set the resulting @list of files has their file type stripped including the dot . So a match " config0 . cfg " would be returned as " config0 " . Number of files in @list zero if no matching files of @type .
int dir(const char *dir, const char *type, int (*filter) (const char *file), char ***list, int strip)
{
	int i, n, num = 0;
	char **files;
	struct dirent **namelist;
	assert(list);
	if (!dir)
		dir = ".";
	if (!type)
		type = "";
	matcher_type = type;
	matcher_filter = filter;
	n = scandir(dir, &namelist, matcher, alphasort);
	if (n < 0) {
		perror("scandir");
	} else if (n > 0) {
		files = (char **)malloc(n * sizeof(char *));
		for (i = 0; i < n; i++) {
			if (files) {
				char *name = namelist[i]->d_name;
				char *type = strrchr(name, '.');
				if (type && strip)
					*type = 0;
				files[i] = strdup(name);
				num++;
			}
			free(namelist[i]);
		}
		if (num)
			*list = files;
	}
	if (namelist)
		free(namelist);
	return num;
}
int dir ( const char * dir , const char * type , int ( * filter ) ( const char * file ) , char * * * list , int strip ) { int i , n , num = 0 ; char * * files ; struct dirent * * namelist ; assert ( list ) ; if ( ! dir ) dir = " " ; if ( ! type ) type = " " ; matcher_type = type ; matcher_filter = filter ; n = scandir ( dir , & namelist , matcher , alphasort ) ; if ( n < 0 ) { perror ( " " ) ; } else if ( n > 0 ) { files = ( char * * ) malloc ( n * sizeof ( char * ) ) ; for ( i = 0 ; i < n ; i ++ ) { if ( files ) { char * name = namelist [ i ] -> d_name ; char * type = strrchr ( name , ' ' ) ; if ( type && strip ) * type = 0 ; files [ i ] = strdup ( name ) ; num ++ ; } free ( namelist [ i ] ) ; } if ( num ) * list = files ; } if ( namelist ) free ( namelist ) ; return num ; }
dir - List all files of a certain type in the given directory.
dir - List all files of a certain type in the given directory .
/* Assuming current directory */ /* Assuming all files. */