Directory search?
As sure as I am that this was discussed before, I''m having trouble finding it on these boards: What''s a nice portable way to get directory file listings? Win32 provides the _findfirst(), _findnext(), and _findclose() functions. Is there an equivalent (or better, a standard library routine that does the same thing) under linux?
char **GetDirEntNames(const char *parent)
{
#ifdef __MSW__
long ffh; /* Find file handle. */
struct _finddata_t d; /* Find data return structure. */
char prev_cwd[PATH_MAX];
/* Record previous current working dir. */
getcwd(prev_cwd, PATH_MAX);
prev_cwd[PATH_MAX - 1] = ''\0'';
/* Change to parent dir. */
chdir(parent);
/* Find first file in specified directory */
ffh = _findfirst("*", &d);
if(ffh == -1L)
{
return(NULL);
}
else
{
/* Found first file in directory. */
int i = 0;
char **rtn_names = (char **)malloc(sizeof(char *));
if(rtn_names == NULL)
{
_findclose(ffh);
return(NULL);
}
rtn_names = StringCopyAlloc(d.name);
i++;
/* Find additional (if any) files in directory. */
while(_findnext(ffh, &d) == 0)
{
rtn_names = (char **)realloc(rtn_names, (i + 1) * sizeof(char *)
);
if(rtn_names == NULL)
{
_findclose(ffh);
return(NULL);
}
rtn_names = StringCopyAlloc(d.name);<br> i++;<br> }<br><br> /* Close find file handle and its resources. */<br> _findclose(ffh);<br><br> /* Allocate last pointer to be NULL. */<br> rtn_names = (char **)realloc(rtn_names, (i + 1) * sizeof(char *));<br> if(rtn_names != NULL)<br> {<br> rtn_names = NULL;<br> }<br><br> return(rtn_names);<br> }<br>#else<br> int i;<br> DIR *dir;<br> struct dirent *de;<br> char **rtn_names;<br><br><br> if(parent == NULL) <br> return(NULL);<br><br> /* Open dir. */<br> dir = opendir(parent);<br> if(dir == NULL)<br> return(NULL);<br><br><br> /* Allocate rtn_names pointers. */<br> rtn_names = NULL;<br> <br> for(i = 0; 1; i++)<br> {<br> /* Reallocate rtn_names pointers. */<br> rtn_names = (char **)realloc(<br> rtn_names,<br> (i + 1) * sizeof(char *)<br> );<br> if(rtn_names == NULL)<br> {<br> closedir(dir);<br> dir = NULL; <br> return(NULL);<br> }<br><br> /* Get next dir entry. */<br> de = (struct dirent *)readdir(dir);<br> if(de == NULL)<br> break;<br><br> rtn_names = StringCopyAlloc(de->d_name);<br> if(rtn_names == NULL)<br> break;<br> }<br><br> closedir(dir);<br> dir = NULL;<br><br> /* Set last entry to NULL. */<br> rtn_names = NULL;<br><br> return(rtn_names);<br>#endif /* NOT __MSW__ */<br>}<br> </i>
{
#ifdef __MSW__
long ffh; /* Find file handle. */
struct _finddata_t d; /* Find data return structure. */
char prev_cwd[PATH_MAX];
/* Record previous current working dir. */
getcwd(prev_cwd, PATH_MAX);
prev_cwd[PATH_MAX - 1] = ''\0'';
/* Change to parent dir. */
chdir(parent);
/* Find first file in specified directory */
ffh = _findfirst("*", &d);
if(ffh == -1L)
{
return(NULL);
}
else
{
/* Found first file in directory. */
int i = 0;
char **rtn_names = (char **)malloc(sizeof(char *));
if(rtn_names == NULL)
{
_findclose(ffh);
return(NULL);
}
rtn_names = StringCopyAlloc(d.name);
i++;
/* Find additional (if any) files in directory. */
while(_findnext(ffh, &d) == 0)
{
rtn_names = (char **)realloc(rtn_names, (i + 1) * sizeof(char *)
);
if(rtn_names == NULL)
{
_findclose(ffh);
return(NULL);
}
rtn_names = StringCopyAlloc(d.name);<br> i++;<br> }<br><br> /* Close find file handle and its resources. */<br> _findclose(ffh);<br><br> /* Allocate last pointer to be NULL. */<br> rtn_names = (char **)realloc(rtn_names, (i + 1) * sizeof(char *));<br> if(rtn_names != NULL)<br> {<br> rtn_names = NULL;<br> }<br><br> return(rtn_names);<br> }<br>#else<br> int i;<br> DIR *dir;<br> struct dirent *de;<br> char **rtn_names;<br><br><br> if(parent == NULL) <br> return(NULL);<br><br> /* Open dir. */<br> dir = opendir(parent);<br> if(dir == NULL)<br> return(NULL);<br><br><br> /* Allocate rtn_names pointers. */<br> rtn_names = NULL;<br> <br> for(i = 0; 1; i++)<br> {<br> /* Reallocate rtn_names pointers. */<br> rtn_names = (char **)realloc(<br> rtn_names,<br> (i + 1) * sizeof(char *)<br> );<br> if(rtn_names == NULL)<br> {<br> closedir(dir);<br> dir = NULL; <br> return(NULL);<br> }<br><br> /* Get next dir entry. */<br> de = (struct dirent *)readdir(dir);<br> if(de == NULL)<br> break;<br><br> rtn_names = StringCopyAlloc(de->d_name);<br> if(rtn_names == NULL)<br> break;<br> }<br><br> closedir(dir);<br> dir = NULL;<br><br> /* Set last entry to NULL. */<br> rtn_names = NULL;<br><br> return(rtn_names);<br>#endif /* NOT __MSW__ */<br>}<br> </i>
Tara Milana - WP Entertainmenthttp://wolfpack.twu.net/Comp graphics artist and programmer.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement