fixed ls. Removed tmp busybox code. Now all code at WTFPL

This commit is contained in:
Your Name 2024-06-18 15:07:22 +03:00
parent 2eb86c9525
commit 04c183bd9a
1 changed files with 19 additions and 22 deletions

View File

@ -77,7 +77,11 @@ struct d_node **list(const char *path, size_t *nfiles, int *ret) {
return NULL;
}
struct d_node *dn = NULL, *cur;
struct d_node **dn = malloc(sizeof(struct d_node *));
if (dn == NULL) {
fprintf(stderr, "ls: malloc: %s\n", strerror(errno));
return NULL;
}
struct dirent *ep;
while ((ep = readdir(dp)) != NULL) {
@ -88,36 +92,29 @@ struct d_node **list(const char *path, size_t *nfiles, int *ret) {
if (full_path == NULL)
continue;
cur = stat_file(full_path, 0);
if (cur == NULL) {
struct d_node **bckp = realloc(dn, sizeof(struct d_node *) * (*nfiles + 1));
if (bckp == NULL) {
free(full_path);
free(dn);
fprintf(stderr, "ls: realloc: %s\n", strerror(errno));
return NULL;
}
dn = bckp;
dn[*nfiles] = stat_file(full_path, 0);
if (dn[*nfiles] == NULL) {
*ret = 1;
free(full_path);
continue;
}
cur->next = dn;
dn = cur;
(*nfiles)++;
}
closedir(dp);
if (dn == NULL)
return NULL;
struct d_node **dir = malloc((*nfiles + 1) * sizeof(struct d_node *));
if (dir == NULL) {
fprintf(stderr, "ls: malloc failed\n");
return NULL;
}
for (size_t i = 0; i < *nfiles; i++) {
dir[i] = dn;
dn = dn->next;
}
return dir;
return dn;
}
struct d_node **list_one(const char *path, int *ret) {