2008-08-06 11:15:24 -07:00
|
|
|
/* EINA - EFL data type library
|
|
|
|
* Copyright (C) 2007-2008 Jorge Luis Zapata Muga
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library;
|
|
|
|
* if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-08-28 00:46:42 -07:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2008-07-30 06:35:49 -07:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <string.h>
|
2008-08-18 01:18:16 -07:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2008-08-28 00:46:42 -07:00
|
|
|
#include <dirent.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_ALLOCA_H
|
|
|
|
# include <alloca.h>
|
|
|
|
#elif defined __GNUC__
|
|
|
|
# define alloca __builtin_alloca
|
|
|
|
#elif defined _AIX
|
|
|
|
# define alloca __alloca
|
|
|
|
#elif defined _MSC_VER
|
|
|
|
# include <malloc.h>
|
|
|
|
# define alloca _alloca
|
|
|
|
#else
|
|
|
|
# include <stddef.h>
|
|
|
|
# ifdef __cplusplus
|
|
|
|
extern "C"
|
|
|
|
# endif
|
|
|
|
void *alloca (size_t);
|
|
|
|
#endif
|
|
|
|
|
2008-07-30 06:35:49 -07:00
|
|
|
|
|
|
|
#include "eina_file.h"
|
2008-07-30 05:46:55 -07:00
|
|
|
#include "eina_private.h"
|
2008-08-08 22:47:15 -07:00
|
|
|
|
2008-08-28 00:46:42 -07:00
|
|
|
/*============================================================================*
|
|
|
|
* Global *
|
|
|
|
*============================================================================*/
|
|
|
|
|
2008-07-30 05:46:55 -07:00
|
|
|
/*============================================================================*
|
|
|
|
* API *
|
|
|
|
*============================================================================*/
|
2008-08-08 22:47:15 -07:00
|
|
|
|
2008-07-30 05:46:55 -07:00
|
|
|
/**
|
|
|
|
* Lis all files on the directory calling the function for every file found
|
2008-08-08 22:47:15 -07:00
|
|
|
* @param dir The directory name
|
2008-07-30 05:46:55 -07:00
|
|
|
* @param recursive Iterate recursively in the directory
|
2008-08-08 22:47:15 -07:00
|
|
|
* @param cb The callback to be called
|
|
|
|
* @param data The data to pass to the callback
|
2008-07-30 05:46:55 -07:00
|
|
|
*/
|
2008-08-18 01:18:16 -07:00
|
|
|
EAPI Eina_Bool
|
|
|
|
eina_file_dir_list(const char *dir, Eina_Bool recursive, Eina_File_Dir_List_Cb cb, void *data)
|
2008-07-30 05:46:55 -07:00
|
|
|
{
|
|
|
|
struct dirent *de;
|
|
|
|
DIR *d;
|
|
|
|
|
2008-08-18 01:18:16 -07:00
|
|
|
if (!cb) return EINA_FALSE;
|
|
|
|
|
2008-07-30 05:46:55 -07:00
|
|
|
d = opendir(dir);
|
2008-08-18 01:18:16 -07:00
|
|
|
if (!d) return EINA_FALSE;
|
2008-07-30 05:46:55 -07:00
|
|
|
|
2008-07-30 06:35:49 -07:00
|
|
|
while ((de = readdir(d)))
|
2008-07-30 05:46:55 -07:00
|
|
|
{
|
|
|
|
if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
|
|
|
|
continue;
|
2008-08-18 01:18:16 -07:00
|
|
|
|
2008-07-30 05:46:55 -07:00
|
|
|
cb(de->d_name, dir, data);
|
|
|
|
/* d_type is only available on linux and bsd (_BSD_SOURCE) */
|
2008-08-18 01:18:16 -07:00
|
|
|
|
|
|
|
if (recursive == EINA_TRUE) {
|
|
|
|
char *path;
|
|
|
|
|
|
|
|
path = alloca(strlen(dir) + strlen(de->d_name) + 2);
|
|
|
|
strcpy(path, dir);
|
|
|
|
strcat(path, "/");
|
|
|
|
strcat(path, de->d_name);
|
|
|
|
|
|
|
|
if (de->d_type == DT_UNKNOWN) {
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (stat(path, &st))
|
|
|
|
continue ;
|
|
|
|
|
|
|
|
if (!S_ISDIR(st.st_mode))
|
|
|
|
continue ;
|
|
|
|
} else if (de->d_type != DT_DIR) {
|
|
|
|
continue ;
|
|
|
|
}
|
|
|
|
|
2008-07-30 05:46:55 -07:00
|
|
|
eina_file_dir_list(path, recursive, cb, data);
|
|
|
|
}
|
|
|
|
}
|
2008-08-18 01:18:16 -07:00
|
|
|
|
2008-07-30 05:46:55 -07:00
|
|
|
closedir(d);
|
|
|
|
|
2008-08-18 01:18:16 -07:00
|
|
|
return EINA_TRUE;
|
|
|
|
}
|