This commit is contained in:
Your Name 2024-03-10 14:09:27 +03:00
parent ba33727ab3
commit 35c030b66c
5 changed files with 77 additions and 18 deletions

2
TODO
View File

@ -52,5 +52,5 @@ Findutils:
find
BUGS:
ls (unicode strlen, -l flag col)
ls (unicode strlen)
xargs (getopt with glibc)

View File

@ -22,8 +22,9 @@
/* colors for ls */
#define LS_DIR_COLOR "\033[1;34m"
#define LS_LINK_COLOR "\033[1;35m"
#define LS_SOCK_COLOR LS_LINK_COLOR
#define LS_FIFO_COLOR LS_LINK_COLOR
#define LS_SOCK_COLOR "\033[35m"
#define LS_FIFO_COLOR "\033[1;35m"
#define LS_BLOCK_COLOR "\033[1;33m"
#define LS_EXE_COLOR "\033[1;32m"
/* Init scripts */

View File

@ -12,6 +12,7 @@
#define WR_PERM (S_IWUSR | S_IWGRP | S_IWOTH)
#define EX_PERM (S_IXUSR | S_IXGRP | S_IXOTH)
#define RD_PERM (S_IRUSR | S_IRGRP | S_IROTH)
#define S_PERM (S_ISUID | S_ISGID | S_ISVTX)
#define FULL_PERM (WR_PERM | EX_PERM | RD_PERM)
mode_t mu_parse_mode(const char *s, mode_t cur_mode) {
@ -32,6 +33,10 @@ mode_t mu_parse_mode(const char *s, mode_t cur_mode) {
for (size_t i = 0; i < strlen(s); i++) {
switch (s[i]) {
case 's':
mode |= S_PERM;
break;
case 'r':
mode |= RD_PERM;
break;

View File

@ -163,34 +163,90 @@ char *get_date(time_t mtime) {
/* Print */
int print(const struct d_node *node) {
/* F_flag and c_flag Make output look pretty */
char suf = ' ';
char ind = '-';
char *color = "";
char mode[] = "----------";
if (node->stats.st_mode & S_IRUSR)
mode[1] = 'r';
if (node->stats.st_mode & S_IRGRP)
mode[4] = 'r';
if (node->stats.st_mode & S_IROTH)
mode[7] = 'r';
if (node->stats.st_mode & S_IWUSR)
mode[2] = 'w';
if (node->stats.st_mode & S_IWGRP)
mode[5] = 'w';
if (node->stats.st_mode & S_IWOTH)
mode[8] = 'w';
if (node->stats.st_mode & S_IXUSR)
mode[3] = 'x';
if (node->stats.st_mode & S_IXGRP)
mode[6] = 'x';
if (node->stats.st_mode & S_IXOTH)
mode[9] = 'x';
if (node->stats.st_mode & S_ISUID) {
if (node->stats.st_mode & S_IXUSR)
mode[3] = 's';
else
mode[3] = 'S';
}
if (node->stats.st_mode & S_ISGID) {
if (node->stats.st_mode & S_IRGRP)
mode[6] = 's';
else
mode[6] = 'S';
}
if (node->stats.st_mode & S_ISVTX) {
if (node->stats.st_mode & S_IROTH)
mode[9] = 't';
else
mode[9] = 'T';
}
if (S_ISDIR(node->stats.st_mode)) {
if (node->name[strlen(node->name) - 1] != '/')
suf = '/';
ind = 'd';
mode[0] = 'd';
color = LS_DIR_COLOR;
}
else if (S_ISLNK(node->stats.st_mode)) {
suf = '@';
ind = 'l';
mode[0] = 'l';
color = LS_LINK_COLOR;
}
else if (S_ISSOCK(node->stats.st_mode)) {
suf = '=';
ind = 's';
mode[0] = 's';
color = LS_SOCK_COLOR;
}
else if (S_ISBLK(node->stats.st_mode)) {
mode[0] = 'b';
color = LS_BLOCK_COLOR;
}
else if (S_ISFIFO(node->stats.st_mode)) {
suf = '|';
ind = 'p';
mode[0] = 'p';
color = LS_FIFO_COLOR;
}
@ -213,10 +269,7 @@ int print(const struct d_node *node) {
}
if (l_flag) {
putchar(ind);
printf("%c%c%c", (node->stats.st_mode & S_IRUSR) ? 'r' : '-', (node->stats.st_mode & S_IWUSR) ? 'w' : '-', (node->stats.st_mode & S_IXUSR) ? 'x' : '-');
printf("%c%c%c", (node->stats.st_mode & S_IRGRP) ? 'r' : '-', (node->stats.st_mode & S_IWOTH) ? 'w' : '-', (node->stats.st_mode & S_IXGRP) ? 'x' : '-');
printf("%c%c%c", (node->stats.st_mode & S_IROTH) ? 'r' : '-', (node->stats.st_mode & S_IWOTH) ? 'w' : '-', (node->stats.st_mode & S_IXOTH) ? 'x' : '-');
printf("%s", mode);
struct passwd *pw = getpwuid(node->stats.st_uid);
struct group *gr = getgrgid(node->stats.st_gid);
@ -224,10 +277,10 @@ int print(const struct d_node *node) {
char *gr_name = (gr != 0) ? gr->gr_name : "nobody";
char *pw_name = (pw != 0) ? pw->pw_name : "nobody";
if (h_flag)
ret += printf(" %4ju %2s %2s %6s %s ", (uintmax_t)node->stats.st_nlink, pw_name, gr_name, mu_humansize(node->stats.st_size, 1024), get_date(node->stats.st_mtime));
ret += printf(" %4ju %4s %6s %6s %s ", (uintmax_t)node->stats.st_nlink, pw_name, gr_name, mu_humansize(node->stats.st_size, 1024), get_date(node->stats.st_mtime));
else
ret += printf(" %4ju %2s %2s %10jd %s ", (uintmax_t)node->stats.st_nlink, pw_name, gr_name, (uintmax_t)node->stats.st_size, get_date(node->stats.st_mtime));
ret += printf(" %4ju %4s %6s %10jd %s ", (uintmax_t)node->stats.st_nlink, pw_name, gr_name, (uintmax_t)node->stats.st_size, get_date(node->stats.st_mtime));
}
if (c_flag && p_flag)
@ -330,7 +383,7 @@ int ls(const char *dir_name, int label, struct winsize w) {
qsort(dir, files, sizeof(struct d_node *), sorter);
if ((label || R_flag) && !d_flag && !its_file)
printf("\n%s:\n", dir_name);
printf("%s:\n", dir_name);
/* pipe print */
if (!p_flag || l_flag || O_flag) {

View File

@ -19,7 +19,7 @@ long parse_long(const char *str) {
exit(1);
}
else if (value < 1) {
else if (value < 0) {
fprintf(stderr, "mknod: number is negative: %s\n", str);
exit(1);
}