ls color output

This commit is contained in:
Your Name 2023-11-28 21:43:15 +03:00
parent 96b12d1a7d
commit a1a5f8b1f7
1 changed files with 39 additions and 11 deletions

View File

@ -1,5 +1,3 @@
/* Bugs: unicode strlen, -l flag col */
#include <pwd.h>
#include <grp.h>
#include <time.h>
@ -19,6 +17,7 @@
unsigned int a_flag;
unsigned int l_flag;
unsigned int F_flag;
unsigned int c_flag;
unsigned int p_flag;
struct d_node {
@ -127,17 +126,39 @@ void GetPerm(struct stat sb) {
}
int print(const struct d_node *node) {
/* F_flag and c_flag Make output look pretty */
char suf = ' ';
if (F_flag) {
if (S_ISDIR(node->stats.st_mode))
char *color = "";
if (S_ISDIR(node->stats.st_mode)) {
if (F_flag)
suf = '/';
else if ((node->stats.st_mode & S_IXUSR) || (node->stats.st_mode & S_IXGRP) || (node->stats.st_mode & S_IXOTH))
suf = '*';
color = "\033[34m";
}
else if (S_ISLNK(node->stats.st_mode)) {
if (F_flag)
suf = '@';
color = "\033[35m";
}
else if ((node->stats.st_mode & S_IXUSR) || (node->stats.st_mode & S_IXGRP) || (node->stats.st_mode & S_IXOTH)) {
if (F_flag)
suf = '*';
color = "\033[32m";
}
/* Output */
if (c_flag)
printf("%s", color);
int ret = 0;
if (!l_flag)
return printf("%s%c ", node->name, suf);
ret = printf("%s%c ", node->name, suf);
if (l_flag) {
GetPerm(node->stats);
@ -151,10 +172,13 @@ int print(const struct d_node *node) {
struct passwd *pw = getpwuid(node->stats.st_uid);
struct group *gr = getgrgid(node->stats.st_gid);
return printf(" %s %s %10jd %s %s%c", (pw != 0) ? pw->pw_name : "nobody", (gr != 0) ? gr->gr_name : "nobody", (uintmax_t)node->stats.st_size, date, node->name, suf);
ret = printf(" %s %s %10jd %s %s%c", (pw != 0) ? pw->pw_name : "nobody", (gr != 0) ? gr->gr_name : "nobody", (uintmax_t)node->stats.st_size, date, node->name, suf);
}
return 0;
if (c_flag)
printf("\033[0m");
return ret;
}
void col_print(struct d_node **node, size_t files, struct winsize w) {
@ -228,7 +252,7 @@ int ls(const char *dir_name, int label, struct winsize w) {
int main(int argc, char **argv) {
int opt;
while ((opt = getopt(argc, argv, "alF")) != -1) {
while ((opt = getopt(argc, argv, "alFc")) != -1) {
switch (opt) {
case 'a':
a_flag = 1;
@ -242,8 +266,12 @@ int main(int argc, char **argv) {
F_flag = 1;
break;
case 'c':
c_flag = 1;
break;
default:
printf("ls [path]\n\t[-a Show hidden files]\n\t[-l Use a long listing format]\n\t[-F Append indicator to names]\n");
printf("ls [path]\n\t[-a Show hidden files]\n\t[-l Use a long listing format]\n\t[-F Append indicator to names]\n\t[-c color mode]\n");
return 0;
}
}