micro-utils/src/ls.c
2024-07-09 15:43:55 +03:00

510 lines
9.8 KiB
C

#define _LS_C
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include "mode_to_str.h"
#include "mu_strlen.h"
#include "make_path.h"
#include "get_stat.h"
#include "config.h"
#include "human.h"
static char O_flag;
static char a_flag;
static char l_flag;
static char F_flag;
static char c_flag;
static char R_flag;
static char d_flag;
static char L_flag;
static char h_flag;
static char s_flag;
static char i_flag;
static char p_flag;
static char nul_flag;
static int sortd(const void *p1, const void *p2);
static int (*sorter)(const void *p1, const void *p2) = sortd;
struct d_node {
/* basename */
char *name;
/* For free */
char *full_name;
struct d_node *next;
struct stat stats;
};
/* Work with dir */
static struct d_node *stat_file(char *filename, const int lfile) {
/* lfile its flag. 1 if passed file from list_one() */
struct d_node *file = malloc(sizeof(struct d_node));
if (file == NULL) {
fprintf(stderr, "ls: malloc: %s\n", strerror(errno));
return NULL;
}
if (mu_get_stats("ls", !L_flag, filename, &file->stats)) {
free(file);
return NULL;
}
file->full_name = (lfile) ? strdup(filename) : filename;
file->name = strrchr(filename, '/');
if (file->name == NULL || lfile)
file->name = filename;
else
file->name++;
return file;
}
static void dfree(struct d_node **dir, const size_t files) {
for (size_t i = 0; i < files; i++) {
if (dir[i] != NULL && dir[i]->full_name != NULL)
free(dir[i]->full_name);
if (dir[i] != NULL)
free(dir[i]);
}
free(dir);
}
static struct d_node **list(const char *path, off_t *total_size, size_t *nfiles, int *ret) {
DIR *dp = opendir(path);
if (dp == NULL) {
fprintf(stderr, "ls: %s: %s\n", path, strerror(errno));
return NULL;
}
struct d_node **dn = malloc(sizeof(struct d_node *));
if (dn == NULL) {
fprintf(stderr, "ls: malloc: %s\n", strerror(errno));
return NULL;
}
struct dirent *ep;
while ((ep = readdir(dp)) != NULL) {
if (ep->d_name[0] == '.' && !a_flag)
continue;
char *full_path = mu_make_path("ls", path, ep->d_name);
if (full_path == NULL)
continue;
struct d_node **bckp = realloc(dn, sizeof(struct d_node *) * (*nfiles + 1));
if (bckp == NULL) {
free(full_path);
dfree(dn, *nfiles);
closedir(dp);
fprintf(stderr, "ls: realloc: %s\n", strerror(errno));
return NULL;
}
dn = bckp;
dn[*nfiles] = stat_file(full_path, 0);
if (dn[*nfiles] == NULL) {
*ret = 1;
free(full_path);
continue;
}
*total_size += dn[*nfiles]->stats.st_blocks;
(*nfiles)++;
}
closedir(dp);
return dn;
}
static char *get_date(const time_t mtime) {
static char date[100];
strftime(date, sizeof(date), "%b %d %H:%M", localtime(&mtime));
return date;
}
/* Print */
static int print(const struct d_node *node) {
char suf = ' ';
char *color = "";
char *mode = mu_mode_2_str(node->stats.st_mode);
if (S_ISDIR(node->stats.st_mode)) {
if (node->name[strlen(node->name) - 1] != '/')
suf = '/';
mode[0] = 'd';
color = LS_DIR_COLOR;
}
else if (S_ISLNK(node->stats.st_mode)) {
suf = '@';
mode[0] = 'l';
color = LS_LINK_COLOR;
}
else if (S_ISSOCK(node->stats.st_mode)) {
suf = '=';
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 = '|';
mode[0] = 'p';
color = LS_FIFO_COLOR;
}
else if ((node->stats.st_mode & S_IXUSR) || (node->stats.st_mode & S_IXGRP) || (node->stats.st_mode & S_IXOTH)) {
suf = '*';
color = LS_EXE_COLOR;
}
int ret = 0;
if (i_flag)
ret += printf("%7ju ", (uintmax_t)node->stats.st_ino);
if (s_flag) {
off_t size = 512 * node->stats.st_blocks;
if (h_flag)
ret += printf("%7s ", mu_humansize(size, 1024));
else
ret += printf("%7jd ", size / 1024);
}
if (l_flag) {
printf("%s", mode);
struct passwd *pw = getpwuid(node->stats.st_uid);
struct group *gr = getgrgid(node->stats.st_gid);
if (pw == NULL || gr == NULL) {
fprintf(stderr, "ls: print: %s\n", strerror(errno));
return -1;
}
if (h_flag)
ret += printf(" %4ju %4s %6s %6s %s ", (uintmax_t)node->stats.st_nlink, pw->pw_name, gr->gr_name, mu_humansize(node->stats.st_size, 1024), get_date(node->stats.st_mtime));
else
ret += printf(" %4ju %4s %6s %10ld %s ", (uintmax_t)node->stats.st_nlink, pw->pw_name, gr->gr_name, node->stats.st_size, get_date(node->stats.st_mtime));
}
if (c_flag && p_flag)
printf("%s", color);
printf("%s", node->name);
ret += mu_strlen(node->name);
if (c_flag && p_flag)
printf("\033[0m");
if (F_flag)
printf("%c", suf);
return ret;
}
static int col_print(struct d_node **node, const size_t files, const struct winsize w) {
/* Get max len */
size_t maxlen = 0;
for (size_t i = 0; i < files; i++) {
size_t len = mu_strlen(node[i]->name);
if (len > maxlen)
maxlen = len;
}
/* Calc */
maxlen += 3;
if (i_flag)
maxlen += 10;
if (s_flag)
maxlen += 10;
size_t ncols = w.ws_col / maxlen;
size_t nrows = files;
if (ncols > 1) {
nrows = files / ncols;
if (nrows * ncols < files)
nrows++;
}
else
ncols = 1;
int col = 0;
int nexttab = 0;
/* Mc print */
for (size_t i = 0; i < nrows; i++) {
for (size_t j = 0; j < ncols; j++) {
size_t index = j * nrows + i;
if (index < files) {
if (col > 0) {
nexttab -= col;
col += nexttab;
for (int k = 0; k < nexttab; k++)
putchar(' ');
}
nexttab = col + (int)maxlen;
int ret = print(node[index]);
if (ret == -1)
return 1;
col += ret;
}
}
putchar((nul_flag) ? '\0' : '\n');
col = 0;
}
return 0;
}
static int struct_print(struct d_node **dir, const size_t files, const struct winsize w) {
int ret = 0;
/* pipe print */
if (!p_flag || l_flag || O_flag) {
for (size_t i = 0; i < files; i++) {
if (print(dir[i]) == -1)
ret = 1;
putchar((nul_flag) ? '\0' : '\n');
}
}
else
if (col_print(dir, files, w))
ret = 1;
return ret;
}
/* Sort */
static int sortt(const void *p1, const void *p2) {
return (*(struct d_node **)p2)->stats.st_mtime - (*(struct d_node **)p1)->stats.st_mtime;
}
static int sorts(const void *p1, const void *p2) {
return (*(struct d_node **)p2)->stats.st_size - (*(struct d_node **)p1)->stats.st_size;
}
static int sortd(const void *p1, const void *p2) {
return strcmp((*(struct d_node **)p1)->full_name, (*(struct d_node **)p2)->full_name);
}
/* Ls */
static int ls_dir(const char *dir_name, const int label, const struct winsize w) {
if (dir_name == NULL)
return 0;
int ret = 0;
size_t files = 0;
off_t total_size = 0;
struct d_node **dir = list(dir_name, &total_size, &files, &ret);
/* Title */
if ((label || R_flag) && !d_flag)
printf("\n%s:\n", dir_name);
if (l_flag) {
if (h_flag)
printf("total: %s\n", mu_humansize(total_size * 512, 1024));
else
printf("total: %jd\n", (intmax_t)total_size / 2);
}
if (dir == NULL)
return 1;
qsort(dir, files, sizeof(struct d_node *), sorter);
if (struct_print(dir, files, w))
ret = 1;
if (R_flag)
for (size_t i = 0; i < files; i++)
if (S_ISDIR(dir[i]->stats.st_mode) && strcmp(dir[i]->name, "..") && strcmp(dir[i]->name, "."))
ls_dir(dir[i]->full_name, 1, w);
dfree(dir, files);
return ret;
}
static int ls_files(int argc, char **argv, const struct winsize w) {
size_t files = 0;
struct d_node **file = malloc(sizeof(struct d_node *));
if (file == NULL) {
fprintf(stderr, "ls: malloc: %s\n", strerror(errno));
return 1;
}
int ret = 0;
for (int i = 0; i < argc; i++) {
struct stat sb;
if (mu_get_lstat("ls", argv[i], &sb)) {
ret = 1;
argv[i] = NULL;
continue;
}
if (S_ISDIR(sb.st_mode))
continue;
struct d_node **tmp = realloc(file, sizeof(struct d_node *) * (files + 2));
if (tmp == NULL) {
dfree(file, files);
argv[i] = NULL;
return 1;
}
file = tmp;
file[files] = stat_file(argv[i], 1);
if (file[files] == NULL) {
argv[i] = NULL;
continue;
}
files++;
argv[i] = NULL;
}
if (files) {
qsort(file, files, sizeof(struct d_node *), sorter);
if (struct_print(file, files, w))
ret = 1;
}
dfree(file, files);
return ret;
}
int main(int argc, char **argv) {
int opt;
while ((opt = getopt(argc, argv, "1alFcRdLhistS0")) != -1) {
switch (opt) {
case '1':
O_flag = 1;
break;
case 'a':
a_flag = 1;
break;
case 'l':
l_flag = 1;
break;
case 'F':
F_flag = 1;
break;
case 'c':
c_flag = 1;
break;
case 'R':
d_flag = 0;
R_flag = 1;
break;
case 'd':
R_flag = 0;
d_flag = 1;
break;
case 'L':
L_flag = 1;
break;
case 'h':
h_flag = 1;
break;
case 'i':
i_flag = 1;
break;
case 's':
s_flag = 1;
break;
case 't':
sorter = sortt;
break;
case 'S':
sorter = sorts;
break;
case '0':
nul_flag = 1;
break;
default:
puts("ls [1alFcRdLhistS0] [dir1 file2...]\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\t-R Recursive\n\t-1 One column\n\t-d Print only dir names\n\t-L Follow symlinks\n\t-h Sizes in human readable format\n\t-i Listen inodes\n\t-t Sort by mtime\n\t-S Sort by size\n\t-s Print file size\n\t-0 End line with \\0");
return 0;
}
}
argv += optind;
argc -= optind;
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
/* Check if programm piped, 1 - false, 0 - true */
p_flag = isatty(STDOUT_FILENO);
int ret = 0;
if (ls_files(argc, argv, w))
ret = 1;
if (argc == 0) {
if (ls_dir(".", 0, w))
ret = 1;
}
else if (argc == 1) {
if (ls_dir(argv[0], 0, w))
ret = 1;
}
else {
for (int i = 0; i < argc; i++)
if (ls_dir(argv[i], 1, w))
ret = 1;
}
return ret;
}