micro-utils/coreutils/ls.c

264 lines
4.9 KiB
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 "make_path.h"
#include "get_stat.h"
unsigned int a_flag;
unsigned int l_flag;
unsigned int F_flag;
unsigned int p_flag;
struct d_node {
/* basename */
char *name;
/* For free */
char *full_name;
struct d_node *next;
struct stat stats;
};
/* Work with dir */
struct d_node *stat_file(char *filename) {
struct d_node *file = malloc(sizeof(struct d_node));
if (file == NULL)
return NULL;
if (mu_get_lstat("ls", filename, &file->stats))
return NULL;
file->full_name = filename;
file->name = strrchr(filename, '/') + 1;
return file;
}
struct d_node **list(const char *path, size_t *nfiles) {
DIR *dp = opendir(path);
if (dp == NULL) {
fprintf(stderr, "ls: %s: %s\n", path, strerror(errno));
return NULL;
}
struct d_node **dir, *cur, *dr = NULL;
size_t files = 0;
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;
cur = stat_file(full_path);
if (cur == NULL)
continue;
cur->next = dr;
dr = cur;
files++;
}
closedir(dp);
if (dr == NULL)
return NULL;
*nfiles = files;
dir = malloc((files + 1) * sizeof(struct d_node *));
if (dir == NULL) {
fprintf(stderr, "ls: malloc failed\n");
exit(1);
}
for (size_t i = 0; i < files; i++) {
dir[i] = cur;
cur = cur->next;
}
return dir;
}
void dfree(struct d_node **dir) {
struct d_node *cur = dir[0], *next;
while (cur != NULL) {
next = cur->next;
free(cur->full_name);
free(cur);
cur = next;
}
free(dir);
}
/* Print */
void GetPerm(struct stat sb) {
if (S_ISDIR(sb.st_mode))
putchar('d');
else if (S_ISFIFO(sb.st_mode))
putchar('p');
else if (S_ISSOCK(sb.st_mode))
putchar('s');
else
putchar('-');
printf("%c%c%c", (sb.st_mode & S_IRUSR) ? 'r' : '-', (sb.st_mode & S_IWUSR) ? 'w' : '-', (sb.st_mode & S_IXUSR) ? 'x' : '-');
printf("%c%c%c", (sb.st_mode & S_IRGRP) ? 'r' : '-', (sb.st_mode & S_IWOTH) ? 'w' : '-', (sb.st_mode & S_IXGRP) ? 'x' : '-');
printf("%c%c%c", (sb.st_mode & S_IROTH) ? 'r' : '-', (sb.st_mode & S_IWOTH) ? 'w' : '-', (sb.st_mode & S_IXOTH) ? 'x' : '-');
}
int print(const struct d_node *node) {
char suf = ' ';
if (F_flag) {
if (S_ISDIR(node->stats.st_mode))
suf = '/';
else if ((node->stats.st_mode & S_IXUSR) || (node->stats.st_mode & S_IXGRP) || (node->stats.st_mode & S_IXOTH))
suf = '*';
}
if (!l_flag)
return printf("%s%c", node->name, suf);
if (l_flag) {
GetPerm(node->stats);
struct tm *tm = localtime(&node->stats.st_mtime);
char date[14];
if (!strftime(date, sizeof(date), "%b %d %H:%M", tm))
return 0;
struct passwd *pw = getpwuid(node->stats.st_uid);
struct group *gr = getgrgid(node->stats.st_gid);
return printf(" %s %s %10jd %s %s%c", (pw != 0) ? pw->pw_name : "nobody", (gr != 0) ? gr->gr_name : "nobody", (uintmax_t)node->stats.st_size, date, node->name, suf);
}
return 0;
}
void col_print(struct d_node **node, size_t files, struct winsize w) {
/* Get max len */
size_t maxlen = 0;
for (size_t i = 0; i < files; i++)
if (strlen(node[i]->name) > maxlen)
maxlen = strlen(node[i]->name) + 3;
size_t ncols = w.ws_col / maxlen;
size_t nrows = files;
if (ncols > 1)
nrows = files / ncols;
int col = 0;
int nexttab = 0;
/* Mc print */
for (size_t i = 0; i < nrows; i++) {
for (size_t j = 0; j < ncols; j++) {
if (i < files) {
if (col > 0) {
nexttab -= col;
for (int k = 0; k < nexttab; k++)
putchar(' ');
col += nexttab;
}
nexttab = col + (int)maxlen;
col += print(node[i * ncols + j]);
}
}
putchar('\n');
col = 0;
}
}
int ls(const char *dir_name, int label, struct winsize w) {
size_t files = 0;
struct d_node **dir = list(dir_name, &files);
if (dir == NULL)
return 1;
if (label)
printf("\n%s:\n", dir_name);
/* pipe print */
if (!p_flag || l_flag) {
for (size_t i = 0; i < files; i++) {
print(dir[i]);
putchar('\n');
}
}
/* mc print */
else
col_print(dir, files, w);
dfree(dir);
return 0;
}
int main(int argc, char **argv) {
int opt;
while ((opt = getopt(argc, argv, "alF")) != -1) {
switch (opt) {
case 'a':
a_flag = 1;
break;
case 'l':
l_flag = 1;
break;
case 'F':
F_flag = 1;
break;
default:
printf("ls [path]\n\t[-a Show hidden files] [-l Use a long listing format]\n\t[-F Append indicator to names]\n");
return 0;
}
}
argv += optind;
argc -= optind;
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
/* Check if programm piped, 1 - flase, 0 - true */
p_flag = isatty(STDOUT_FILENO);
if (argc < 1)
ls(".", 0, w);
if (argc == 1)
ls(argv[0], 0, w);
else
for (int i = 0; i < argc; i++)
ls(argv[i], 1, w);
return 0;
}