mirror of
https://github.com/troglobit/libite.git
synced 2026-11-28 20:48:27 +00:00
tree(): Refactor, flatten tree
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
1 parent
441d5f58d7
commit
32c774deef
1 file changed
+38
-40
@@ -59,56 +59,54 @@ static void get_perms(struct stat *st, char *buf, size_t len)
|
|||||||
|
|
||||||
static int descend(char *path, int show_perms, char *pfx)
|
static int descend(char *path, int show_perms, char *pfx)
|
||||||
{
|
{
|
||||||
int result = 0;
|
int i, n, result = 0;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
|
struct dirent **namelist = NULL;
|
||||||
|
|
||||||
if (-1 == lstat(path, &st))
|
if (-1 == lstat(path, &st))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
if ((st.st_mode & S_IFMT) == S_IFDIR) {
|
if ((st.st_mode & S_IFMT) != S_IFDIR) {
|
||||||
int i, n;
|
errno = ENOTDIR;
|
||||||
struct dirent **namelist = NULL;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
n = scandir(path, &namelist, filter, alphasort);
|
n = scandir(path, &namelist, filter, alphasort);
|
||||||
if (n) {
|
if (n) {
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
char t = ' ', p[14] = "", s[256] = "";
|
char t = ' ', p[14] = "", s[256] = "";
|
||||||
char buf[256];
|
char buf[256];
|
||||||
char dir[80];
|
char dir[80];
|
||||||
|
|
||||||
if (i + 1 == n) {
|
if (i + 1 == n) {
|
||||||
printf("%s `- ", pfx);
|
printf("%s `- ", pfx);
|
||||||
snprintf(dir, sizeof(dir), "%s ", pfx);
|
snprintf(dir, sizeof(dir), "%s ", pfx);
|
||||||
} else {
|
} else {
|
||||||
printf("%s|-- ", pfx);
|
printf("%s|-- ", pfx);
|
||||||
snprintf(dir, sizeof(dir), "%s| ", pfx);
|
snprintf(dir, sizeof(dir), "%s| ", pfx);
|
||||||
}
|
|
||||||
|
|
||||||
snprintf(buf, sizeof(buf), "%s/%s", path, namelist[i]->d_name);
|
|
||||||
if (!lstat(buf, &st)) {
|
|
||||||
if (show_perms)
|
|
||||||
get_perms(&st, p, sizeof(p));
|
|
||||||
if ((st.st_mode & S_IFMT) == S_IFDIR)
|
|
||||||
t = '/';
|
|
||||||
if (S_ISLNK(st.st_mode)) {
|
|
||||||
snprintf(s, sizeof(s), "-> ");
|
|
||||||
if (-1 == readlink(buf, &s[3], sizeof(s) - 3))
|
|
||||||
s[0] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("%s%s%c%s\n", p, namelist[i]->d_name, t, s);
|
|
||||||
if (t == '/')
|
|
||||||
result += descend(buf, show_perms, dir);
|
|
||||||
|
|
||||||
free(namelist[i]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
free(namelist);
|
snprintf(buf, sizeof(buf), "%s/%s", path, namelist[i]->d_name);
|
||||||
|
if (!lstat(buf, &st)) {
|
||||||
|
if (show_perms)
|
||||||
|
get_perms(&st, p, sizeof(p));
|
||||||
|
if ((st.st_mode & S_IFMT) == S_IFDIR)
|
||||||
|
t = '/';
|
||||||
|
if (S_ISLNK(st.st_mode)) {
|
||||||
|
snprintf(s, sizeof(s), "-> ");
|
||||||
|
if (-1 == readlink(buf, &s[3], sizeof(s) - 3))
|
||||||
|
s[0] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%s%s%c%s\n", p, namelist[i]->d_name, t, s);
|
||||||
|
if (t == '/')
|
||||||
|
result += descend(buf, show_perms, dir);
|
||||||
|
|
||||||
|
free(namelist[i]);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
errno = ENOTDIR;
|
free(namelist);
|
||||||
result = -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
Reference in new issue
Block a user