summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2023-11-23 13:35:57 -0500
committerTavian Barnes <tavianator@tavianator.com>2023-11-23 13:56:03 -0500
commitcdbd5e6fcf1f447b515e717f7d7194471ef714a7 (patch)
tree80e35850febdda9416a1306c8805f2a1da3dabc8
parent9032d107ab759450c21fea8f82865ff48c743132 (diff)
downloadbfs-cdbd5e6fcf1f447b515e717f7d7194471ef714a7.tar.xz
ctx: Switch paths from darray to RESERVE()
-rw-r--r--src/ctx.c5
-rw-r--r--src/ctx.h3
-rw-r--r--src/eval.c3
-rw-r--r--src/parse.c20
4 files changed, 16 insertions, 15 deletions
diff --git a/src/ctx.c b/src/ctx.c
index 692f5c5..f5dc465 100644
--- a/src/ctx.c
+++ b/src/ctx.c
@@ -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);
diff --git a/src/ctx.h b/src/ctx.h
index 1c7df63..75891da 100644
--- a/src/ctx.h
+++ b/src/ctx.h
@@ -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. */
diff --git a/src/eval.c b/src/eval.c
index eb4a0ca..55b14f0 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -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, &copy) != 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;
}