usbloadergx/source/listfiles.c

192 lines
4.6 KiB
C
Raw Normal View History

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