Userspace: Add color to ls

This commit is contained in:
Bananymous 2023-06-11 22:46:14 +03:00
parent 12d56be5cc
commit 6a8180470d
1 changed files with 29 additions and 19 deletions

View File

@ -31,6 +31,21 @@ const char* mode_string(mode_t mode)
return buffer; return buffer;
} }
const char* color_string(mode_t mode)
{
if ((mode & 0770000) == S_IFLNK)
return "\e[36m";
if ((mode & 0770000) == S_IFDIR)
return "\e[34m";
if ((mode & 0770000) == S_IFCHR)
return "\e[33m";
if ((mode & 0770000) == S_IFBLK)
return "\e[33m";
if ((mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
return "\e[32m";
return "";
}
void list_directory(const char* path) void list_directory(const char* path)
{ {
DIR* dirp = opendir(path); DIR* dirp = opendir(path);
@ -45,29 +60,24 @@ void list_directory(const char* path)
if (!all && dirent->d_name[0] == '.') if (!all && dirent->d_name[0] == '.')
continue; continue;
if (list)
{
if (!first) if (!first)
printf("\n"); printf(list ? "\n" : " ");
struct stat st; struct stat st;
if (fstatat(dirfd(dirp), dirent->d_name, &st, AT_SYMLINK_NOFOLLOW) == -1) if (fstatat(dirfd(dirp), dirent->d_name, &st, AT_SYMLINK_NOFOLLOW) == -1)
{ {
perror("stat"); perror("stat");
printf("?????????? ???? %s", dirent->d_name); if (list)
} printf("?????????? ???? ???? ?????? %s", dirent->d_name);
else else
{
printf("%s %6d %s", mode_string(st.st_mode), st.st_size, dirent->d_name);
}
}
else
{
if (!first)
printf(" ");
printf("%s", dirent->d_name); printf("%s", dirent->d_name);
} }
if (list)
printf("%s %4d %4d %6d %s%s\e[m", mode_string(st.st_mode), st.st_uid, st.st_gid, st.st_size, color_string(st.st_mode), dirent->d_name);
else
printf("%s%s\e[m", color_string(st.st_mode), dirent->d_name);
first = false; first = false;
} }