[Change] Order files with lfile_readDir by newest first.
This commit is contained in:
parent
3baf8ae134
commit
71e0803b1a
63
src/lfile.c
63
src/lfile.c
@ -93,36 +93,87 @@ char** lfile_readDir(int* lfiles, const char* path) {
|
|||||||
snprintf(file, PATH_MAX, "%s%s", lfile_basePath(), path);
|
snprintf(file, PATH_MAX, "%s%s", lfile_basePath(), path);
|
||||||
|
|
||||||
#ifdef LINUX
|
#ifdef LINUX
|
||||||
|
int i, j, k, n;
|
||||||
DIR* d;
|
DIR* d;
|
||||||
struct dirent *dir;
|
struct dirent *dir;
|
||||||
char* name;
|
char* name;
|
||||||
int mfiles;
|
int mfiles;
|
||||||
|
struct stat sb;
|
||||||
|
time_t* tt, *ft;
|
||||||
|
char** tfiles;
|
||||||
|
|
||||||
(*lfiles) = 0;
|
(*lfiles) = 0;
|
||||||
mfiles = 100;
|
mfiles = 100;
|
||||||
files = malloc(sizeof(char*)*mfiles);
|
tfiles = malloc(sizeof(char*)*mfiles);
|
||||||
|
tt = malloc(sizeof(time_t)*mfiles);
|
||||||
|
|
||||||
d = opendir(file);
|
d = opendir(file);
|
||||||
if(d == NULL) {
|
if(d == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
|
||||||
|
|
||||||
|
/* Get the file list. */
|
||||||
while((dir = readdir(d)) != NULL) {
|
while((dir = readdir(d)) != NULL) {
|
||||||
name = dir->d_name;
|
name = dir->d_name;
|
||||||
|
|
||||||
if((strcmp(name, ".")==0) || (strcmp(name, "..")==0))
|
/* Skip hidden directories. */
|
||||||
|
if(name[0] == '.')
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
/* Stat the file. */
|
||||||
|
snprintf(file, PATH_MAX, "%s%s/%s", lfile_basePath(), path, name);
|
||||||
|
if(stat(file, &sb) == -1)
|
||||||
|
continue; /* Unable to stat. */
|
||||||
|
|
||||||
|
/* Enough memory? */
|
||||||
if((*lfiles)+1 > mfiles) {
|
if((*lfiles)+1 > mfiles) {
|
||||||
mfiles += 100;
|
mfiles += 100;
|
||||||
files = realloc(files, sizeof(char*) * mfiles);
|
tfiles = realloc(files, sizeof(char*) * mfiles);
|
||||||
|
tt = realloc(tt, sizeof(time_t) * mfiles);
|
||||||
}
|
}
|
||||||
|
|
||||||
files[(*lfiles)] = strdup(name);
|
/* Write the information. */
|
||||||
|
tfiles[(*lfiles)] = strdup(name);
|
||||||
|
tt[(*lfiles)] = sb.st_mtime;
|
||||||
(*lfiles)++;
|
(*lfiles)++;
|
||||||
}
|
}
|
||||||
|
|
||||||
closedir(d);
|
closedir(d);
|
||||||
|
|
||||||
|
/* Sort by last changed date. */
|
||||||
|
if((*lfiles) > 0) {
|
||||||
|
/* Need to allocate some stuff. */
|
||||||
|
files = malloc(sizeof(char*)*(*lfiles));
|
||||||
|
ft = malloc(sizeof(time_t)*(*lfiles));
|
||||||
|
|
||||||
|
/* Fill the list. */
|
||||||
|
for(i = 0; i < (*lfiles); i++) {
|
||||||
|
n = -1;
|
||||||
|
|
||||||
|
/* Get the next lowest. */
|
||||||
|
for(j = 0; j < (*lfiles); j++) {
|
||||||
|
/* Is lower? */
|
||||||
|
if((n == -1) || (tt[j] > tt[n])) {
|
||||||
|
/* Check if it's already there. */
|
||||||
|
for(k = 0; k < i; k++)
|
||||||
|
if(strcmp(files[k], tfiles[j])==0)
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* New lowest. */
|
||||||
|
if(k >= i)
|
||||||
|
n = j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
files[i] = tfiles[n];
|
||||||
|
ft[i] = tt[n];
|
||||||
|
}
|
||||||
|
free(ft);
|
||||||
|
} else
|
||||||
|
files = NULL;
|
||||||
|
|
||||||
|
/* Free temp stuff. */
|
||||||
|
free(tfiles);
|
||||||
|
free(tt);
|
||||||
|
|
||||||
#else
|
#else
|
||||||
#error "Needs implementation."
|
#error "Needs implementation."
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user