source: abuse/trunk/src/sdlport/jdir.cpp @ 28

Last change on this file since 28 was 2, checked in by Sam Hocevar, 18 years ago
  • imported original 0.7.0 tarball
File size: 1.2 KB
RevLine 
[2]1#include <sys/types.h>
2#include <stdio.h>
3#include <dirent.h>
4#include <stdlib.h>
5#include <string.h>
6#include <unistd.h>
7#include "jmalloc.hpp"
8
9void get_directory(char *path, char **&files, int &tfiles, char **&dirs, int &tdirs)
10{
11        struct dirent *de;
12        files = NULL;
13        dirs = NULL;
14        tfiles = 0;
15        tdirs = 0;
16        DIR *d = opendir( path );
17
18        if( !d )
19                return;
20
21        char **tlist = NULL;
22        int t = 0;
23        char curdir[200];
24        getcwd( curdir, 200 );
25        chdir( path );
26
27        do
28        {
29                de = readdir( d );
30                if( de )
31                {
32                        t++;
33                        tlist = (char **)jrealloc(tlist,sizeof(char *)*t,"tmp file list");
34                        tlist[t-1] = strcpy((char *)jmalloc(strlen(de->d_name)+1,"tmp file name"),de->d_name);     
35                }
36        } while( de );
37        closedir( d );
38
39        for( int i=0; i < t; i++ )
40        {
41                d = opendir( tlist[i] );
42                if( d )
43                {
44                        tdirs++;
45                        dirs = (char **)jrealloc(dirs,sizeof(char *)*tdirs,"dir list");
46                        dirs[tdirs-1] = strcpy((char *)jmalloc(strlen(tlist[i])+1,"tmp file name"),tlist[i]);
47                        closedir( d );
48                }
49                else
50                {
51                        tfiles++;
52                        files = (char **)jrealloc(files,sizeof(char *)*tfiles,"dir list");
53                        files[tfiles-1] = strcpy((char *)jmalloc(strlen(tlist[i])+1,"tmp file name"),tlist[i]);
54                }
55                jfree( tlist[i] );
56        }
57        if( t )
58                jfree( tlist );
59        chdir( curdir );
60}
Note: See TracBrowser for help on using the repository browser.