diff options
author | 2015-10-29 21:19:09 +0000 | |
---|---|---|
committer | 2015-10-29 21:19:09 +0000 | |
commit | 3d6ea532155b1ea54538f097c04f1f251652d24e (patch) | |
tree | fe0143e4f8bb13ef75365551950b1e8a76e92c29 | |
parent | Remove useless FACILITY define and use LOG_CRON directly. (diff) | |
download | wireguard-openbsd-3d6ea532155b1ea54538f097c04f1f251652d24e.tar.xz wireguard-openbsd-3d6ea532155b1ea54538f097c04f1f251652d24e.zip |
Convert env_get() into env_find() similar to __findenv() in libc.
Use env_find() in both env_get() and env_set() to find a var in envp.
Remove now-unused strcmp_until() function.
-rw-r--r-- | usr.sbin/cron/env.c | 98 | ||||
-rw-r--r-- | usr.sbin/cron/funcs.h | 3 | ||||
-rw-r--r-- | usr.sbin/cron/misc.c | 17 |
3 files changed, 51 insertions, 67 deletions
diff --git a/usr.sbin/cron/env.c b/usr.sbin/cron/env.c index 0b274e65b60..4ab8485b2e0 100644 --- a/usr.sbin/cron/env.c +++ b/usr.sbin/cron/env.c @@ -1,4 +1,4 @@ -/* $OpenBSD: env.c,v 1.29 2015/02/09 22:35:08 deraadt Exp $ */ +/* $OpenBSD: env.c,v 1.30 2015/10/29 21:19:09 millert Exp $ */ /* Copyright 1988,1990,1993,1994 by Paul Vixie * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") @@ -22,7 +22,7 @@ char ** env_init(void) { - char **p = malloc(sizeof(char **)); + char **p = malloc(sizeof(char *)); if (p != NULL) p[0] = NULL; @@ -63,49 +63,64 @@ env_copy(char **envp) return (p); } -char ** -env_set(char **envp, char *envstr) +static char * +env_find(char *name, char **envp, size_t *count) { - int count, found; - char **p, *envtmp; + char **ep, *p, *q; + size_t len; /* - * count the number of elements, including the null pointer; - * also set 'found' to -1 or index of entry if already in here. + * Find name in envp and return its value along with the + * index it was found at or the length of envp if not found. + * We treat a '=' in name as end of string for env_set(). */ - found = -1; - for (count = 0; envp[count] != NULL; count++) { - if (!strcmp_until(envp[count], envstr, '=')) - found = count; + for (p = name; *p && *p != '='; p++) + continue; + len = (size_t)(p - name); + for (ep = envp; (p = *ep) != NULL; ep++) { + if ((q = strchr(p, '=')) == NULL) + continue; + if ((size_t)(q - p) == len && strncmp(p, name, len) == 0) { + p = q + 1; + break; + } } - count++; /* for the NULL */ - - if (found != -1) { - /* - * it exists already, so just free the existing setting, - * save our new one there, and return the existing array. - */ - if ((envtmp = strdup(envstr)) == NULL) - return (NULL); - free(envp[found]); - envp[found] = envtmp; + *count = (size_t)(ep - envp); + return (p); +} + +char * +env_get(char *name, char **envp) +{ + size_t count; + + return (env_find(name, envp, &count)); +} + +char ** +env_set(char **envp, char *envstr) +{ + size_t count, len; + char **p, *envcopy; + + if ((envcopy = strdup(envstr)) == NULL) + return (NULL); + + /* Replace existing name if found. */ + if (env_find(envstr, envp, &count) != NULL) { + free(envp[count]); + envp[count] = envcopy; return (envp); } - /* - * it doesn't exist yet, so resize the array, move null pointer over - * one, save our string over the old null pointer, and return resized - * array. - */ - if ((envtmp = strdup(envstr)) == NULL) - return (NULL); - p = reallocarray(envp, count+1, sizeof(char **)); + /* Realloc envp and append new variable. */ + p = reallocarray(envp, count + 2, sizeof(char **)); if (p == NULL) { - free(envtmp); + free(envcopy); return (NULL); } - p[count] = p[count-1]; - p[count-1] = envtmp; + p[count++] = envcopy; + p[count] = NULL; return (p); } @@ -226,18 +241,3 @@ load_env(char *envstr, FILE *f) return (FALSE); return (TRUE); } - -char * -env_get(char *name, char **envp) -{ - int len = strlen(name); - char *p, *q; - - while ((p = *envp++) != NULL) { - if (!(q = strchr(p, '='))) - continue; - if ((q - p) == len && !strncmp(p, name, len)) - return (q+1); - } - return (NULL); -} diff --git a/usr.sbin/cron/funcs.h b/usr.sbin/cron/funcs.h index 6c938d9714f..f133d7b55c0 100644 --- a/usr.sbin/cron/funcs.h +++ b/usr.sbin/cron/funcs.h @@ -1,4 +1,4 @@ -/* $OpenBSD: funcs.h,v 1.19 2015/10/06 14:58:37 tedu Exp $ */ +/* $OpenBSD: funcs.h,v 1.20 2015/10/29 21:19:09 millert Exp $ */ /* * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") @@ -50,7 +50,6 @@ int job_runqueue(void), load_env(char *, FILE *), cron_pclose(FILE *, pid_t), glue_strings(char *, size_t, const char *, const char *, char), - strcmp_until(const char *, const char *, char), allowed(const char *, const char *, const char *), open_socket(void), safe_p(const char *, const char *), diff --git a/usr.sbin/cron/misc.c b/usr.sbin/cron/misc.c index 625e9ffa579..6b7898a8988 100644 --- a/usr.sbin/cron/misc.c +++ b/usr.sbin/cron/misc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: misc.c,v 1.61 2015/10/29 21:17:47 millert Exp $ */ +/* $OpenBSD: misc.c,v 1.62 2015/10/29 21:19:09 millert Exp $ */ /* Copyright 1988,1990,1993,1994 by Paul Vixie * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") @@ -22,21 +22,6 @@ static int LogFD = -1; static int syslog_open = FALSE; -int -strcmp_until(const char *left, const char *right, char until) -{ - while (*left && *left != until && *left == *right) { - left++; - right++; - } - - if ((*left=='\0' || *left == until) && - (*right=='\0' || *right == until)) { - return (0); - } - return (*left - *right); -} - void set_cron_cwd(void) { |