nuke/list.h

53 lines
1.2 KiB
C

#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
static void add_file(char *name, char ***files, int *size, int *capacity)
{
if (*size == *capacity) {
*capacity *= 2;
*files = realloc(*files, sizeof(char*) * (*capacity));
}
(*files)[*size] = strdup(name);
(*size)++;
}
static void add_files(char *name, char ***files, int *size, int *capacity)
{
DIR *dir;
struct dirent *entry;
add_file(name, files, size, capacity);
if (!(dir = opendir(name))) {
return;
}
while ((entry = readdir(dir)) != NULL) {
char path[1024];
if (entry->d_type == DT_DIR) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
add_files(path, files, size, capacity);
} else {
snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
add_file(path, files, size, capacity);
}
}
closedir(dir);
}
/*int main(int argc, char *argv[]) {
int capacity = 32, size = 0;
char **files = malloc(sizeof(char*)*capacity);
for (int i = 1; i < argc; i++) {
add_files(argv[i], &files, &size, &capacity);
}
for (int f = 0; f < size; f++) {
printf("%s\n", files[f]);
}
return 0;
}*/