lazysphere/src/commands/ls.c

553 lines
14 KiB
C
Raw Normal View History

2023-04-28 04:36:15 +00:00
#include "../command.h"
#include <grp.h>
#include <pwd.h>
#include <dirent.h>
#include <ftw.h>
#include <limits.h>
2023-05-02 22:02:47 +00:00
#include <string.h>
2023-04-28 04:36:15 +00:00
2023-04-30 06:12:02 +00:00
#define FILE_COLOR ANSCII BLACK COLOR
#define DIR_COLOR ANSCII BOLD NEXT NORMAL BLUE COLOR
#define DIR_COLOR_EXEC ANSCII BACKGROUND GREEN NEXT NORMAL BLACK COLOR
2023-05-02 04:37:30 +00:00
#define LINK_COLOR ANSCII BOLD NEXT NORMAL TURQUOISE COLOR
2023-04-30 06:12:02 +00:00
#define SET_UID_COLOR ANSCII BACKGROUND RED NEXT NORMAL WHITE COLOR
#define SET_GID_COLOR ANSCII BACKGROUND YELLOW NEXT NORMAL BLACK COLOR
#define EXEC_COLOR ANSCII BOLD NEXT NORMAL GREEN COLOR
#define BLK_COLOR ANSCII BOLD NEXT NORMAL YELLOW COLOR
2023-05-02 04:37:30 +00:00
#define SOCK_COLOR ANSCII BOLD NEXT NORMAL MAGENTA COLOR
2023-04-28 04:36:15 +00:00
2023-05-01 22:43:32 +00:00
static struct {
2023-04-28 04:36:15 +00:00
bool hidden;
bool hide_dot;
bool more_info;
bool one_column;
bool recurse;
2023-04-30 06:12:02 +00:00
enum When colored;
2023-05-01 22:43:32 +00:00
} flags;
2023-04-28 04:36:15 +00:00
2023-04-28 16:31:49 +00:00
struct FileInfo {
struct passwd* usr;
struct group* grp;
2023-05-02 22:02:47 +00:00
char* name;
2023-04-30 06:12:02 +00:00
char date[13];
char mode[11];
char size[5];
int links;
2023-05-02 04:37:30 +00:00
int bytes;
2023-04-28 16:31:49 +00:00
bool set_uid;
bool set_gid;
bool exec;
2023-04-29 00:32:18 +00:00
unsigned char type;
2023-04-28 16:31:49 +00:00
};
struct FileListInfo {
int max_link;
int max_usr;
int max_grp;
int max_size;
int max_name;
int total_len;
2023-05-02 04:37:30 +00:00
int total_size;
2023-04-28 16:31:49 +00:00
};
2023-04-28 04:36:15 +00:00
static DIR* get_directory(char* path) {
DIR* d = opendir(path);
if (d == NULL) {
2023-04-28 17:11:56 +00:00
if (errno == ENOTDIR) {
2023-05-03 16:17:56 +00:00
error_s("`%s` is a a file\n", path);
2023-04-28 17:11:56 +00:00
} else {
2023-05-03 16:17:56 +00:00
error_s("failed to open directory '%s': %s\n", path, strerror(errno));
2023-04-28 17:11:56 +00:00
}
2023-04-28 04:36:15 +00:00
}
return d;
}
2023-05-02 22:02:47 +00:00
static bool get_file_info(const char* file_name, struct FileInfo* info) {
2023-04-28 04:36:15 +00:00
2023-05-04 20:10:37 +00:00
uid_t uid;
gid_t gid;
int save, ty;
2023-04-28 16:31:49 +00:00
struct stat s;
2023-05-04 20:10:37 +00:00
size_t file_len;
uid = getuid();
gid = getgid();
2023-04-29 00:32:18 +00:00
memset(&s, 0, sizeof(struct stat));
2023-05-04 20:10:37 +00:00
save = push_path_buffer(file_name);
2023-05-02 22:02:47 +00:00
if (lstat(get_path_buffer(), &s) < 0) {
2023-05-03 16:17:56 +00:00
error_s("failed to read file '%s': %s\n", get_path_buffer(), strerror(errno));
2023-05-02 22:02:47 +00:00
pop_path_buffer(save);
2023-04-28 16:31:49 +00:00
return false;
}
2023-05-02 22:02:47 +00:00
2023-05-04 20:10:37 +00:00
ty = (s.st_mode & S_IFMT) >> 12;
2023-04-30 06:12:02 +00:00
2023-04-28 16:31:49 +00:00
info->set_uid = false;
info->set_gid = false;
info->exec = false;
2023-04-30 06:12:02 +00:00
switch (ty) {
case DT_BLK:
info->mode[0] = 'b';
break;
case DT_CHR:
info->mode[0] = 'c';
break;
case DT_DIR:
info->mode[0] = 'd';
break;
case DT_FIFO:
info->mode[0] = 'f';
break;
case DT_LNK:
info->mode[0] = 'l';
break;
case DT_SOCK:
info->mode[0] = 's';
break;
case DT_UNKNOWN:
info->mode[0] = 'u';
break;
case DT_WHT:
info->mode[0] = 'w';
break;
default:
info->mode[0] = '-';
break;
}
2023-04-28 16:31:49 +00:00
info->mode[1] = (s.st_mode & S_IRUSR) ? 'r' : '-';
info->mode[2] = (s.st_mode & S_IWUSR) ? 'w' : '-';
if (s.st_mode & S_IXUSR) {
if (s.st_mode & S_ISUID) {
info->mode[3] = 's';
info->set_uid = true;
2023-04-28 04:36:15 +00:00
} else {
2023-04-28 16:31:49 +00:00
info->mode[3] = 'x';
2023-04-28 04:36:15 +00:00
}
2023-04-28 16:31:49 +00:00
if (!info->exec) info->exec = s.st_uid == uid;
} else {
info->mode[3] = '-';
}
2023-04-28 04:36:15 +00:00
2023-04-28 16:31:49 +00:00
info->mode[4] = (s.st_mode & S_IRGRP) ? 'r' : '-';
info->mode[5] = (s.st_mode & S_IWGRP) ? 'w' : '-';
if (s.st_mode & S_IXGRP) {
if (s.st_mode & S_ISGID) {
info->mode[6] = 's';
info->set_gid = true;
2023-04-28 04:36:15 +00:00
} else {
2023-04-28 16:31:49 +00:00
info->mode[6] = 'x';
2023-04-28 04:36:15 +00:00
}
2023-04-28 16:31:49 +00:00
if (!info->exec) info->exec = s.st_gid == gid;
} else {
info->mode[6] = '-';
}
2023-04-28 04:36:15 +00:00
2023-04-28 16:31:49 +00:00
info->mode[7] = (s.st_mode & S_IROTH) ? 'r' : '-';
info->mode[8] = (s.st_mode & S_IWOTH) ? 'w' : '-';
if (s.st_mode & S_IXOTH) {
info->mode[9] = 'x';
info->exec = true;
} else {
info->mode[9] = '-';
}
2023-04-28 04:36:15 +00:00
2023-04-28 16:31:49 +00:00
info->mode[10] = '\0';
2023-04-28 04:36:15 +00:00
2023-04-28 16:31:49 +00:00
info->usr = getpwuid(s.st_uid);
2023-05-02 22:02:47 +00:00
if (info->usr == NULL) {
2023-05-03 16:17:56 +00:00
error_s("failed to get user from `%s`\n", get_path_buffer());
2023-05-02 22:02:47 +00:00
pop_path_buffer(save);
return false;
}
2023-04-28 16:31:49 +00:00
info->grp = getgrgid(s.st_gid);
2023-05-02 22:02:47 +00:00
if (info->grp == NULL) {
2023-05-03 16:17:56 +00:00
error_s("failed to get user from `%s`\n", get_path_buffer());
2023-05-02 22:02:47 +00:00
pop_path_buffer(save);
return false;
}
2023-04-28 16:31:49 +00:00
info->links = s.st_nlink;
2023-04-30 06:12:02 +00:00
info->type = ty;
2023-04-29 00:32:18 +00:00
2023-05-04 20:10:37 +00:00
file_len = strlen(file_name) + 1;
2023-05-02 22:02:47 +00:00
info->name = malloc(file_len);
memcpy(info->name, file_name, file_len);
2023-04-28 04:36:15 +00:00
2023-04-28 16:31:49 +00:00
print_file_size(s.st_size, info->size);
2023-04-30 06:12:02 +00:00
print_date_time(s.st_mtim.tv_sec + s.st_mtim.tv_nsec / 1000000000, info->date);
2023-05-02 04:37:30 +00:00
2023-05-02 22:02:47 +00:00
info->bytes = (s.st_size + s.st_blksize - 1) / s.st_blksize;
2023-05-02 04:37:30 +00:00
2023-05-02 22:02:47 +00:00
pop_path_buffer(save);
2023-04-28 16:31:49 +00:00
return true;
}
2023-04-28 04:36:15 +00:00
2023-04-28 16:31:49 +00:00
static char* get_file_color(struct FileInfo* info) {
char* color;
2023-04-29 00:32:18 +00:00
if (info->type == DT_DIR) {
2023-04-30 06:12:02 +00:00
if (info->mode[8] == 'w') {
color = DIR_COLOR_EXEC;
} else {
color = DIR_COLOR;
}
2023-04-29 00:32:18 +00:00
} else if (info->type == DT_LNK) {
2023-04-28 16:31:49 +00:00
color = LINK_COLOR;
2023-05-02 04:37:30 +00:00
} else if (info->type == DT_SOCK) {
color = SOCK_COLOR;
2023-04-30 06:12:02 +00:00
} else if (
info->type == DT_CHR ||
2023-05-02 04:37:30 +00:00
info->type == DT_BLK
2023-04-30 06:12:02 +00:00
) {
color = BLK_COLOR;
2023-04-28 16:31:49 +00:00
} else {
if (info->set_uid) {
color = SET_UID_COLOR;
} else if (info->set_gid) {
color = SET_GID_COLOR;
} else if (info->exec) {
color = EXEC_COLOR;
} else {
color = FILE_COLOR;
2023-04-28 04:36:15 +00:00
}
2023-04-28 16:31:49 +00:00
}
return color;
}
2023-04-28 04:36:15 +00:00
2023-05-02 22:02:47 +00:00
static void list_files(struct FileInfo* files, int file_len, struct FileListInfo info) {
2023-05-02 04:37:30 +00:00
2023-05-04 20:10:37 +00:00
struct winsize w;
char* color;
int column_width, row_count, i;
2023-05-02 04:37:30 +00:00
if (flags.more_info) {
char total[13];
print_file_size(info.total_size, total);
printf("total %s\n", total);
}
2023-04-28 16:31:49 +00:00
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
2023-04-28 19:48:55 +00:00
if (!isatty(1)) {
2023-05-01 22:43:32 +00:00
flags.one_column = true;
if (flags.colored == AUTO)
flags.colored = NO;
2023-04-28 19:48:55 +00:00
}
2023-05-04 20:10:37 +00:00
column_width = info.max_name + 1;
row_count = w.ws_col / column_width;
2023-04-28 16:31:49 +00:00
2023-05-04 20:10:37 +00:00
for (i = 0; i < file_len; i++) {
2023-04-28 16:31:49 +00:00
struct FileInfo finfo = files[i];
color = get_file_color(&finfo);
2023-05-01 22:43:32 +00:00
if (flags.more_info) {
2023-04-30 06:12:02 +00:00
printf("%s %*d %*s %*s %*s %s %s%s%s",
2023-04-28 16:31:49 +00:00
finfo.mode,
info.max_link,
finfo.links,
info.max_usr,
finfo.usr->pw_name,
info.max_grp,
finfo.grp->gr_name,
info.max_size,
finfo.size,
finfo.date,
2023-05-01 22:43:32 +00:00
flags.colored != NO ? color : "",
2023-04-30 06:12:02 +00:00
finfo.name,
2023-05-01 22:43:32 +00:00
flags.colored != NO ? "\x1b[0m" : ""
2023-04-28 16:31:49 +00:00
);
2023-04-29 00:32:18 +00:00
if (finfo.type == DT_LNK) {
2023-05-02 22:02:47 +00:00
int save = push_path_buffer(finfo.name);
2023-04-28 16:31:49 +00:00
char lnk[PATH_MAX];
2023-04-30 06:12:02 +00:00
ssize_t n;
2023-05-02 22:02:47 +00:00
if ((n = readlink(get_path_buffer(), lnk, PATH_MAX)) != -1) {
2023-04-30 06:12:02 +00:00
printf(" -> %.*s\n", (int)n, lnk);
2023-04-28 16:31:49 +00:00
} else {
2023-04-30 06:12:02 +00:00
putchar('\n');
2023-04-28 16:31:49 +00:00
}
2023-05-02 22:02:47 +00:00
pop_path_buffer(save);
2023-04-28 04:36:15 +00:00
} else {
2023-04-28 16:31:49 +00:00
putchar('\n');
2023-04-28 04:36:15 +00:00
}
2023-05-01 22:43:32 +00:00
} else if (flags.one_column) {
printf("%s%s%s\n", flags.colored != NO ? color : "", finfo.name, flags.colored != NO ? "\x1b[0m" : "");
2023-04-28 04:36:15 +00:00
} else {
2023-04-28 16:31:49 +00:00
if (info.total_len > w.ws_col) {
if (i != 0 && i % row_count == 0) putchar('\n');
2023-05-01 22:43:32 +00:00
printf("%s%*s%s", flags.colored != NO ? color : "", -column_width,
finfo.name, flags.colored != NO ? "\x1b[0m" : "");
2023-04-28 16:31:49 +00:00
} else {
2023-05-01 22:43:32 +00:00
printf("%s%s%s ", flags.colored != NO ? color : "", finfo.name,
flags.colored != NO ? "\x1b[0m" : "");
2023-04-28 16:31:49 +00:00
}
2023-04-28 04:36:15 +00:00
}
2023-05-02 22:02:47 +00:00
free(finfo.name);
2023-04-28 04:36:15 +00:00
}
2023-05-01 22:43:32 +00:00
if (!flags.more_info) printf("\n");
2023-04-28 04:36:15 +00:00
}
2023-04-28 16:31:49 +00:00
static int num_places (int n) {
int r = 1;
if (n < 0) n = (n == INT_MIN) ? INT_MAX: -n;
while (n > 9) {
n /= 10;
r++;
}
return r;
}
static void push_file(
struct FileInfo** files,
struct FileListInfo* info,
int* size, int* capacity,
2023-05-02 22:02:47 +00:00
const char* file_path
2023-04-28 16:31:49 +00:00
) {
struct FileInfo finfo;
2023-05-04 20:10:37 +00:00
int user_len, group_len, name_len, size_len, link_len;
2023-05-02 22:02:47 +00:00
if (!get_file_info(file_path, &finfo)) return;
2023-04-28 16:31:49 +00:00
if (*size == *capacity) {
*capacity *= 2;
*files = realloc(*files, sizeof(struct FileInfo) * *capacity);
}
2023-05-04 20:10:37 +00:00
user_len = strlen(finfo.usr->pw_name);
2023-04-28 16:31:49 +00:00
if (user_len > info->max_usr) info->max_usr = user_len;
2023-05-04 20:10:37 +00:00
group_len = strlen(finfo.grp->gr_name);
2023-04-28 16:31:49 +00:00
if (group_len > info->max_grp) info->max_grp = group_len;
2023-05-04 20:10:37 +00:00
name_len = strlen(file_path);
2023-04-28 16:31:49 +00:00
if (name_len > info->max_name) info->max_name = name_len;
2023-05-04 20:10:37 +00:00
size_len = strlen(finfo.size);
2023-04-28 16:31:49 +00:00
if (size_len > info->max_size) info->max_size = size_len;
2023-05-04 20:10:37 +00:00
link_len = num_places(finfo.links);
2023-04-28 16:31:49 +00:00
if (link_len > info->max_link) info->max_link = link_len;
info->total_len += name_len + 2;
2023-05-02 04:37:30 +00:00
info->total_size += finfo.bytes;
2023-04-28 16:31:49 +00:00
(*files)[*size] = finfo;
(*size)++;
}
2023-05-02 22:02:47 +00:00
static void recurse_directory(char* dir_name) {
2023-04-28 04:36:15 +00:00
DIR* d;
2023-05-04 20:10:37 +00:00
int capacity, size, save;
2023-04-28 04:36:15 +00:00
struct dirent* file;
2023-05-04 20:10:37 +00:00
struct FileInfo* files;
struct FileListInfo info;
save = push_path_buffer(dir_name);
2023-04-28 04:36:15 +00:00
2023-05-02 22:02:47 +00:00
d = get_directory(get_path_buffer());
if (d == NULL) {
return;
}
2023-04-28 04:36:15 +00:00
2023-05-04 20:10:37 +00:00
capacity = 8;
size = 0;
files = malloc(sizeof(struct FileInfo) * capacity);
2023-04-28 16:31:49 +00:00
memset(&info, 0, sizeof(struct FileListInfo));
2023-04-28 04:36:15 +00:00
while((file = readdir(d)) != NULL) {
2023-05-01 22:43:32 +00:00
if (!flags.hidden && prefix(".", file->d_name)) continue;
2023-05-02 04:37:30 +00:00
if (flags.hide_dot && is_dot_dir(file->d_name)) continue;
2023-05-02 22:02:47 +00:00
if (file->d_type == DT_DIR && !is_dot_dir(file->d_name)) {
recurse_directory(file->d_name);
} else {
push_file(&files, &info, &size, &capacity, file->d_name);
2023-04-28 04:36:15 +00:00
}
}
2023-05-04 20:10:37 +00:00
if (flags.colored == NO) {
2023-05-02 22:02:47 +00:00
printf("\n%s:\n", get_path_buffer());
2023-05-04 20:10:37 +00:00
} else {
2023-05-02 22:02:47 +00:00
printf("\n%s%s:%s\n", DIR_COLOR, get_path_buffer(), FILE_COLOR);
2023-05-04 20:10:37 +00:00
}
2023-04-28 04:36:15 +00:00
2023-05-02 22:02:47 +00:00
list_files(files, size, info);
free(files);
if (!flags.more_info) printf("\n");
2023-04-28 04:36:15 +00:00
closedir(d);
2023-04-28 17:11:56 +00:00
2023-05-02 22:02:47 +00:00
pop_path_buffer(save);
2023-04-28 04:36:15 +00:00
}
2023-05-02 22:02:47 +00:00
static void list_directory(char* path) {
2023-05-04 20:10:37 +00:00
DIR* d;
int capacity, size, save;
struct FileInfo* files;
struct FileListInfo info;
struct dirent* file;
2023-05-01 22:43:32 +00:00
if (flags.recurse) {
2023-05-02 22:02:47 +00:00
recurse_directory(path);
return;
2023-04-28 04:36:15 +00:00
}
2023-05-04 20:10:37 +00:00
d = get_directory(path);
2023-05-02 22:02:47 +00:00
if (d == NULL) return;
2023-05-04 20:10:37 +00:00
save = push_path_buffer(path);
2023-04-28 04:36:15 +00:00
2023-05-04 20:10:37 +00:00
capacity = 8;
size = 0;
files = malloc(sizeof(struct FileInfo) * capacity);
2023-04-28 16:31:49 +00:00
memset(&info, 0, sizeof(struct FileListInfo));
2023-04-28 04:36:15 +00:00
while ((file = readdir(d)) != NULL) {
2023-05-01 22:43:32 +00:00
if (!flags.hidden && prefix(".", file->d_name)) continue;
if (flags.hide_dot && is_dot_dir(file->d_name)) continue;
2023-05-02 22:02:47 +00:00
push_file(&files, &info, &size, &capacity, file->d_name);
2023-04-28 04:36:15 +00:00
}
2023-05-02 22:02:47 +00:00
if (size > 0) list_files(files, size, info);
2023-04-28 16:31:49 +00:00
free(files);
2023-05-02 22:02:47 +00:00
pop_path_buffer(save);
2023-04-28 04:36:15 +00:00
closedir(d);
}
2023-04-30 06:12:02 +00:00
static bool is_dir(const char* path) {
struct stat s;
if (stat(path, &s) < 0) return false;
return S_ISDIR(s.st_mode);
}
2023-05-01 22:43:32 +00:00
static void list_file_args(int start, int argc, char** argv) {
2023-05-04 20:10:37 +00:00
int capacity, size, i;
struct FileInfo* files;
2023-04-30 06:12:02 +00:00
struct FileListInfo info;
2023-05-04 20:10:37 +00:00
capacity = 8;
size = 0;
files = malloc(sizeof(struct FileInfo) * capacity);
2023-04-30 06:12:02 +00:00
memset(&info, 0, sizeof(struct FileListInfo));
2023-05-04 20:10:37 +00:00
for (i = start; i < argc; i++) {
2023-04-30 06:12:02 +00:00
if (is_dir(argv[i])) continue;
2023-05-04 20:10:37 +00:00
push_file(&files, &info, &size, &capacity, argv[i]);
2023-04-30 06:12:02 +00:00
}
2023-05-02 22:02:47 +00:00
if (size > 0) list_files(files, size, info);
2023-04-30 06:12:02 +00:00
free(files);
}
2023-05-01 22:43:32 +00:00
static void help(void) {
2023-04-28 04:36:15 +00:00
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");
2023-05-01 22:43:32 +00:00
}
static int short_arg(char c, char* next) {
UNUSED(next);
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:
2023-05-03 16:17:56 +00:00
return ARG_INVALID;
2023-05-01 22:43:32 +00:00
}
return ARG_UNUSED;
}
static int long_arg(char* cur, char* next) {
UNUSED(next);
if (prefix("--color=", cur)) {
char* arg = cur + 8;
if (streql("yes", arg) || streql("always", arg)) {
flags.colored = YES;
} else if (streql("auto", arg)) {
flags.colored = AUTO;
} else if (streql("no", arg) || streql("never", arg)) {
flags.colored = NO;
} else {
2023-05-03 16:17:56 +00:00
error("invalid color options: %s", arg);
2023-05-01 22:43:32 +00:00
}
} else {
return ARG_IGNORE;
}
return ARG_UNUSED;
2023-04-28 04:36:15 +00:00
}
COMMAND(ls) {
2023-05-04 20:10:37 +00:00
int start, i;
bool titled;
2023-04-28 04:36:15 +00:00
flags.hidden = false;
flags.more_info = false;
flags.hide_dot = false;
flags.one_column = false;
flags.recurse = false;
2023-05-03 16:17:56 +00:00
flags.colored = NO;
2023-04-29 00:32:18 +00:00
2023-05-04 20:10:37 +00:00
start = parse_args(argc, argv, help, short_arg, long_arg);
2023-04-28 04:36:15 +00:00
if (argc - start == 0) {
2023-05-01 22:43:32 +00:00
list_directory(".");
2023-04-28 04:36:15 +00:00
return EXIT_SUCCESS;
}
2023-05-01 22:43:32 +00:00
list_file_args(start, argc, argv);
2023-04-30 06:12:02 +00:00
2023-05-04 20:10:37 +00:00
titled = argc - start > 1;
for (i = start; i < argc; i++) {
2023-04-30 06:12:02 +00:00
if (!is_dir(argv[i])) continue;
2023-05-04 20:10:37 +00:00
2023-04-28 04:36:15 +00:00
if (titled && !flags.recurse) {
2023-05-04 20:10:37 +00:00
if (flags.colored != NO) {
2023-04-30 06:12:02 +00:00
printf("\n%s%s:%s\n", DIR_COLOR, argv[i], FILE_COLOR);
2023-05-04 20:10:37 +00:00
} else {
2023-04-30 06:12:02 +00:00
printf("\n%s:\n", argv[i]);
2023-05-04 20:10:37 +00:00
}
2023-04-28 04:36:15 +00:00
}
2023-05-04 20:10:37 +00:00
2023-05-02 04:37:30 +00:00
list_directory(argv[i]);
2023-04-28 04:36:15 +00:00
}
return EXIT_SUCCESS;
}