Big update: 7/N fix

This commit is contained in:
Your Name 2023-11-09 15:09:13 +03:00
parent 11b3a7907a
commit c595117839

View File

@ -10,11 +10,13 @@
#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 {
@ -23,6 +25,7 @@ struct d_node {
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)
@ -91,9 +94,6 @@ struct d_node **list(const char *path, size_t *nfiles) {
}
void dfree(struct d_node **dir) {
if (dir == NULL)
return;
struct d_node *cur = dir[0], *next;
while (cur != NULL) {
next = cur->next;
@ -104,9 +104,48 @@ void dfree(struct d_node **dir) {
free(dir);
}
/* Print */
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 = '*';
}
printf("%s%c", node->name, suf);
}
int ls(const char *dir_name, int label, struct winsize w) {
/* Unused, tmp */
(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);
if (!p_flag)
for (size_t i = 0; i < files; i++) {
print(dir[i]);
putchar('\n');
}
/* Todo: sort and print */
else {}
dfree(dir);
return 0;
}
int main(int argc, char **argv) {
int opt;
while ((opt = getopt(argc, argv, "al")) != -1) {
while ((opt = getopt(argc, argv, "alF")) != -1) {
switch (opt) {
case 'a':
a_flag = 1;
@ -116,8 +155,12 @@ int main(int argc, char **argv) {
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");
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;
}
}
@ -125,18 +168,22 @@ int main(int argc, char **argv) {
argv += optind;
argc -= optind;
struct d_node **dir = NULL;
size_t files = 0;
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
/* Check if programm piped, 1 - flase, 0 - true */
p_flag = isatty(STDOUT_FILENO);
if (argc < 1)
dir = list(".", &files);
ls(".", 0, w);
if (argc == 1)
dir = list(argv[0], &files);
ls(argv[0], 0, w);
for (size_t i = 0; i < files; i++)
puts(dir[i]->name);
else
for (int i = 0; i < argc; i++)
ls(argv[i], 1, w);
dfree(dir);
return 0;
}