diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2023-11-23 13:35:57 -0500 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2023-11-23 13:56:03 -0500 |
commit | cdbd5e6fcf1f447b515e717f7d7194471ef714a7 (patch) | |
tree | 80e35850febdda9416a1306c8805f2a1da3dabc8 /src | |
parent | 9032d107ab759450c21fea8f82865ff48c743132 (diff) | |
download | bfs-cdbd5e6fcf1f447b515e717f7d7194471ef714a7.tar.xz |
ctx: Switch paths from darray to RESERVE()
Diffstat (limited to 'src')
-rw-r--r-- | src/ctx.c | 5 | ||||
-rw-r--r-- | src/ctx.h | 3 | ||||
-rw-r--r-- | src/eval.c | 3 | ||||
-rw-r--r-- | src/parse.c | 20 |
4 files changed, 16 insertions, 15 deletions
@@ -4,7 +4,6 @@ #include "ctx.h" #include "alloc.h" #include "color.h" -#include "darray.h" #include "diag.h" #include "expr.h" #include "mtab.h" @@ -242,10 +241,10 @@ int bfs_ctx_free(struct bfs_ctx *ctx) { free_colors(ctx->colors); - for (size_t i = 0; i < darray_length(ctx->paths); ++i) { + for (size_t i = 0; i < ctx->npaths; ++i) { free((char *)ctx->paths[i]); } - darray_free(ctx->paths); + free(ctx->paths); free(ctx->argv); free(ctx); @@ -27,6 +27,9 @@ struct bfs_ctx { /** The root paths. */ const char **paths; + /** The number of root paths. */ + size_t npaths; + /** The main command line expression. */ struct bfs_expr *expr; /** An expression for files to filter out. */ @@ -12,7 +12,6 @@ #include "color.h" #include "config.h" #include "ctx.h" -#include "darray.h" #include "diag.h" #include "dir.h" #include "dstring.h" @@ -1622,7 +1621,7 @@ int bfs_eval(struct bfs_ctx *ctx) { struct bftw_args bftw_args = { .paths = ctx->paths, - .npaths = darray_length(ctx->paths), + .npaths = ctx->npaths, .callback = eval_callback, .ptr = &args, .nopenfd = fdlimit, diff --git a/src/parse.c b/src/parse.c index d3938fc..13e65bc 100644 --- a/src/parse.c +++ b/src/parse.c @@ -15,7 +15,6 @@ #include "color.h" #include "config.h" #include "ctx.h" -#include "darray.h" #include "diag.h" #include "dir.h" #include "eval.h" @@ -481,16 +480,17 @@ static char **parser_advance(struct parser_state *state, enum token_type type, s * Parse a root path. */ static int parse_root(struct parser_state *state, const char *path) { - char *copy = strdup(path); - if (!copy) { - parse_perror(state, "strdup()"); + struct bfs_ctx *ctx = state->ctx; + const char **root = RESERVE(const char *, &ctx->paths, &ctx->npaths); + if (!root) { + parse_perror(state, "RESERVE()"); return -1; } - struct bfs_ctx *ctx = state->ctx; - if (DARRAY_PUSH(&ctx->paths, ©) != 0) { - parse_perror(state, "DARRAY_PUSH()"); - free(copy); + *root = strdup(path); + if (!*root) { + --ctx->npaths; + parse_perror(state, "strdup()"); return -1; } @@ -3617,7 +3617,7 @@ void bfs_ctx_dump(const struct bfs_ctx *ctx, enum debug_flags flag) { } } - for (size_t i = 0; i < darray_length(ctx->paths); ++i) { + for (size_t i = 0; i < ctx->npaths; ++i) { const char *path = ctx->paths[i]; char c = path[0]; if (c == '-' || c == '(' || c == ')' || c == '!' || c == ',') { @@ -3778,7 +3778,7 @@ struct bfs_ctx *bfs_parse_cmdline(int argc, char *argv[]) { goto fail; } - if (darray_length(ctx->paths) == 0 && state.implicit_root) { + if (ctx->npaths == 0 && state.implicit_root) { if (parse_root(&state, ".") != 0) { goto fail; } |