From 12dcb09794c48a333bb850d353540c688c503b76 Mon Sep 17 00:00:00 2001 From: dev Date: Sat, 14 Nov 2026 17:42:44 +0000 Subject: Options: getopt_long instead of three hand-rolled parsers option() in hook.c and one_option() in cmd/main.c were the same function under two names, and validate's seven options were a chain of seven calls to one of them. getopt_long does all of it - both --opt value and --opt=value, the lot - so the parsers are gone and what is left is a table and what each entry sets. Two small helpers carry what getopt_long cannot: the name behind a long option's val, for the 'wants a value' message, and the optind reset that lets a run parse options twice on two different argv slices. A command now gets argv[0] as its own name, which is where getopt_long expects argv[0]; the commands that take names step over it. strip() in validate.c was authz_trim() again, and cmd_authorize freed the same three pointers on four exit paths. One behaviour change: 'authorize --' is now the end-of-options marker rather than an unknown argument. Anything after it is still refused. --- src/validate.c | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-) (limited to 'src/validate.c') diff --git a/src/validate.c b/src/validate.c index 91be26a..c5d05ab 100644 --- a/src/validate.c +++ b/src/validate.c @@ -77,19 +77,6 @@ static int ci_prefix(const char *line, const char *want) return 1; } -static char *strip(char *s) -{ - char *end; - - while (*s != '\0' && isspace((unsigned char)*s)) - s++; - end = s + strlen(s); - while (end > s && isspace((unsigned char)end[-1])) - end--; - *end = '\0'; - return s; -} - // [section] key = value, last one winning - which is the mistake worth // reporting in a gitconfig. @@ -109,7 +96,7 @@ static int ini_value(const char *path, const char *section, const char *key, if (f == NULL) return -1; while ((len = getline(&line, &cap, f)) != -1) { - char *t = strip(line); + char *t = authz_trim(line); char *eq; if (*t == '\0' || *t == '#' || *t == ';') @@ -124,9 +111,9 @@ static int ini_value(const char *path, const char *section, const char *key, if (eq == NULL) continue; *eq = '\0'; - if (!ci_equal(strip(t), key)) + if (!ci_equal(authz_trim(t), key)) continue; - snprintf(out, outsz, "%s", strip(eq + 1)); + snprintf(out, outsz, "%s", authz_trim(eq + 1)); { // gitconfig allows quoting size_t n = strlen(out); @@ -158,7 +145,7 @@ static int directive_value(const char *path, const char *name, char *out, size_t if (f == NULL) return -1; while ((len = getline(&line, &cap, f)) != -1) { - char *t = strip(line); + char *t = authz_trim(line); char *sp; if (*t == '\0' || *t == '#') @@ -171,7 +158,7 @@ static int directive_value(const char *path, const char *name, char *out, size_t *sp = '\0'; if (!ci_equal(t, name)) continue; - snprintf(out, outsz, "%s", strip(sp + 1)); + snprintf(out, outsz, "%s", authz_trim(sp + 1)); free(line); fclose(f); return 1; @@ -337,7 +324,7 @@ static void check_account(const policy_t *p, validate_opts o) break; } if (nf >= 7 && strcmp(fields[0], "git") == 0) { - char *shell = strip(fields[6]); + char *shell = authz_trim(fields[6]); found = 1; if (strstr(shell, "git-shell") == NULL) @@ -368,7 +355,7 @@ static void check_keys(const policy_t *p, validate_opts o) return; } while ((len = getline(&line, &cap, f)) != -1) { - char *t = strip(line); + char *t = authz_trim(line); char *at; if (*t == '\0' || *t == '#') @@ -426,7 +413,7 @@ static void check_htpasswd(const policy_t *p, validate_opts o) return; } while ((len = getline(&line, &cap, f)) != -1) { - char *t = strip(line); + char *t = authz_trim(line); char *colon; if (*t == '\0' || *t == '#') -- cgit v1.2.3