Iterate filesystem and store information in a struct array

This page summarizes the projects mentioned and recommended in the original post on /r/C_Programming

InfluxDB - Power Real-Time Data Analytics at Scale
Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.
www.influxdata.com
featured
SaaSHub - Software Alternatives and Reviews
SaaSHub helps you find the best software and product alternatives
www.saashub.com
featured
  • dirent

    C/C++ library for retrieving information on files and directories

  • #include #include #include #include #include #include #include typedef struct file_info { char *path; // ToDo: more info in the future } file_info; // Function prototypes bool is_special_file(const char *file); file_info **find_all_files(const char *root_dir); int find_files(const char *dir_path, file_info ***files, int *cap, int *cnt); file_info *file_info_new(const char *path); int main(int argc, char **argv) { find_all_files("/tmp/test"); // ToDo: Free all the memory correctly return 0; } file_info *file_info_new(const char *path) { file_info *file = malloc(sizeof(file_info)); if (!file) return NULL; file->path = path; return file; } file_info **find_all_files(const char *root_dir) { file_info **files = malloc(sizeof(file_info *)); int capacity = 1; int count = 0; int result = find_files(root_dir, &files, &capacity, &count); printf("Array cap: %d\n", capacity); printf("Array count: %d\n", count); printf("-------------------\n"); for (int i = 0; i < count; i++) { printf("%s\n", files[i]->path); } return files; } // Based on the implementation from https://github.com/tronkko/dirent int find_files(const char *dir_path, file_info ***files, int *cap, int *cnt) { char buffer[PATH_MAX + 2]; char *p = buffer; char *end = &buffer[PATH_MAX]; // Copy directory name to buffer const char *src = dir_path; while (p < end && *src != '\0') { *p++ = *src++; } *p = '\0'; // Open directory stream DIR *dir_stream = opendir(dir_path); if (!dir_stream) { printf("Failed to open %s (%s)\n", dir_path, strerror(errno)); return -1; } // Iterate all files/directories within the directory struct dirent *ent; while ((ent = readdir(dir_stream)) != NULL) { char *q = p; char c; // Get final character of directory name if (buffer < q) c = q[-1]; else c = ':'; // Append directory separator if not already there if (c != ':' && c != '/' && c != '\\') *q++ = '/'; // Append file name to current directory path src = ent->d_name; while (q < end && *src != '\0') { *q++ = *src++; } *q = '\0'; // Resize array if needed if (*cnt == *cap) { int realloc_size = (*cap) * 2; file_info **tmp_files = realloc(*files, realloc_size * sizeof(file_info *)); if (!tmp_files) exit(1); *cap *= 2; *files = tmp_files; } // Process every dirent type switch (ent->d_type) { case DT_LNK: { printf("[LINK] %s\n", buffer); (*files)[*cnt] = file_info_new(buffer); (*cnt)++; break; } case DT_REG: { printf("[FILE] %s\n", buffer); (*files)[*cnt] = file_info_new(buffer); (*cnt)++; break; } case DT_DIR: { if (!is_special_file(ent->d_name)) { printf("[DIR] %s\n", buffer); (*files)[*cnt] = file_info_new(buffer); (*cnt)++; find_files(buffer, files, cap, cnt); } break; } default: break; } } return 0; } bool is_special_file(const char *file) { if (strcmp(file, ".") != 0 && strcmp(file, "..") != 0) return false; return true; }

  • InfluxDB

    Power Real-Time Data Analytics at Scale. Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.

    InfluxDB logo
NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts