diff options
| author | Sadeep Madurange <sadeep@asciimx.com> | 2026-03-07 17:32:43 +0800 |
|---|---|---|
| committer | Sadeep Madurange <sadeep@asciimx.com> | 2026-03-07 21:44:32 +0800 |
| commit | 337114f01a6029778cbaefeef4f340e006872b91 (patch) | |
| tree | c1cc5bc087eb92ef44170820ab4f163837ab099a | |
| parent | ab25ae7cd84e88593d4e58ebeb27baec85a90f4a (diff) | |
| download | cvn-337114f01a6029778cbaefeef4f340e006872b91.tar.gz | |
Code clean-up: structs, renaming, err/errx.
| -rw-r--r-- | main.c | 157 |
1 files changed, 86 insertions, 71 deletions
@@ -2,6 +2,7 @@ #include <err.h> #include <errno.h> #include <fcntl.h> +#include <sha1.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -19,9 +20,16 @@ static inline void init(int argc, char *argv[]); static inline void status(int argc, char *argv[]); static inline void traverse(void); -static inline uint8_t ignore(const char *path); +static inline int ignore(const char *path); static inline int cmp(const void *a, const void *b); +struct stack; +struct stack_ent; +static inline void stack_alloc(struct stack *st); +static inline struct stack_ent *pop(struct stack *st); +static inline void push(struct stack *st, struct stack_ent *new_ent); +static inline void stack_free(struct stack *st); + static inline void *_xmalloc(size_t s, const char *file, int line); static inline void *_xrealloc(void *ptr, size_t s, const char *file, int line); @@ -38,12 +46,10 @@ struct command cmd[] = { int main(int argc, char *argv[]) { - uint8_t i; + int i; - if (argc < 2) { - fprintf(stderr, "Usage: %s <command> [<args>]\n", argv[0]); - return 1; - } + if (argc < 2) + errx(1, "Usage: %s <command> [<args>]", argv[0]); for (i = 0; cmd[i].name != NULL; i++) { if (strcmp(argv[1], cmd[i].name) == 0) { @@ -72,23 +78,18 @@ static inline void init(int argc, char *argv[]) } if (mkdir(REPO, 0755) == -1) { - if (errno != EEXIST) { - perror("Failed to create repository"); - return; - } + if (errno != EEXIST) + err(1, "Failed to create repository"); } - if ((repo_fd = open(REPO, O_RDONLY | O_DIRECTORY)) == -1) { - perror("Failed to open repository"); - return; - } + if ((repo_fd = open(REPO, O_RDONLY | O_DIRECTORY)) == -1) + err(1, "Failed to open repository"); if ((idx_fd = openat(repo_fd, INDEX, O_WRONLY | O_CREAT | O_EXCL, 0644)) == -1) { if (errno != EEXIST) { close(repo_fd); - perror("Failed to create index"); - return; + err(1, "Failed to create index"); } } else close(idx_fd); @@ -102,10 +103,16 @@ static inline void status(int argc, char *argv[]) traverse(); } -struct node { +struct stack_ent { char *path; - unsigned char is_dir; - struct node *parent; + int is_dir; + struct stack_ent *parent; +}; + +struct stack { + struct stack_ent **ents; + size_t len; + size_t cap; }; static inline void traverse(void) @@ -114,35 +121,26 @@ static inline void traverse(void) char *rel_path; struct stat st; struct dirent *entry; - struct node *cur_dir, *root, *new_node; - struct node **dirs, **tree; - size_t dirs_len, tree_len; - size_t dirs_cap, tree_cap; - - tree_len = 0, tree_cap = 512; - tree = MALLOC(sizeof(tree[0]) * tree_cap); + struct stack work_tree, stk; + struct stack_ent *cur_dir, *root, *new_ent; - dirs_len = 0, dirs_cap = 512; - dirs = MALLOC(sizeof(dirs[0]) * dirs_cap); + stack_alloc(&stk); + stack_alloc(&work_tree); - root = MALLOC(sizeof(struct node)); + root = MALLOC(sizeof(struct stack_ent)); root->path = strdup("."); root->is_dir = 1; root->parent = NULL; - dirs[dirs_len++] = root; + push(&stk, root); - while (dirs_len > 0) { - cur_dir = dirs[--dirs_len]; + while (stk.len > 0) { + cur_dir = pop(&stk); if (!(dir = opendir(cur_dir->path))) { warn("Failed to open directory %s", cur_dir->path); continue; } - if (tree_len >= tree_cap - 1) { - tree_cap <<= 1; - tree = REALLOC(tree, sizeof(tree[0]) * tree_cap); - } - tree[tree_len++] = cur_dir; + push(&work_tree, cur_dir); while ((entry = readdir(dir))) { if (ignore(entry->d_name)) @@ -156,50 +154,65 @@ static inline void traverse(void) } if (S_ISDIR(st.st_mode)) { - if (dirs_len >= dirs_cap) { - dirs_cap <<= 1; - dirs = REALLOC(dirs, sizeof(dirs[0]) * dirs_cap); - } - new_node = MALLOC(sizeof(struct node)); - new_node->path = rel_path; - new_node->is_dir = 1; - new_node->parent = cur_dir; - dirs[dirs_len++] = new_node; + new_ent = MALLOC(sizeof(struct stack_ent)); + new_ent->path = rel_path; + new_ent->is_dir = 1; + new_ent->parent = cur_dir; + push(&stk, new_ent); } else { - if (tree_len >= tree_cap) { - tree_cap <<= 1; - tree = REALLOC(tree, sizeof(tree[0]) * tree_cap); - } - new_node = MALLOC(sizeof(struct node)); - new_node->path = rel_path; - new_node->is_dir = 0; - new_node->parent = cur_dir; - tree[tree_len++] = new_node; + new_ent = MALLOC(sizeof(struct stack_ent)); + new_ent->path = rel_path; + new_ent->is_dir = 0; + new_ent->parent = cur_dir; + push(&work_tree, new_ent); } } closedir(dir); } - for (int i = (int)tree_len - 1; i >= 0; i--) { + for (ssize_t i = work_tree.len - 1; i >= 0; i--) { printf("Entry: %s, parent: %s\n", - tree[i]->path, - tree[i]->parent ? tree[i]->parent->path : "NONE"); + work_tree.ents[i]->path, + work_tree.ents[i]->parent ? work_tree.ents[i]->parent->path : "NONE"); } - for (size_t i = 0; i < tree_len; i++) { - free(tree[i]->path); - free(tree[i]); + stack_free(&stk); + stack_free(&work_tree); +} + +static inline void stack_alloc(struct stack *st) +{ + st->len = 0; + st->cap = 512; + st->ents = MALLOC(sizeof(st->ents[0]) * st->cap); +} + +static inline struct stack_ent *pop(struct stack *st) +{ + return st->ents[--(st->len)]; +} + +static inline void push(struct stack *st, struct stack_ent *new_ent) +{ + if (st->len >= st->cap) { + st->cap <<= 1; + st->ents = REALLOC(st->ents, sizeof(struct stack_ent *) * st->cap); } - free(tree); - free(dirs); + st->ents[st->len++] = new_ent; } -static inline int cmp(const void *a, const void *b) +static inline void stack_free(struct stack *st) { - return strcmp(*(const char **)a, *(const char **)b); + size_t i; + + for (i = 0; i < st->len; i++) { + free(st->ents[i]->path); + free(st->ents[i]); + } + free(st->ents); } -static inline unsigned char ignore(const char *path) +static inline int ignore(const char *path) { return strcmp(path, ".") == 0 || strcmp(path, "..") == 0 || @@ -208,17 +221,19 @@ static inline unsigned char ignore(const char *path) static inline void *_xmalloc(size_t s, const char *file, int line) { - void *p = malloc(s); - if (!p) + void *p; + + if (!(p = malloc(s))) err(1, "%s:%d: malloc", file, line); return p; } -static inline void *_xrealloc(void *ptr, size_t s, +static inline void *_xrealloc(void *ptr, size_t s, const char *file, int line) { - void *p = realloc(ptr, s); - if (!p) + void *p; + + if (!(p = realloc(ptr, s))) err(1, "%s:%d: realloc", file, line); return p; } |
