diff --git a/readme.md b/readme.md index eb605d8..3dfe6e5 100644 --- a/readme.md +++ b/readme.md @@ -4,7 +4,7 @@ A terrible busybox/gnu coreutils clone. Currently the only supported commands are: -`dd`, `cat`, `yes`, `echo`, `printf`, `id`, `groups` +`dd`, `cat`, `yes`, `echo`, `printf`, `id`, `groups`, `ls` ## How to diff --git a/src/command.h b/src/command.h index 354f2ee..4a2c50b 100644 --- a/src/command.h +++ b/src/command.h @@ -16,3 +16,4 @@ COMMAND(echo); COMMAND(print); COMMAND_EMPTY(groups); COMMAND_EMPTY(user_id); +COMMAND(ls); diff --git a/src/commands/ls.c b/src/commands/ls.c new file mode 100644 index 0000000..beebd19 --- /dev/null +++ b/src/commands/ls.c @@ -0,0 +1,287 @@ +#include "../command.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define FILE_COLOR "\x1b[0m" +#define DIR_COLOR "\x1b[1;34m" +#define SET_UID_COLOR "\x1b[41m" +#define SET_GID_COLOR "\x1b[43m" +#define EXEC_COLOR "\x1b[1;92m" +#define LINK_COLOR "\x1b[1;96m" + +struct Flags { + bool hidden; + bool hide_dot; + bool more_info; + bool one_column; + bool recurse; +}; + +static DIR* get_directory(char* path) { + DIR* d = opendir(path); + if (d == NULL) { + printf("\x1b[0merror: failed to open directory '%s': %s\n", path, strerror(errno)); + } + return d; +} + +static void append_path(char buf[PATH_MAX], const char* dir_path, const struct dirent* file) { + size_t dir_len = strlen(dir_path); + memcpy(buf, dir_path, dir_len); + if (buf[dir_len-1] != '/') { + buf[dir_len] = '/'; + dir_len++; + } + memcpy(buf + dir_len, file->d_name, strlen(file->d_name) + 1); +} + +static void list_file(struct dirent* file, struct Flags* flags, const char* dir_path) { + + char* color; + + if (file->d_type == DT_DIR) { + color = DIR_COLOR; + } else if (file->d_type == DT_LNK) { + color = LINK_COLOR; + } else { + color = FILE_COLOR; + } + + if (flags->more_info) { + + __uid_t uid = getuid(); + __gid_t gid = getgid(); + + char buf[PATH_MAX]; + append_path(buf, dir_path, file); + + struct stat s; + if (stat(buf, &s) < 0) { + printf("\x1b[0merror: failed to read file '%s': %s\n", buf, strerror(errno)); + return; + } + + if (file->d_type == DT_DIR) putchar('d'); + else if (file->d_type == DT_LNK) putchar('l'); + else putchar('-'); + + bool group_sticky = false; + bool user_sticky = false; + bool exectuable = false; + + putchar((s.st_mode & S_IRUSR) ? 'r' : '-'); + putchar((s.st_mode & S_IWUSR) ? 'w' : '-'); + if (s.st_mode & S_IXUSR) { + if (putchar(s.st_mode & S_ISUID)) { + putchar('s'); + user_sticky = true; + } else { + putchar('x'); + } + if (!exectuable) exectuable = s.st_uid == uid; + } else { + putchar('-'); + } + + putchar((s.st_mode & S_IRGRP) ? 'r' : '-'); + putchar((s.st_mode & S_IWGRP) ? 'w' : '-'); + if (s.st_mode & S_IXGRP) { + if (putchar(s.st_mode & S_ISGID)) { + putchar('s'); + group_sticky = true; + } else { + putchar('x'); + } + if (!exectuable) exectuable = s.st_gid == gid; + } else { + putchar('-'); + } + + putchar((s.st_mode & S_IROTH) ? 'r' : '-'); + putchar((s.st_mode & S_IWOTH) ? 'w' : '-'); + if (s.st_mode & S_IXOTH) { + putchar('x'); + exectuable = true; + } else { + putchar('-'); + } + + printf(" %ld\t", s.st_nlink); + + struct passwd* passwd = getpwuid(s.st_uid); + struct group* group = getgrgid(s.st_gid); + + printf("%s ", passwd->pw_name); + printf("%s ", group->gr_name); + + print_file_size(s.st_size); + print_date_time(s.st_mtim.tv_sec + s.st_mtim.tv_nsec / 1000000); + + if (file->d_type == DT_REG) { + if (user_sticky) { + color = SET_UID_COLOR; + } else if (group_sticky) { + color = SET_GID_COLOR; + } else if (exectuable) { + color = EXEC_COLOR; + } + } + + printf("%s%s\x1b[0m", color, file->d_name); + + if (file->d_type == DT_LNK) { + char link[PATH_MAX]; + if (readlink(buf, link, PATH_MAX) < 0) { + printf("\n"); + } else { + printf(" -> %s\n", link); + } + } else { + printf("\n"); + } + + } else { + printf("%s%s", color, file->d_name); + flags->one_column ? putchar('\n') : putchar(' '); + } +} + +static bool is_dot_dir(const char* path) { + return streql(path, ".") || streql(path, ".."); +} + +static void recurse_directory(char* path, struct Flags* flags) { + DIR* d; + struct dirent* file; + bool first = true; + + d = get_directory(path); + if (d == NULL) return; + + while((file = readdir(d)) != NULL) { + if (file->d_type == DT_DIR) continue; + if (!flags->hidden && prefix(".", file->d_name)) continue; + if (is_dot_dir(file->d_name)) continue; + if (first) { + printf("\n%s%s:%s\n", DIR_COLOR, path, FILE_COLOR); + first = false; + } + list_file(file, flags, path); + } + + if (!flags->more_info) printf("\n"); + + closedir(d); + + d = get_directory(path); + if (d == NULL) return; + + while((file = readdir(d)) != NULL) { + if (file->d_type != DT_DIR) continue; + if (!flags->hidden && prefix(".", file->d_name)) continue; + if (is_dot_dir(file->d_name)) continue; + char buf[PATH_MAX]; + append_path(buf, path, file); + recurse_directory(buf, flags); + } + + closedir(d); +} + +static void list_directory(char* path, struct Flags* flags) { + if (flags->recurse) { + recurse_directory(path, flags); + return; + } + + DIR* d = get_directory(path); + if (d == NULL) return; + + struct dirent* file; + + while ((file = readdir(d)) != NULL) { + if (!flags->hidden && prefix(".", file->d_name)) continue; + if (flags->hide_dot && is_dot_dir(file->d_name)) continue; + list_file(file, flags, path); + } + + closedir(d); +} + +static void help() { + printf("Usage: ls [FILE]...\n\n"); + printf("List directory contents\n\n"); + printf("\t-1\tOne column output\n"); + printf("\t-l\tLong format\n"); + printf("\t-a\tInclude names starting with .\n"); + printf("\t-A\tLike -a but without . and ..\n"); + printf("\t-R\tRecurse\n"); + exit(EXIT_SUCCESS); +} + +COMMAND(ls) { + + struct Flags flags; + flags.hidden = false; + flags.more_info = false; + flags.hide_dot = false; + flags.one_column = false; + flags.recurse = false; + + int start = 0; + for (int i = 0; i < argc; i++) { + if (!prefix("-", argv[i])) break; + if (streql("--help", argv[i])) help(); + start++; + for (size_t j = 1; j < strlen(argv[i]); j++) { + char c = argv[i][j]; + switch (c) { + case 'R': + flags.recurse = true; + break; + case '1': + flags.one_column = true; + break; + case 'A': + flags.hide_dot = true; + flags.hidden = true; + break; + case 'a': + flags.hidden = true; + break; + case 'l': + flags.more_info = true; + break; + default: + error("error: unkown option -%c", c); + } + } + } + + if (argc - start == 0) { + list_directory(".", &flags); + if (!flags.more_info) printf("\n"); + return EXIT_SUCCESS; + } + + bool titled = argc - start > 1; + for (int i = start; i < argc; i++) { + if (titled && !flags.recurse) { + printf("\n%s%s:%s\n", DIR_COLOR, argv[i], FILE_COLOR); + } + list_directory(argv[i], &flags); + if (titled && !flags.recurse) printf("\n"); + } + + if (!flags.more_info) printf("\n"); + return EXIT_SUCCESS; +} diff --git a/src/main.c b/src/main.c index 28961c9..63aa775 100644 --- a/src/main.c +++ b/src/main.c @@ -20,7 +20,7 @@ int main (ARGUMENTS) { if (argc < 2) { printf("usage: lazysphere [function [arguments]...]\n\n"); printf("currently defined functions:\n"); - printf("\tdd, cat, yes, echo, printf, id, groups\n"); + printf("\tdd, cat, yes, echo, printf, id, groups, ls\n"); return EXIT_SUCCESS; } argc--; @@ -48,6 +48,8 @@ int main (ARGUMENTS) { return groups(); } else if (streql(cmd, "id")) { return user_id(); + } else if (streql(cmd, "ls")) { + return ls(NEXT_ARGS); } else { error("error: invalid command %s", cmd); } diff --git a/src/shared.c b/src/shared.c index ee3c2f1..a8837c4 100644 --- a/src/shared.c +++ b/src/shared.c @@ -4,6 +4,7 @@ #include #include #include +#include void error(const char* format, ...) { va_list list; @@ -44,3 +45,41 @@ bool streql(const char* a, const char* b) { bool prefix(const char* pre, const char* str) { return strncmp(pre, str, strlen(pre)) == 0; } + +static char fs_types[5] = {'K','M','G','T','P'}; +void print_file_size(size_t bytes) { + int index = 0; + float next = bytes; + while (true) { + if (next < 1000) { + break; + } + if (index == 5) { + printf("999P"); + return; + }; + next /= 1024; + index++; + } + + if (next/100 < 1) putchar(' '); + if (next/10 < 1) putchar(' '); + if (index == 0) putchar(' '); + + printf("%u", (int)(next+.5)); + if (index > 0) { + putchar(fs_types[index - 1]); + } + putchar(' '); +} + +static char* months[12] = + {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; +void print_date_time(time_t mills) { + struct tm* info; + info = localtime(&mills); + printf("%s ", months[info->tm_mon]); + if (info->tm_mday < 10) + printf(" "); + printf("%d %02d:%02d ", info->tm_mday, info->tm_hour, info->tm_sec); +} diff --git a/src/shared.h b/src/shared.h index 430af4b..56f0a70 100644 --- a/src/shared.h +++ b/src/shared.h @@ -2,14 +2,16 @@ #include #include +#include __attribute__ ((__format__(printf, 1, 2))) void error(const char* format, ...); FILE* get_file(const char* path, const char* type); - long int get_number(const char* text); bool streql(const char* a, const char* b); - bool prefix(const char* pre, const char* str); + +void print_file_size(size_t bytes); +void print_date_time(time_t mills);