ls fix: recursion, file

This commit is contained in:
Your Name 2023-11-29 19:52:21 +03:00
parent f04874487c
commit 22d75498df
4 changed files with 66 additions and 21 deletions

1
TODO
View File

@ -1,7 +1,6 @@
With "micro-" prefix
*Todo:
**ls (file)
tail
expr
uniq

0
build.sh Normal file → Executable file
View File

View File

@ -15,11 +15,14 @@
#include "get_stat.h"
#include "config.h"
unsigned int O_flag;
unsigned int a_flag;
unsigned int l_flag;
unsigned int F_flag;
unsigned int c_flag;
unsigned int R_flag;
unsigned int d_flag;
unsigned int L_flag;
unsigned int p_flag;
struct d_node {
@ -39,11 +42,19 @@ struct d_node *stat_file(char *filename) {
if (file == NULL)
return NULL;
if (mu_get_lstat("ls", filename, &file->stats))
if (mu_get_lstat("ls", filename, &file->stats)) {
free(file);
return NULL;
}
file->full_name = filename;
file->name = strrchr(filename, '/') + 1;
file->name = strrchr(filename, '/');
if (file->name == NULL)
file->name = filename;
else
file->name++;
return file;
}
@ -67,8 +78,10 @@ struct d_node **list(const char *path, size_t *nfiles) {
continue;
cur = stat_file(full_path);
if (cur == NULL)
if (cur == NULL) {
free(full_path);
continue;
}
cur->next = dr;
dr = cur;
@ -96,6 +109,21 @@ struct d_node **list(const char *path, size_t *nfiles) {
return dir;
}
struct d_node **list_file(const char *path) {
struct d_node **dir = malloc(sizeof(struct d_node *));
char *new_path = strdup(path);
dir[0] = stat_file(new_path);
if (dir[0] == NULL) {
free(dir);
return NULL;
}
return dir;
}
void dfree(struct d_node **dir) {
struct d_node *cur = dir[0], *next;
while (cur != NULL) {
@ -146,7 +174,7 @@ int print(const struct d_node *node) {
/* Output */
if (c_flag)
if (c_flag && p_flag)
printf("%s", color);
int ret = 0;
@ -171,7 +199,7 @@ int print(const struct d_node *node) {
ret = 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, (F_flag) ? suf : 0);
}
if (c_flag)
if (c_flag && p_flag)
printf("\033[0m");
return ret;
@ -223,7 +251,19 @@ void col_print(struct d_node **node, size_t files, struct winsize w) {
int ls(const char *dir_name, int label, struct winsize w) {
size_t files = 0;
struct d_node **dir = list(dir_name, &files);
struct stat sb;
if (mu_get_stat("ls", dir_name, &sb))
return 1;
struct d_node **dir = NULL;
if (S_ISDIR(sb.st_mode))
dir = list(dir_name, &files);
else {
dir = list_file(dir_name);
files++;
}
if (dir == NULL)
return 1;
@ -231,7 +271,7 @@ int ls(const char *dir_name, int label, struct winsize w) {
printf("\n%s:\n", dir_name);
/* pipe print */
if (!p_flag || l_flag) {
if (!p_flag || l_flag || O_flag) {
for (size_t i = 0; i < files; i++) {
print(dir[i]);
putchar('\n');
@ -244,14 +284,8 @@ int ls(const char *dir_name, int label, struct winsize w) {
if (R_flag)
for (size_t i = 0; i < files; i++)
if (S_ISDIR(dir[i]->stats.st_mode)) {
char *full_path = mu_make_path("ls", dir_name, dir[i]->name);
if (full_path == NULL)
continue;
ls(full_path, 1, w);
free(full_path);
}
if (S_ISDIR(dir[i]->stats.st_mode) && strcmp(dir[i]->name, "..") && strcmp(dir[i]->name, "."))
ls(dir[i]->full_name, 1, w);
dfree(dir);
return 0;
@ -259,8 +293,12 @@ int ls(const char *dir_name, int label, struct winsize w) {
int main(int argc, char **argv) {
int opt;
while ((opt = getopt(argc, argv, "alFcR")) != -1) {
while ((opt = getopt(argc, argv, "1alFcRL")) != -1) {
switch (opt) {
case '1':
O_flag = 1;
break;
case 'a':
a_flag = 1;
break;
@ -281,8 +319,16 @@ int main(int argc, char **argv) {
R_flag = 1;
break;
case 'd':
d_flag = 1;
break;
case 'L':
L_flag = 1;
break;
default:
printf("ls [dir1 dir2...]\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] [-R Recursive]\n");
printf("ls [dir1 dir2...]\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] [-R Recursive]\n\t[-1 One column] [-d Print only dir names]\n\t[-L Follow symlinks]\n");
return 0;
}
}
@ -293,7 +339,7 @@ int main(int argc, char **argv) {
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
/* Check if programm piped, 1 - flase, 0 - true */
/* Check if programm piped, 1 - false, 0 - true */
p_flag = isatty(STDOUT_FILENO);
if (argc < 1)

View File

@ -8,8 +8,8 @@ long parse_long(const char *str) {
char *ptr;
val = strtol(str, &ptr, 0);
if (!*str || *ptr)
return 0;
if (*ptr || val == 0)
return 1;
return val;
}