micro-utils/coreutils/ls.c

143 lines
2.3 KiB
C
Raw Normal View History

2023-10-20 22:24:35 +03:00
#include <pwd.h>
#include <grp.h>
2023-10-20 00:19:46 +03:00
#include <time.h>
#include <stdio.h>
#include <errno.h>
2023-10-30 18:45:24 +03:00
#include <stdlib.h>
#include <string.h>
2023-10-20 16:59:07 +03:00
#include <stdint.h>
2023-10-20 17:32:50 +03:00
#include <unistd.h>
#include <dirent.h>
2023-11-08 19:47:09 +03:00
#include <sys/stat.h>
#include <sys/types.h>
2023-10-31 12:53:32 +03:00
#include "make_path.h"
#include "get_stat.h"
2023-10-20 22:24:35 +03:00
2023-10-20 00:19:46 +03:00
unsigned int a_flag;
unsigned int l_flag;
2023-11-08 11:05:04 +03:00
unsigned int p_flag;
2023-10-20 00:19:46 +03:00
2023-11-08 19:47:09 +03:00
struct d_node {
char *name;
struct d_node *next;
struct stat stats;
};
2023-11-01 15:33:16 +03:00
2023-11-08 19:47:09 +03: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 15:33:16 +03:00
2023-11-08 19:47:09 +03:00
if (mu_get_lstat("ls", filename, &file->stats))
return NULL;
2023-11-01 15:33:16 +03:00
2023-11-08 19:47:09 +03:00
return file;
2023-10-20 00:19:46 +03:00
}
2023-11-08 19:47:09 +03:00
struct d_node **list(const char *path, size_t *nfiles) {
2023-10-30 18:45:24 +03:00
DIR *dp = opendir(path);
if (dp == NULL) {
2023-10-20 17:32:50 +03:00
fprintf(stderr, "ls: %s: %s\n", path, strerror(errno));
2023-11-08 19:47:09 +03:00
return NULL;
2023-10-20 17:32:50 +03:00
}
2023-11-08 19:47:09 +03:00
struct d_node **dir, *cur, *dr = NULL;
size_t files = 0;
2023-10-20 00:19:46 +03:00
struct dirent *ep;
while ((ep = readdir(dp)) != NULL) {
2023-10-20 00:19:46 +03:00
if (ep->d_name[0] == '.' && !a_flag)
continue;
2023-10-31 12:53:32 +03:00
char *full_path = mu_make_path("ls", path, ep->d_name);
2023-10-30 18:45:24 +03:00
if (full_path == NULL)
2023-11-08 19:47:09 +03:00
continue;
2023-10-20 00:19:46 +03:00
2023-11-08 19:47:09 +03:00
cur = stat_file(full_path);
if (cur == NULL) {
free(full_path);
continue;
2023-10-20 00:19:46 +03:00
}
2023-10-30 18:45:24 +03:00
free(full_path);
2023-10-30 18:09:33 +03:00
2023-11-08 19:47:09 +03:00
cur->name = ep->d_name;
cur->next = dr;
dr = cur;
files++;
}
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++) {
dir[i] = dr;
dr = dr->next;
if (dr == NULL)
break;
}
closedir(dp);
2023-11-08 19:47:09 +03:00
return dir;
}
2023-11-08 19:47:09 +03:00
void dfree(struct d_node **dir) {
if (dir == NULL)
return;
struct d_node *cur = dir[0], *next;
while (cur != NULL) {
next = cur->next;
free(cur);
cur = next;
}
2023-11-08 19:47:09 +03:00
free(dir);
}
2023-11-08 11:05:04 +03:00
int main(int argc, char **argv) {
int opt;
while ((opt = getopt(argc, argv, "al")) != -1) {
switch (opt) {
case 'a':
a_flag = 1;
break;
2023-11-08 11:05:04 +03:00
case 'l':
l_flag = 1;
break;
2023-10-20 00:19:46 +03:00
2023-11-08 11:05:04 +03:00
default:
printf("ls [path]\n\t[-a Show hidden files] [-l Use a long listing format]\n");
return 0;
}
}
2023-11-08 11:05:04 +03:00
argv += optind;
argc -= optind;
2023-11-08 19:47:09 +03:00
struct d_node **dir = NULL;
size_t files = 0;
if (argc < 1)
dir = list(".", &files);
if (argc == 1)
dir = list(argv[0], &files);
for (size_t i = 0; i < files; i++)
puts(dir[i]->name);
2023-10-20 00:19:46 +03:00
2023-11-08 19:47:09 +03:00
dfree(dir);
return 0;
}