mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:57:34 +00:00
Ls: Refactor long directory listings
This commit is contained in:
parent
07c356ce64
commit
fee686e2cd
1 changed files with 78 additions and 73 deletions
151
Userland/ls.cpp
151
Userland/ls.cpp
|
@ -104,6 +104,72 @@ int print_name(struct stat& st, const char* name, const char* path_for_link_reso
|
||||||
return nprinted;
|
return nprinted;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool print_filesystem_object(const char* path, const char* name) {
|
||||||
|
struct stat st;
|
||||||
|
int rc = lstat(path, &st);
|
||||||
|
if (rc == -1) {
|
||||||
|
printf("lstat(%s) failed: %s\n", path, strerror(errno));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (flag_show_inode)
|
||||||
|
printf("%08u ", st.st_ino);
|
||||||
|
|
||||||
|
if (S_ISDIR(st.st_mode))
|
||||||
|
printf("d");
|
||||||
|
else if (S_ISLNK(st.st_mode))
|
||||||
|
printf("l");
|
||||||
|
else if (S_ISBLK(st.st_mode))
|
||||||
|
printf("b");
|
||||||
|
else if (S_ISCHR(st.st_mode))
|
||||||
|
printf("c");
|
||||||
|
else if (S_ISFIFO(st.st_mode))
|
||||||
|
printf("f");
|
||||||
|
else if (S_ISSOCK(st.st_mode))
|
||||||
|
printf("s");
|
||||||
|
else if (S_ISREG(st.st_mode))
|
||||||
|
printf("-");
|
||||||
|
else
|
||||||
|
printf("?");
|
||||||
|
|
||||||
|
printf("%c%c%c%c%c%c%c%c",
|
||||||
|
st.st_mode & S_IRUSR ? 'r' : '-',
|
||||||
|
st.st_mode & S_IWUSR ? 'w' : '-',
|
||||||
|
st.st_mode & S_ISUID ? 's' : (st.st_mode & S_IXUSR ? 'x' : '-'),
|
||||||
|
st.st_mode & S_IRGRP ? 'r' : '-',
|
||||||
|
st.st_mode & S_IWGRP ? 'w' : '-',
|
||||||
|
st.st_mode & S_ISGID ? 's' : (st.st_mode & S_IXGRP ? 'x' : '-'),
|
||||||
|
st.st_mode & S_IROTH ? 'r' : '-',
|
||||||
|
st.st_mode & S_IWOTH ? 'w' : '-'
|
||||||
|
);
|
||||||
|
|
||||||
|
if (st.st_mode & S_ISVTX)
|
||||||
|
printf("t");
|
||||||
|
else
|
||||||
|
printf("%c", st.st_mode & S_IXOTH ? 'x' : '-');
|
||||||
|
|
||||||
|
printf(" %4u %4u", st.st_uid, st.st_gid);
|
||||||
|
|
||||||
|
if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))
|
||||||
|
printf(" %4u,%4u ", major(st.st_rdev), minor(st.st_rdev));
|
||||||
|
else
|
||||||
|
printf(" %10u ", st.st_size);
|
||||||
|
|
||||||
|
auto* tm = localtime(&st.st_mtime);
|
||||||
|
printf(" %4u-%02u-%02u %02u:%02u:%02u ",
|
||||||
|
tm->tm_year + 1900,
|
||||||
|
tm->tm_mon + 1,
|
||||||
|
tm->tm_mday,
|
||||||
|
tm->tm_hour,
|
||||||
|
tm->tm_min,
|
||||||
|
tm->tm_sec);
|
||||||
|
|
||||||
|
print_name(st, name, path);
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
int do_dir(const char* path)
|
int do_dir(const char* path)
|
||||||
{
|
{
|
||||||
DIR* dirp = opendir(path);
|
DIR* dirp = opendir(path);
|
||||||
|
@ -118,84 +184,23 @@ int do_dir(const char* path)
|
||||||
continue;
|
continue;
|
||||||
sprintf(pathbuf, "%s/%s", path, de->d_name);
|
sprintf(pathbuf, "%s/%s", path, de->d_name);
|
||||||
|
|
||||||
struct stat st;
|
print_filesystem_object(pathbuf, de->d_name);
|
||||||
int rc = lstat(pathbuf, &st);
|
|
||||||
if (rc == -1) {
|
|
||||||
printf("lstat(%s) failed: %s\n", pathbuf, strerror(errno));
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (flag_show_inode)
|
|
||||||
printf("%08u ", de->d_ino);
|
|
||||||
|
|
||||||
if (S_ISDIR(st.st_mode))
|
|
||||||
printf("d");
|
|
||||||
else if (S_ISLNK(st.st_mode))
|
|
||||||
printf("l");
|
|
||||||
else if (S_ISBLK(st.st_mode))
|
|
||||||
printf("b");
|
|
||||||
else if (S_ISCHR(st.st_mode))
|
|
||||||
printf("c");
|
|
||||||
else if (S_ISFIFO(st.st_mode))
|
|
||||||
printf("f");
|
|
||||||
else if (S_ISSOCK(st.st_mode))
|
|
||||||
printf("s");
|
|
||||||
else if (S_ISREG(st.st_mode))
|
|
||||||
printf("-");
|
|
||||||
else
|
|
||||||
printf("?");
|
|
||||||
|
|
||||||
printf("%c%c%c%c%c%c%c%c",
|
|
||||||
st.st_mode & S_IRUSR ? 'r' : '-',
|
|
||||||
st.st_mode & S_IWUSR ? 'w' : '-',
|
|
||||||
st.st_mode & S_ISUID ? 's' : (st.st_mode & S_IXUSR ? 'x' : '-'),
|
|
||||||
st.st_mode & S_IRGRP ? 'r' : '-',
|
|
||||||
st.st_mode & S_IWGRP ? 'w' : '-',
|
|
||||||
st.st_mode & S_ISGID ? 's' : (st.st_mode & S_IXGRP ? 'x' : '-'),
|
|
||||||
st.st_mode & S_IROTH ? 'r' : '-',
|
|
||||||
st.st_mode & S_IWOTH ? 'w' : '-'
|
|
||||||
);
|
|
||||||
|
|
||||||
if (st.st_mode & S_ISVTX)
|
|
||||||
printf("t");
|
|
||||||
else
|
|
||||||
printf("%c", st.st_mode & S_IXOTH ? 'x' : '-');
|
|
||||||
|
|
||||||
printf(" %4u %4u", st.st_uid, st.st_gid);
|
|
||||||
|
|
||||||
if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))
|
|
||||||
printf(" %4u,%4u ", major(st.st_rdev), minor(st.st_rdev));
|
|
||||||
else
|
|
||||||
printf(" %10u ", st.st_size);
|
|
||||||
|
|
||||||
auto* tm = localtime(&st.st_mtime);
|
|
||||||
printf(" %4u-%02u-%02u %02u:%02u:%02u ",
|
|
||||||
tm->tm_year + 1900,
|
|
||||||
tm->tm_mon + 1,
|
|
||||||
tm->tm_mday,
|
|
||||||
tm->tm_hour,
|
|
||||||
tm->tm_min,
|
|
||||||
tm->tm_sec);
|
|
||||||
|
|
||||||
print_name(st, de->d_name, pathbuf);
|
|
||||||
|
|
||||||
printf("\n");
|
|
||||||
}
|
}
|
||||||
closedir(dirp);
|
closedir(dirp);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int print_filesystem_object_short(const char *path, const char *name, int *nprinted) {
|
bool print_filesystem_object_short(const char *path, const char *name, int *nprinted) {
|
||||||
struct stat st;
|
struct stat st;
|
||||||
int rc = lstat(path, &st);
|
int rc = lstat(path, &st);
|
||||||
if (rc == -1) {
|
if (rc == -1) {
|
||||||
printf("lstat(%s) failed: %s\n", path, strerror(errno));
|
printf("lstat(%s) failed: %s\n", path, strerror(errno));
|
||||||
return 2;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
*nprinted = print_name(st, name);
|
*nprinted = print_name(st, name);
|
||||||
return 0;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int do_dir_short(const char* path)
|
int do_dir_short(const char* path)
|
||||||
|
@ -207,13 +212,13 @@ int do_dir_short(const char* path)
|
||||||
DIR* dirp = opendir(path);
|
DIR* dirp = opendir(path);
|
||||||
if (!dirp) {
|
if (!dirp) {
|
||||||
if (errno == ENOTDIR) {
|
if (errno == ENOTDIR) {
|
||||||
int nprinted;
|
int nprinted;
|
||||||
print_filesystem_object_short(path, path, &nprinted);
|
print_filesystem_object_short(path, path, &nprinted);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
perror("opendir");
|
perror("opendir");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -236,8 +241,8 @@ int do_dir_short(const char* path)
|
||||||
char pathbuf[256];
|
char pathbuf[256];
|
||||||
sprintf(pathbuf, "%s/%s", path, name.characters());
|
sprintf(pathbuf, "%s/%s", path, name.characters());
|
||||||
|
|
||||||
if (print_filesystem_object_short(pathbuf, name.characters(), &nprinted) == 2) {
|
if (!print_filesystem_object_short(pathbuf, name.characters(), &nprinted)) {
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
int column_width = 14;
|
int column_width = 14;
|
||||||
printed_on_row += column_width;
|
printed_on_row += column_width;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue