| version 1.2, 2003/05/20 15:25:27 |
version 1.3, 2003/05/20 19:11:48 |
|
|
| * |
* |
| */ |
*/ |
| |
|
| /* $Platon: games/3do-view/dir.c,v 1.1 2003/05/19 22:24:46 nepto Exp $ */ |
/* $Platon: games/3do-view/dir.c,v 1.2 2003/05/20 15:25:27 nepto Exp $ */ |
| |
|
| #include <string.h> |
#include <string.h> |
| #include <stdlib.h> |
#include <stdlib.h> |
| #include <unistd.h> |
#include <system.h> |
| #include <dirent.h> |
|
| #include <sys/types.h> |
|
| #include <sys/stat.h> |
|
| |
|
| #define DIR_REALLOC_STEP 10 |
#if PLATON_SYSTEM_LINUX |
| |
# include <unistd.h> |
| |
# include <dirent.h> |
| |
# include <sys/types.h> |
| |
# include <sys/stat.h> |
| |
# define DIR_DIRECTORY_CONDITION (S_ISDIR(st.st_mode)) |
| |
# define DIR_FILENAME (dp->d_name) |
| |
# define DIR_INIT_CONDITION (! ((dirp = opendir(".")) == NULL \ |
| |
&& !chdir("/") && (dirp = opendir(".")) == NULL)) |
| |
/* If current directory cannot be opened, fallback to root directory. */ |
| |
#endif |
| |
|
| |
#if PLATON_SYSTEM_MSDOS |
| |
# include <dos.h> |
| |
# include <dir.h> |
| |
# define DIR_DIRECTORY_CONDITION (ffblk.ff_attrib & FA_DIREC) |
| |
# define DIR_FILENAME (ffblk.ff_name) |
| |
# define DIR_INIT_CONDITION (! (findfirst("*.*", &ffblk, \ |
| |
FA_DIREC + FA_HIDDEN + FA_RDONLY + FA_SYSTEM))) |
| |
#endif |
| |
|
| |
#define DIR_REALLOC_STEP (10) |
| |
|
| void get_directories_and_files(char ***directories, char ***files) /* {{{ */ |
void get_directories_and_files(char ***directories, char ***files) /* {{{ */ |
| { |
{ |
| |
#if PLATON_SYSTEM_LINUX |
| DIR *dirp; |
DIR *dirp; |
| struct dirent *dp; |
struct dirent *dp; |
| struct stat st; |
struct stat st; |
| |
#endif |
| |
#if PLATON_SYSTEM_MSDOS |
| |
struct ffblk ffblk; |
| |
#endif |
| register char ***ptr; |
register char ***ptr; |
| long int directories_size, files_size, *size; |
int directories_size, files_size, *size; |
| |
|
| if (directories != NULL) *directories = NULL; |
if (directories != NULL) *directories = NULL; |
| if (files != NULL) *files = NULL; |
if (files != NULL) *files = NULL; |
| |
|
| /* If current directory cannot be opened, fallback to root directory. */ |
if (! DIR_INIT_CONDITION) |
| if ((dirp = opendir(".")) == NULL |
|
| && !chdir("/") && (dirp = opendir(".")) == NULL) |
|
| return; |
return; |
| |
|
| directories_size = 0; |
directories_size = 0; |
| files_size = 0; |
files_size = 0; |
| |
|
| |
#if PLATON_SYSTEM_LINUX |
| for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) { |
for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) { |
| if (stat(dp->d_name, &st)) |
if (stat(DIR_FILENAME, &st)) |
| continue; |
continue; |
| |
#endif |
| |
#if PLATON_SYSTEM_MSDOS |
| |
do { |
| |
#endif |
| |
|
| if (S_ISDIR(st.st_mode)) { |
if (DIR_DIRECTORY_CONDITION) { |
| if (directories == NULL || ! strcmp(dp->d_name, ".")) |
if (directories == NULL || ! strcmp(DIR_FILENAME, ".")) |
| continue; |
continue; |
| ptr = directories; |
ptr = directories; |
| size = &directories_size; |
size = &directories_size; |
| Line 57 void get_directories_and_files(char ***d |
|
| Line 84 void get_directories_and_files(char ***d |
|
| } |
} |
| if (! *size || !((*size + 1) % DIR_REALLOC_STEP)) { |
if (! *size || !((*size + 1) % DIR_REALLOC_STEP)) { |
| register char **new_ptr; |
register char **new_ptr; |
| new_ptr = (char **) realloc(*ptr, |
new_ptr = (char **) realloc((void *) *ptr, |
| sizeof(char *) * (*size + 1 + DIR_REALLOC_STEP)); |
sizeof(char *) * (*size + 1 + DIR_REALLOC_STEP)); |
| if (new_ptr == NULL) |
if (new_ptr == NULL) |
| break; |
break; |
| *ptr = new_ptr; |
*ptr = new_ptr; |
| } |
} |
| if (((*ptr)[*size] = strdup(dp->d_name)) == NULL) |
if (((*ptr)[*size] = strdup(DIR_FILENAME)) == NULL) |
| break; |
break; |
| (*size)++; |
(*size)++; |
| (*ptr)[*size] = NULL; |
(*ptr)[*size] = NULL; |
| |
#if PLATON_SYSTEM_LINUX |
| } |
} |
| |
|
| closedir(dirp); |
closedir(dirp); |
| |
#endif |
| |
#if PLATON_SYSTEM_MSDOS |
| |
} while (! findnext(&ffblk)); |
| |
#endif |
| } /* }}} */ |
} /* }}} */ |
| |
|
| void free_directories_or_files(char **ar) /* {{{ */ |
void free_directories_or_files(char **ar) /* {{{ */ |