ls: sort files with -l
Always collect directory entries in a string_list and evaluate it later. This makes sure that the files are printed alphabetically even when -l is given. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
This commit is contained in:
parent
6dd613f1bc
commit
10d6391399
|
@ -53,6 +53,8 @@ int ls(const char *path, ulong flags)
|
||||||
char tmp[PATH_MAX];
|
char tmp[PATH_MAX];
|
||||||
struct stat s;
|
struct stat s;
|
||||||
struct string_list sl;
|
struct string_list sl;
|
||||||
|
struct string_list *entry;
|
||||||
|
int ret;
|
||||||
|
|
||||||
string_list_init(&sl);
|
string_list_init(&sl);
|
||||||
|
|
||||||
|
@ -71,51 +73,51 @@ int ls(const char *path, ulong flags)
|
||||||
if (!dir)
|
if (!dir)
|
||||||
return -errno;
|
return -errno;
|
||||||
|
|
||||||
while ((d = readdir(dir))) {
|
while ((d = readdir(dir)))
|
||||||
sprintf(tmp, "%s/%s", path, d->d_name);
|
string_list_add_sorted(&sl, d->d_name);
|
||||||
if (flags & LS_COLUMN) {
|
|
||||||
string_list_add_sorted(&sl, d->d_name);
|
|
||||||
} else {
|
|
||||||
if (lstat(tmp, &s))
|
|
||||||
goto out;
|
|
||||||
ls_one(d->d_name, tmp, &s);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
|
|
||||||
if (flags & LS_COLUMN) {
|
if (flags & LS_COLUMN) {
|
||||||
string_list_print_by_column(&sl);
|
string_list_print_by_column(&sl);
|
||||||
string_list_free(&sl);
|
} else {
|
||||||
|
string_list_for_each_entry(entry, &sl) {
|
||||||
|
sprintf(tmp, "%s/%s", path, entry->str);
|
||||||
|
ret = lstat(tmp, &s);
|
||||||
|
if (ret) {
|
||||||
|
printf("%s: %s\n", tmp, strerror(-ret));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
ls_one(entry->str, tmp, &s);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(flags & LS_RECURSIVE))
|
if (!(flags & LS_RECURSIVE))
|
||||||
return 0;
|
goto out;
|
||||||
|
|
||||||
dir = opendir(path);
|
string_list_for_each_entry(entry, &sl) {
|
||||||
if (!dir) {
|
if (!strcmp(entry->str, "."))
|
||||||
errno = ENOENT;
|
|
||||||
return -ENOENT;
|
|
||||||
}
|
|
||||||
|
|
||||||
while ((d = readdir(dir))) {
|
|
||||||
|
|
||||||
if (!strcmp(d->d_name, "."))
|
|
||||||
continue;
|
continue;
|
||||||
if (!strcmp(d->d_name, ".."))
|
if (!strcmp(entry->str, ".."))
|
||||||
continue;
|
continue;
|
||||||
sprintf(tmp, "%s/%s", path, d->d_name);
|
sprintf(tmp, "%s/%s", path, entry->str);
|
||||||
|
|
||||||
|
ret = lstat(tmp, &s);
|
||||||
|
if (ret) {
|
||||||
|
printf("%s: %s\n", tmp, strerror(-ret));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (lstat(tmp, &s))
|
|
||||||
goto out;
|
|
||||||
if (s.st_mode & S_IFDIR) {
|
if (s.st_mode & S_IFDIR) {
|
||||||
char *norm = normalise_path(tmp);
|
char *norm = normalise_path(tmp);
|
||||||
ls(norm, flags);
|
ls(norm, flags);
|
||||||
free(norm);
|
free(norm);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
out:
|
out:
|
||||||
closedir(dir);
|
string_list_free(&sl);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue