Skip to content
Snippets Groups Projects
Verified Commit 65e03d9a authored by STEVAN Antoine's avatar STEVAN Antoine :crab:
Browse files

add main

parent affe3f99
Branches
No related tags found
No related merge requests found
main.c 0 → 100644
#include <dirent.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char **items;
size_t size;
size_t capacity;
} da_str_t;
#define da_append(da, item) \
do { \
if ((da)->size >= (da)->capacity) { \
if ((da)->capacity == 0) { \
(da)->capacity = 256; \
} \
(da)->items = \
realloc((da)->items, (da)->capacity * sizeof(*(da)->items)); \
} \
(da)->items[(da)->size++] = item; \
} while (0)
#define da_foreach(Type, it, da) \
for (Type *it = (da)->items; it < (da)->items + (da)->size; ++it)
#ifdef DEBUG
#define log_infoln(fmt, ...) printf("INFO: " fmt "\n", ##__VA_ARGS__)
#else // DEBUG
#define log_infoln(fmt, ...) ((void)0)
#endif // DEBUG
da_str_t ls(const char *path) {
DIR *dp = opendir(path);
if (dp == NULL) {
if (errno == ENOTDIR) {
return (da_str_t){0};
}
perror("opendir");
exit(EXIT_FAILURE);
}
da_str_t files = {0};
struct dirent *entry;
while ((entry = readdir(dp))) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
continue;
}
da_append(&files, entry->d_name);
}
closedir(dp);
return files;
}
char *concat(const char *s1, const char *s2) {
char *result = malloc(strlen(s1) + strlen(s2) + 1);
strcpy(result, s1);
strcat(result, s2);
return result;
}
da_str_t find_git(const char *path) {
log_infoln("current: %s", path);
da_str_t next = ls(path);
if (next.size == 0) {
log_infoln("\tfile or empty directory");
return (da_str_t){0};
}
bool is_repo = false;
da_foreach(char *, f, &next) {
log_infoln("\t\tnext: %s", *f);
if (strcmp(*f, ".git") == 0) {
is_repo = true;
}
}
da_str_t repos = {0};
if (is_repo) {
log_infoln("\trepo");
da_append(&repos, (char *)path);
return repos;
}
log_infoln("\tNOT a repo");
da_foreach(char *, f, &next) {
char *g = malloc(sizeof(f) + 1);
strcpy(g, *f);
da_str_t res = find_git(concat(concat(path, "/"), g));
da_foreach(char *, r, &res) { da_append(&repos, *r); }
}
return repos;
}
int main(int argc, char *argv[]) {
// da_str_t files = ls((argc > 1) ? argv[1] : ".");
// da_foreach(char *, f, &files) { printf("%s\n", *f); }
// printf(" %zu\n", files.size);
da_str_t repos = find_git((argc > 1) ? argv[1] : ".");
printf("repos found:\n");
da_foreach(char *, r, &repos) { printf("\t%s\n", *r); }
printf(" %zu\n", repos.size);
return EXIT_SUCCESS;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment