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];
|
|
|
|
|
|
|
|
bool findfile(const char * filename, const char * path) {
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *file;
|
|
|
|
|
|
|
|
dir = opendir(path);
|
|
|
|
|
|
|
|
char temp[11];
|
|
|
|
while ((file = readdir(dir))) {
|
|
|
|
snprintf(temp,sizeof(temp),"%s",file->d_name);
|
|
|
|
if (!strncmpi(temp,filename,11)) {
|
|
|
|
closedir(dir);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-10-05 12:53:19 +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);
|
2009-11-10 00:03:13 +01:00
|
|
|
if(len && len< sizeof(dir)-2 && dir[len-1] != '/')
|
2009-10-05 12:53:19 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
2009-11-10 00:03:13 +01: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-10-01 01:10:58 +02:00
|
|
|
char * GetFileName(int i) {
|
|
|
|
return alldirfiles[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
s32 filenamescmp(const void *a, const void *b) {
|
|
|
|
/* Compare strings */
|
|
|
|
return stricmp((char *)a, (char *)b);
|
|
|
|
}
|
|
|
|
|
|
|
|
int GetAllDirFiles(char * filespath) {
|
|
|
|
int countfiles = 0;
|
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
DIR_ITER* dir;
|
|
|
|
dir = diropen (filespath);
|
|
|
|
|
|
|
|
if (dir == NULL) //If empty
|
|
|
|
return 0;
|
|
|
|
while (dirnext(dir,filenames,&st) == 0) {
|
|
|
|
if ((st.st_mode & S_IFDIR) == 0) {
|
|
|
|
// st.st_mode & S_IFDIR indicates a directory
|
|
|
|
snprintf(alldirfiles[countfiles], 70, "%s", filenames);
|
|
|
|
countfiles++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dirclose(dir);
|
|
|
|
qsort(alldirfiles, countfiles, sizeof(char[70]), filenamescmp);
|
|
|
|
return countfiles;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool checkfile(char * path) {
|
|
|
|
FILE * f;
|
|
|
|
f = fopen(path,"r");
|
|
|
|
if (f) {
|
|
|
|
fclose(f);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-12-28 17:05:16 +01:00
|
|
|
bool SearchFile(const char * searchpath, const char * searched_filename, char * outfilepath)
|
|
|
|
{
|
|
|
|
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];
|
|
|
|
char pathptr[strlen(searchpath)+1];
|
|
|
|
snprintf(pathptr, sizeof(pathptr), "%s", searchpath);
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2009-12-28 17:05:16 +01:00
|
|
|
if(pathptr[strlen(pathptr)-1] == '/')
|
|
|
|
{
|
|
|
|
pathptr[strlen(pathptr)-1] = '\0';
|
|
|
|
}
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2009-12-28 17:05:16 +01:00
|
|
|
dir = diropen(pathptr);
|
|
|
|
if(!dir)
|
|
|
|
return false;
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2009-12-28 17:05:16 +01: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)
|
|
|
|
{
|
|
|
|
if(strcmp(filename, ".") != 0 && strcmp(filename, "..") != 0)
|
|
|
|
{
|
|
|
|
char newpath[1024];
|
|
|
|
snprintf(newpath, sizeof(newpath), "%s/%s", pathptr, filename);
|
|
|
|
result = SearchFile(newpath, searched_filename, outfilepath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dirclose(dir);
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2009-12-28 17:05:16 +01:00
|
|
|
return result;
|
|
|
|
}
|