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

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