feat(tools): ls parse LS_COLORS

This commit is contained in:
d0p1 🏳️‍⚧️ 2024-09-17 12:27:39 +02:00
parent 19d04b9f49
commit 982cc66c3f
4 changed files with 137 additions and 14 deletions

View file

@ -8,7 +8,7 @@ bin_PROGRAMS = mkfs.stpd tools.stpd inspect.stpd
mkfs_stpd_SOURCES = mkfs/main.c
mkfs_stpd_LDADD = ../libfs/libfs.a mkfs/bootsector.o
tools_stpd_SOURCES = tools/main.c tools/ls.c tools/copy.c tools/mkdir.c tools/rm.c
tools_stpd_SOURCES = tools/main.c tools/ls.c tools/ls/colors.c tools/copy.c tools/mkdir.c tools/rm.c
inspect_stpd_SOURCES = inspect/main.c

View file

@ -15,6 +15,7 @@
#endif /* HAVE_GETOPT_H */
#include <stupidfs.h>
#include "libfs/fs.h"
#include "ls/colors.h"
#ifdef HAVE_STRUCT_OPTION
static struct option long_options[] = {
@ -172,26 +173,59 @@ print_size(struct fs_inode *ip)
static void
print_name(struct stpdfs_dirent *dirent, struct fs_inode *ip)
{
enum COLORS type;
type = C_NORM;
if ((ip->inode.mode & STPDFS_S_IFMT) == STPDFS_S_IFREG)
{
type = C_FILE;
}
else if ((ip->inode.mode & STPDFS_S_IFMT) == STPDFS_S_IFDIR)
{
if (ip->inode.mode & (STPDFS_S_IWOTH | STPDFS_S_ISVTX))
{
type = C_STICKY_OTH_WRITABLE;
}
else if (ip->inode.mode & STPDFS_S_ISVTX)
{
type = C_STICKY;
}
else if (ip->inode.mode & STPDFS_S_IWOTH)
{
type = C_OTHER_WRITABLE;
}
else
{
type = C_DIR;
}
}
else if ((ip->inode.mode & STPDFS_S_IFMT) == STPDFS_S_IFLNK)
{
type = C_LINK;
}
else if (ip->inode.mode & STPDFS_S_ISUID)
{
type = C_SETUID;
}
else if (ip->inode.mode & STPDFS_S_ISGID)
{
type = C_SETGID;
}
else if (ip->inode.mode & STPDFS_S_IXUSR)
{
type = C_EXEC;
}
if (color)
{
if ((ip->inode.mode & STPDFS_S_IFMT) == STPDFS_S_IFDIR)
{
printf("\033[01;34m");
}
else if ((ip->inode.mode & STPDFS_S_IFMT) == STPDFS_S_IFLNK)
{
printf("\033[01;36m");
}
else if (ip->inode.mode & STPDFS_S_IXUSR)
{
printf("\033[01;32m");
}
printf("%s%s%s", ls_colors[C_LEFT], ls_colors[type], ls_colors[C_RIGHT]);
}
printf("%s\n", dirent->filename);
if (color)
{
printf("\033[0m");
printf("%s%s%s", ls_colors[C_LEFT], ls_colors[C_END], ls_colors[C_RIGHT]);
}
}
@ -294,6 +328,7 @@ cmd_ls(int argc, char **argv)
break;
case 'c':
ls_parse_colors();
color = 1;
break;

64
tools/tools/ls/colors.c Normal file
View file

@ -0,0 +1,64 @@
#include "colors.h"
#include <stdlib.h>
#include <string.h>
char *ls_colors[] = {
/* C_NORM */ "0",
/* C_FILE */ "0",
/* C_DIR */ "34",
/* C_LINK */ "35",
/* C_SETUID */ "30;41",
/* C_SETGID */ "30;46",
/* C_STICKY_OTH_WRITABLE */ "30;42",
/* C_OTHER_WRITABLE */ "30;43",
/* C_STICKY */ "0",
/* C_EXEC */ "31",
/* C_LEFT */ "\033[",
/* C_RIGHT */ "m",
/* C_END */ "0"
};
static const struct {
char *abrv;
enum COLORS type;
} abrv2type[] = {
{"no=", C_NORM},
{"fi=", C_FILE},
{"di=", C_DIR},
{"ln=", C_LINK},
{"su=", C_SETUID},
{"sg=", C_SETGID},
{"tw=", C_STICKY_OTH_WRITABLE},
{"ow=", C_OTHER_WRITABLE},
{"st=", C_STICKY},
{"ex=", C_EXEC},
{"lc=", C_LEFT},
{"rc=", C_RIGHT},
{"ec=", C_END},
{NULL, 0}
};
void
ls_parse_colors(void)
{
char *colors;
char *line;
int idx;
colors = getenv("LS_COLORS");
if (colors == NULL) return;
line = strtok(colors, ":");
while (line != NULL)
{
for (idx = 0; abrv2type[idx].abrv != NULL; idx++)
{
if (strncmp(abrv2type[idx].abrv, line, 3) == 0)
{
ls_colors[abrv2type[idx].type] = line + 3;
break;
}
}
line = strtok(NULL, ":");
}
}

24
tools/tools/ls/colors.h Normal file
View file

@ -0,0 +1,24 @@
#ifndef TOOLS_LS_COLORS_H
# define TOOLS_LS_COLORS_H 1
enum COLORS {
C_NORM,
C_FILE,
C_DIR,
C_LINK,
C_SETUID,
C_SETGID,
C_STICKY_OTH_WRITABLE,
C_OTHER_WRITABLE,
C_STICKY,
C_EXEC,
C_LEFT,
C_RIGHT,
C_END
};
extern char *ls_colors[];
void ls_parse_colors(void);
#endif /* !TOOLS_LS_COLORS_H */