diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2019-08-29 23:45:45 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2019-08-29 23:45:45 -0400 |
commit | a30b3f503bede87043262343ed26d6995b0a85d9 (patch) | |
tree | e24031938af531e2d2b308ae0c48f5c2639775b7 /parse.c | |
parent | c14a376ef6effe089d98e2211cb15e4b66e57fc1 (diff) | |
download | bfs-a30b3f503bede87043262343ed26d6995b0a85d9.tar.xz |
darray: New dynamic array library
Diffstat (limited to 'parse.c')
-rw-r--r-- | parse.c | 21 |
1 files changed, 5 insertions, 16 deletions
@@ -23,6 +23,7 @@ #include "bfs.h" #include "cmdline.h" +#include "darray.h" #include "diag.h" #include "dstring.h" #include "eval.h" @@ -292,7 +293,7 @@ int free_cmdline(struct cmdline *cmdline) { cfclose(cerr); free_colors(cmdline->colors); - free(cmdline->paths); + darray_free(cmdline->paths); free(cmdline->argv); free(cmdline); } @@ -505,18 +506,7 @@ static char **parser_advance(struct parser_state *state, enum token_type type, s */ static int parse_root(struct parser_state *state, const char *path) { struct cmdline *cmdline = state->cmdline; - size_t i = cmdline->npaths; - if ((i & (i + 1)) == 0) { - const char **paths = realloc(cmdline->paths, 2*(i + 1)*sizeof(*paths)); - if (!paths) { - return -1; - } - cmdline->paths = paths; - } - - cmdline->paths[i] = path; - cmdline->npaths = i + 1; - return 0; + return DARRAY_PUSH(&cmdline->paths, &path); } /** @@ -3276,7 +3266,7 @@ void dump_cmdline(const struct cmdline *cmdline, bool verbose) { cfprintf(cerr, " "); } - for (size_t i = 0; i < cmdline->npaths; ++i) { + for (size_t i = 0; i < darray_length(cmdline->paths); ++i) { const char *path = cmdline->paths[i]; char c = path[0]; if (c == '-' || c == '(' || c == ')' || c == '!' || c == ',') { @@ -3361,7 +3351,6 @@ struct cmdline *parse_cmdline(int argc, char *argv[]) { cmdline->argv = NULL; cmdline->paths = NULL; - cmdline->npaths = 0; cmdline->colors = NULL; cmdline->cout = NULL; cmdline->cerr = NULL; @@ -3460,7 +3449,7 @@ struct cmdline *parse_cmdline(int argc, char *argv[]) { goto fail; } - if (cmdline->npaths == 0) { + if (darray_length(cmdline->paths) == 0) { if (parse_root(&state, ".") != 0) { goto fail; } |