Sort directory entries when using -r
This commit is contained in:
parent
5a35484e71
commit
c5f6b2c0d5
15
main.c
15
main.c
|
@ -199,10 +199,15 @@ int check_append(const char *filename) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int fncmp(const void *a, const void *b) {
|
||||||
|
return strcoll(*((char* const*) a), *((char* const*) b));
|
||||||
|
}
|
||||||
|
|
||||||
void read_dir_rec(const char *dirname) {
|
void read_dir_rec(const char *dirname) {
|
||||||
char *filename;
|
char *filename;
|
||||||
const char **dirnames;
|
const char **dirnames;
|
||||||
int dircnt, diridx;
|
int dircnt, diridx;
|
||||||
|
int fcnt, fstart;
|
||||||
unsigned char first;
|
unsigned char first;
|
||||||
size_t len;
|
size_t len;
|
||||||
DIR *dir;
|
DIR *dir;
|
||||||
|
@ -217,6 +222,9 @@ void read_dir_rec(const char *dirname) {
|
||||||
dirnames = (const char**) s_malloc(dircnt * sizeof(const char*));
|
dirnames = (const char**) s_malloc(dircnt * sizeof(const char*));
|
||||||
dirnames[0] = dirname;
|
dirnames[0] = dirname;
|
||||||
|
|
||||||
|
fcnt = 0;
|
||||||
|
fstart = fileidx;
|
||||||
|
|
||||||
while (diridx > 0) {
|
while (diridx > 0) {
|
||||||
dirname = dirnames[--diridx];
|
dirname = dirnames[--diridx];
|
||||||
if (!(dir = opendir(dirname))) {
|
if (!(dir = opendir(dirname))) {
|
||||||
|
@ -238,7 +246,9 @@ void read_dir_rec(const char *dirname) {
|
||||||
}
|
}
|
||||||
dirnames[diridx++] = filename;
|
dirnames[diridx++] = filename;
|
||||||
} else {
|
} else {
|
||||||
if (!check_append(filename))
|
if (check_append(filename))
|
||||||
|
++fcnt;
|
||||||
|
else
|
||||||
free(filename);
|
free(filename);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -251,6 +261,9 @@ void read_dir_rec(const char *dirname) {
|
||||||
first = 0;
|
first = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (fcnt > 1)
|
||||||
|
qsort(filenames + fstart, fcnt, sizeof(char*), fncmp);
|
||||||
|
|
||||||
free(dirnames);
|
free(dirnames);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue