summaryrefslogtreecommitdiff
path: root/usr.sbin/cron/env.c
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>2015-10-29 21:19:10 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>2015-10-29 21:19:10 +0000
commite055efe28a9c2594e47e61418a3993dbe88d53ae (patch)
treedfcecd4da57898e1fb4af6def58470d6b2175140 /usr.sbin/cron/env.c
parent0cac71277996d182ed6a0ed979b8773a6a23c04b (diff)
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.
Diffstat (limited to 'usr.sbin/cron/env.c')
-rw-r--r--usr.sbin/cron/env.c98
1 files changed, 49 insertions, 49 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);
-}