1
Fork 0
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:
faissaloo 2019-05-27 02:41:22 +01:00 committed by Andreas Kling
parent 07c356ce64
commit fee686e2cd

View file

@ -104,29 +104,16 @@ int print_name(struct stat& st, const char* name, const char* path_for_link_reso
return nprinted;
}
int do_dir(const char* path)
{
DIR* dirp = opendir(path);
if (!dirp) {
perror("opendir");
return 1;
}
char pathbuf[PATH_MAX];
while (auto* de = readdir(dirp)) {
if (de->d_name[0] == '.' && !flag_show_dotfiles)
continue;
sprintf(pathbuf, "%s/%s", path, de->d_name);
bool print_filesystem_object(const char* path, const char* name) {
struct stat st;
int rc = lstat(pathbuf, &st);
int rc = lstat(path, &st);
if (rc == -1) {
printf("lstat(%s) failed: %s\n", pathbuf, strerror(errno));
return 2;
printf("lstat(%s) failed: %s\n", path, strerror(errno));
return false;
}
if (flag_show_inode)
printf("%08u ", de->d_ino);
printf("%08u ", st.st_ino);
if (S_ISDIR(st.st_mode))
printf("d");
@ -177,25 +164,43 @@ int do_dir(const char* path)
tm->tm_min,
tm->tm_sec);
print_name(st, de->d_name, pathbuf);
print_name(st, name, path);
printf("\n");
return true;
}
int do_dir(const char* path)
{
DIR* dirp = opendir(path);
if (!dirp) {
perror("opendir");
return 1;
}
char pathbuf[PATH_MAX];
while (auto* de = readdir(dirp)) {
if (de->d_name[0] == '.' && !flag_show_dotfiles)
continue;
sprintf(pathbuf, "%s/%s", path, de->d_name);
print_filesystem_object(pathbuf, de->d_name);
}
closedir(dirp);
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;
int rc = lstat(path, &st);
if (rc == -1) {
printf("lstat(%s) failed: %s\n", path, strerror(errno));
return 2;
return false;
}
*nprinted = print_name(st, name);
return 0;
return true;
}
int do_dir_short(const char* path)
@ -236,7 +241,7 @@ int do_dir_short(const char* path)
char pathbuf[256];
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;
}
int column_width = 14;