micro-utils/coreutils/ls.c

231 lines
4.3 KiB
C
Raw Normal View History

2023-10-20 19:24:35 +00:00
#include <pwd.h>
#include <grp.h>
2023-10-19 21:19:46 +00:00
#include <time.h>
#include <stdio.h>
#include <errno.h>
2023-10-30 15:45:24 +00:00
#include <stdlib.h>
#include <string.h>
2023-10-20 13:59:07 +00:00
#include <stdint.h>
2023-10-20 14:32:50 +00:00
#include <unistd.h>
#include <dirent.h>
2023-11-08 16:47:09 +00:00
#include <sys/stat.h>
#include <sys/types.h>
2023-11-09 12:09:13 +00:00
#include <sys/ioctl.h>
2023-10-31 09:53:32 +00:00
#include "make_path.h"
#include "get_stat.h"
2023-10-20 19:24:35 +00:00
2023-10-19 21:19:46 +00:00
unsigned int a_flag;
unsigned int l_flag;
2023-11-09 12:09:13 +00:00
unsigned int F_flag;
2023-11-08 08:05:04 +00:00
unsigned int p_flag;
2023-10-19 21:19:46 +00:00
2023-11-08 16:47:09 +00:00
struct d_node {
2023-11-10 10:10:25 +00:00
/* basename() */
2023-11-08 16:47:09 +00:00
char *name;
2023-11-10 10:10:25 +00:00
/* For free */
char *full_name;
2023-11-08 16:47:09 +00:00
struct d_node *next;
struct stat stats;
};
2023-11-01 12:33:16 +00:00
2023-11-09 12:09:13 +00:00
/* Work with dir */
2023-11-08 16:47:09 +00:00
struct d_node *stat_file(char *filename) {
struct d_node *file = malloc(sizeof(struct d_node));
if (file == NULL)
return NULL;
2023-11-01 12:33:16 +00:00
2023-11-08 16:47:09 +00:00
if (mu_get_lstat("ls", filename, &file->stats))
return NULL;
2023-11-01 12:33:16 +00:00
2023-11-10 10:10:25 +00:00
file->full_name = filename;
2023-11-10 10:39:55 +00:00
file->name = strrchr(filename, '/') + 1;
2023-11-08 16:47:09 +00:00
return file;
2023-10-19 21:19:46 +00:00
}
2023-11-08 16:47:09 +00:00
struct d_node **list(const char *path, size_t *nfiles) {
2023-10-30 15:45:24 +00:00
DIR *dp = opendir(path);
if (dp == NULL) {
2023-10-20 14:32:50 +00:00
fprintf(stderr, "ls: %s: %s\n", path, strerror(errno));
2023-11-08 16:47:09 +00:00
return NULL;
2023-10-20 14:32:50 +00:00
}
2023-11-08 16:47:09 +00:00
struct d_node **dir, *cur, *dr = NULL;
size_t files = 0;
2023-10-19 21:19:46 +00:00
struct dirent *ep;
while ((ep = readdir(dp)) != NULL) {
2023-10-19 21:19:46 +00:00
if (ep->d_name[0] == '.' && !a_flag)
continue;
2023-10-31 09:53:32 +00:00
char *full_path = mu_make_path("ls", path, ep->d_name);
2023-10-30 15:45:24 +00:00
if (full_path == NULL)
2023-11-08 16:47:09 +00:00
continue;
2023-10-19 21:19:46 +00:00
2023-11-08 16:47:09 +00:00
cur = stat_file(full_path);
2023-11-10 10:10:25 +00:00
if (cur == NULL)
2023-11-08 16:47:09 +00:00
continue;
2023-10-19 21:19:46 +00:00
2023-11-08 16:47:09 +00:00
cur->next = dr;
dr = cur;
files++;
}
2023-11-10 10:10:25 +00:00
closedir(dp);
2023-11-08 16:47:09 +00:00
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);
}
2023-11-10 10:39:55 +00:00
for (size_t i = 0; i < files; i++) {
dir[i] = cur;
cur = cur->next;
}
2023-11-08 16:47:09 +00:00
return dir;
}
2023-11-08 16:47:09 +00:00
void dfree(struct d_node **dir) {
struct d_node *cur = dir[0], *next;
while (cur != NULL) {
next = cur->next;
2023-11-10 10:10:25 +00:00
free(cur->full_name);
2023-11-08 16:47:09 +00:00
free(cur);
cur = next;
}
2023-11-08 16:47:09 +00:00
free(dir);
}
2023-11-09 12:09:13 +00:00
/* Print */
2023-11-10 10:10:25 +00:00
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' : '-');
}
2023-11-09 12:09:13 +00:00
void print(const struct d_node *node) {
char suf = 0;
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 = '*';
}
2023-11-12 08:31:14 +00:00
if (!l_flag)
2023-11-10 10:10:25 +00:00
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;
struct passwd *pw = getpwuid(node->stats.st_uid);
struct group *gr = getgrgid(node->stats.st_gid);
2023-11-11 22:10:12 +00:00
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);
2023-11-10 10:10:25 +00:00
}
2023-11-09 12:09:13 +00:00
}
int ls(const char *dir_name, int label, struct winsize w) {
(void)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);
2023-11-12 08:31:14 +00:00
if (!p_flag || l_flag) {
2023-11-09 12:09:13 +00:00
for (size_t i = 0; i < files; i++) {
print(dir[i]);
putchar('\n');
}
2023-11-12 08:31:14 +00:00
}
2023-11-09 12:09:13 +00:00
/* Todo: sort and print */
2023-11-12 08:31:14 +00:00
else {
for (size_t i = 0; i < files; i++)
print(dir[i]);
putchar('\n');
}
2023-11-09 12:09:13 +00:00
dfree(dir);
return 0;
}
2023-11-08 08:05:04 +00:00
int main(int argc, char **argv) {
int opt;
2023-11-09 12:09:13 +00:00
while ((opt = getopt(argc, argv, "alF")) != -1) {
2023-11-08 08:05:04 +00:00
switch (opt) {
case 'a':
a_flag = 1;
break;
2023-11-08 08:05:04 +00:00
case 'l':
l_flag = 1;
break;
2023-10-19 21:19:46 +00:00
2023-11-09 12:09:13 +00:00
case 'F':
F_flag = 1;
break;
2023-11-08 08:05:04 +00:00
default:
2023-11-09 12:09:13 +00:00
printf("ls [path]\n\t[-a Show hidden files] [-l Use a long listing format]\n\t[-F Append indicator to names]\n");
2023-11-08 08:05:04 +00:00
return 0;
}
}
2023-11-08 08:05:04 +00:00
argv += optind;
argc -= optind;
2023-11-09 12:09:13 +00:00
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
/* Check if programm piped, 1 - flase, 0 - true */
p_flag = isatty(STDOUT_FILENO);
2023-11-08 16:47:09 +00:00
if (argc < 1)
2023-11-09 12:09:13 +00:00
ls(".", 0, w);
2023-11-08 16:47:09 +00:00
if (argc == 1)
2023-11-09 12:09:13 +00:00
ls(argv[0], 0, w);
2023-11-08 16:47:09 +00:00
2023-11-09 12:09:13 +00:00
else
for (int i = 0; i < argc; i++)
ls(argv[i], 1, w);
2023-10-19 21:19:46 +00:00
return 0;
}
2023-11-09 12:09:13 +00:00