This commit is contained in:
Your Name 2024-01-07 13:14:47 +03:00
parent c520e6e833
commit 30634551ba
1 changed files with 12 additions and 11 deletions

View File

@ -118,7 +118,7 @@ struct d_node **list(const char *path, size_t *nfiles, int *ret) {
}
struct d_node **list_one(const char *path, int *ret) {
struct d_node **dir = malloc(sizeof(struct d_node *));
struct d_node **dir = malloc(sizeof(struct d_node *) * 2);
if (dir == NULL) {
fprintf(stderr, "ls: malloc failed\n");
exit(1);
@ -137,17 +137,15 @@ struct d_node **list_one(const char *path, int *ret) {
return NULL;
}
dir[1] = NULL;
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;
for (size_t i = 0; dir[i]; i++) {
free(dir[i]->full_name);
free(dir[i]);
}
free(dir);
@ -406,17 +404,20 @@ int main(int argc, char **argv) {
/* Check if programm piped, 1 - false, 0 - true */
p_flag = isatty(STDOUT_FILENO);
int ret = 0;
if (argc < 1)
return ls(".", 0, w);
if (argc == 1)
else if (argc == 1)
return ls(argv[0], 0, w);
else
else {
int ret = 0;
for (int i = 0; i < argc; i++)
if (ls(argv[i], 1, w))
ret = 1;
return ret;
return ret;
}
return 0;
}