1 | /* |
---|
2 | * Abuse - dark 2D side-scrolling platform game |
---|
3 | * Copyright (c) 2001 Anthony Kruize <trandor@labyrinth.net.au> |
---|
4 | * Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net> |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation; either version 2 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program; if not, write to the Free Software Foundation, |
---|
18 | * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. |
---|
19 | */ |
---|
20 | |
---|
21 | #if defined HAVE_CONFIG_H |
---|
22 | # include "config.h" |
---|
23 | #endif |
---|
24 | |
---|
25 | #include <stdio.h> |
---|
26 | #include <stdlib.h> |
---|
27 | #include <string.h> |
---|
28 | |
---|
29 | #include <sys/types.h> |
---|
30 | #include <dirent.h> |
---|
31 | #include <unistd.h> |
---|
32 | |
---|
33 | void get_directory(char *path, char **&files, int &tfiles, char **&dirs, int &tdirs) |
---|
34 | { |
---|
35 | struct dirent *de; |
---|
36 | files = NULL; |
---|
37 | dirs = NULL; |
---|
38 | tfiles = 0; |
---|
39 | tdirs = 0; |
---|
40 | DIR *d = opendir( path ); |
---|
41 | |
---|
42 | if( !d ) |
---|
43 | return; |
---|
44 | |
---|
45 | char **tlist = NULL; |
---|
46 | int t = 0; |
---|
47 | char curdir[200]; |
---|
48 | getcwd( curdir, 200 ); |
---|
49 | chdir( path ); |
---|
50 | |
---|
51 | do |
---|
52 | { |
---|
53 | de = readdir( d ); |
---|
54 | if( de ) |
---|
55 | { |
---|
56 | t++; |
---|
57 | tlist = (char **)realloc(tlist,sizeof(char *)*t); |
---|
58 | tlist[t-1] = strdup(de->d_name); |
---|
59 | } |
---|
60 | } while( de ); |
---|
61 | closedir( d ); |
---|
62 | |
---|
63 | for( int i=0; i < t; i++ ) |
---|
64 | { |
---|
65 | d = opendir( tlist[i] ); |
---|
66 | if( d ) |
---|
67 | { |
---|
68 | tdirs++; |
---|
69 | dirs = (char **)realloc(dirs,sizeof(char *)*tdirs); |
---|
70 | dirs[tdirs-1] = strdup(tlist[i]); |
---|
71 | closedir( d ); |
---|
72 | } |
---|
73 | else |
---|
74 | { |
---|
75 | tfiles++; |
---|
76 | files = (char **)realloc(files,sizeof(char *)*tfiles); |
---|
77 | files[tfiles-1] = strdup(tlist[i]); |
---|
78 | } |
---|
79 | free( tlist[i] ); |
---|
80 | } |
---|
81 | if( t ) |
---|
82 | free( tlist ); |
---|
83 | chdir( curdir ); |
---|
84 | } |
---|