2009-10-01 01:10:58 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <gccore.h>
|
|
|
|
#include <sys/dir.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "listfiles.h"
|
2009-11-15 22:30:44 +01:00
|
|
|
#include "libfat/fat.h"
|
2009-10-01 01:10:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
static char alldirfiles[300][70];
|
|
|
|
char filenames[80];
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
bool findfile( const char * filename, const char * path )
|
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
DIR *dir;
|
|
|
|
struct dirent *file;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
dir = opendir( path );
|
2009-10-01 01:10:58 +02:00
|
|
|
|
|
|
|
char temp[11];
|
2010-09-19 01:16:05 +02:00
|
|
|
while ( ( file = readdir( dir ) ) )
|
|
|
|
{
|
|
|
|
snprintf( temp, sizeof( temp ), "%s", file->d_name );
|
|
|
|
if ( !strncmpi( temp, filename, 11 ) )
|
|
|
|
{
|
|
|
|
closedir( dir );
|
2009-10-01 01:10:58 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
closedir( dir );
|
2009-10-01 01:10:58 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
bool subfoldercreate( const char * fullpath )
|
|
|
|
{
|
|
|
|
//check forsubfolders
|
|
|
|
char dir[300];
|
|
|
|
char * pch = NULL;
|
|
|
|
u32 len;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
strlcpy( dir, fullpath, sizeof( dir ) );
|
|
|
|
len = strlen( dir );
|
|
|
|
if ( len && len < sizeof( dir ) - 2 && dir[len-1] != '/' )
|
|
|
|
{
|
|
|
|
dir[len++] = '/';
|
|
|
|
dir[len] = '\0';
|
|
|
|
}
|
|
|
|
if ( stat( dir, &st ) != 0 ) // fullpath not exist?
|
|
|
|
{
|
|
|
|
while ( len && dir[len-1] == '/' )
|
|
|
|
dir[--len] = '\0'; // remove all trailing /
|
|
|
|
pch = strrchr( dir, '/' );
|
|
|
|
if ( pch == NULL ) return false;
|
|
|
|
*pch = '\0';
|
|
|
|
if ( subfoldercreate( dir ) )
|
|
|
|
{
|
|
|
|
*pch = '/';
|
|
|
|
if ( mkdir( dir, 0777 ) == -1 )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2009-10-01 01:10:58 +02:00
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
bool subfolderremove( const char * fullpath, const char*fp )
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
if ( stat( fullpath, &st ) != 0 ) // fullpath not exist?
|
|
|
|
return false;
|
|
|
|
if ( S_ISDIR( st.st_mode ) )
|
|
|
|
{
|
|
|
|
DIR_ITER *dir = NULL;
|
|
|
|
char filename[256];
|
|
|
|
bool cont = true;
|
|
|
|
while ( cont )
|
|
|
|
{
|
|
|
|
cont = false;
|
|
|
|
dir = diropen( fullpath );
|
|
|
|
if ( dir )
|
|
|
|
{
|
|
|
|
char* bind = fullpath[strlen( fullpath )-1] == '/' ? "" : "/";
|
|
|
|
while ( dirnext( dir, filename, &st ) == 0 )
|
|
|
|
{
|
|
|
|
if ( strcmp( filename, "." ) != 0 && strcmp( filename, ".." ) != 0 )
|
|
|
|
{
|
|
|
|
char currentname[256];
|
|
|
|
if ( S_ISDIR( st.st_mode ) )
|
|
|
|
snprintf( currentname, sizeof( currentname ), "%s%s%s/", fullpath, bind, filename );
|
|
|
|
else
|
|
|
|
snprintf( currentname, sizeof( currentname ), "%s%s%s", fullpath, bind, filename );
|
|
|
|
subfolderremove( currentname, fp );
|
|
|
|
cont = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dirclose( dir );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return unlink( fullpath ) == 0;
|
2009-11-10 00:03:13 +01:00
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
char * GetFileName( int i )
|
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
return alldirfiles[i];
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
s32 filenamescmp( const void *a, const void *b )
|
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
/* Compare strings */
|
2010-09-19 01:16:05 +02:00
|
|
|
return stricmp( ( char * )a, ( char * )b );
|
2009-10-01 01:10:58 +02:00
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
int GetAllDirFiles( char * filespath )
|
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
int countfiles = 0;
|
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
DIR_ITER* dir;
|
2010-09-19 01:16:05 +02:00
|
|
|
dir = diropen ( filespath );
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( dir == NULL ) //If empty
|
2009-10-01 01:10:58 +02:00
|
|
|
return 0;
|
2010-09-19 01:16:05 +02:00
|
|
|
while ( dirnext( dir, filenames, &st ) == 0 )
|
|
|
|
{
|
|
|
|
if ( ( st.st_mode & S_IFDIR ) == 0 )
|
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
// st.st_mode & S_IFDIR indicates a directory
|
2010-09-19 01:16:05 +02:00
|
|
|
snprintf( alldirfiles[countfiles], 70, "%s", filenames );
|
2009-10-01 01:10:58 +02:00
|
|
|
countfiles++;
|
|
|
|
}
|
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
dirclose( dir );
|
|
|
|
qsort( alldirfiles, countfiles, sizeof( char[70] ), filenamescmp );
|
2009-10-01 01:10:58 +02:00
|
|
|
return countfiles;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
bool checkfile( char * path )
|
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
FILE * f;
|
2010-09-19 01:16:05 +02:00
|
|
|
f = fopen( path, "r" );
|
|
|
|
if ( f )
|
|
|
|
{
|
|
|
|
fclose( f );
|
2009-10-01 01:10:58 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
bool SearchFile( const char * searchpath, const char * searched_filename, char * outfilepath )
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2009-12-28 17:05:16 +01:00
|
|
|
struct stat st;
|
|
|
|
DIR_ITER *dir = NULL;
|
|
|
|
bool result = false;
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2009-12-28 17:05:16 +01:00
|
|
|
char filename[1024];
|
2010-09-19 01:16:05 +02:00
|
|
|
char pathptr[strlen( searchpath )+1];
|
|
|
|
snprintf( pathptr, sizeof( pathptr ), "%s", searchpath );
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( pathptr[strlen( pathptr )-1] == '/' )
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
pathptr[strlen( pathptr )-1] = '\0';
|
2009-12-28 17:05:16 +01:00
|
|
|
}
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
dir = diropen( pathptr );
|
|
|
|
if ( !dir )
|
2009-12-28 17:05:16 +01:00
|
|
|
return false;
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
while ( dirnext( dir, filename, &st ) == 0 && result == false )
|
|
|
|
{
|
|
|
|
if ( strcasecmp( filename, searched_filename ) == 0 )
|
|
|
|
{
|
|
|
|
if ( outfilepath )
|
|
|
|
{
|
|
|
|
sprintf( outfilepath, "%s/%s", pathptr, filename );
|
|
|
|
}
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
else if ( ( st.st_mode & S_IFDIR ) != 0 )
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( strcmp( filename, "." ) != 0 && strcmp( filename, ".." ) != 0 )
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2009-12-28 17:05:16 +01:00
|
|
|
char newpath[1024];
|
2010-09-19 01:16:05 +02:00
|
|
|
snprintf( newpath, sizeof( newpath ), "%s/%s", pathptr, filename );
|
|
|
|
result = SearchFile( newpath, searched_filename, outfilepath );
|
2009-12-28 17:05:16 +01:00
|
|
|
}
|
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
dirclose( dir );
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2009-12-28 17:05:16 +01:00
|
|
|
return result;
|
|
|
|
}
|