On Wed, 28 Oct 2015 14:52:21 -0600, "Todd C. Miller" wrote:
> It was only used in env_set(). I've also removed the useless
> FACILITY define and fixed a sizeof().
New diff that converts env_get() into env_find() and uses it similarly
to how __findenv() is used in libc. It's less code that way.
- todd
Index: usr.sbin/cron/env.c
===================================================================
RCS file: /cvs/src/usr.sbin/cron/env.c,v
retrieving revision 1.29
diff -u -p -u -r1.29 env.c
--- usr.sbin/cron/env.c 9 Feb 2015 22:35:08 -0000 1.29
+++ usr.sbin/cron/env.c 29 Oct 2015 20:39:27 -0000
@@ -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,65 @@ env_copy(char **envp)
return (p);
}
-char **
-env_set(char **envp, char *envstr)
+static char *
+env_find(char *name, char **envp0, size_t *count)
{
- int count, found;
- char **p, *envtmp;
+ char **envp = envp0;
+ char *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;
- }
- 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;
- return (envp);
+ for (p = name; *p && *p != '='; p++)
+ continue;
+ len = (size_t)(p - name);
+ while ((p = *envp++) != NULL) {
+ if (!(q = strchr(p, '=')))
+ continue;
+ if ((size_t)(q - p) == len && !strncmp(p, name, len)) {
+ p = q + 1;
+ break;
+ }
}
+ *count = (size_t)(envp - envp0);
+ return (p);
+}
- /*
- * 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)
+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);
- p = reallocarray(envp, count+1, sizeof(char **));
+
+ /* Replace existing name if found. */
+ if (env_find(envstr, envp, &count) != NULL) {
+ free(envp[count]);
+ envp[count] = envcopy;
+ return (envp);
+ }
+
+ /* 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);
}
@@ -225,19 +241,4 @@ load_env(char *envstr, FILE *f)
if (snprintf(envstr, MAX_ENVSTR, "%s=%s", name, val) >= MAX_ENVSTR)
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);
}
Index: usr.sbin/cron/funcs.h
===================================================================
RCS file: /cvs/src/usr.sbin/cron/funcs.h,v
retrieving revision 1.19
diff -u -p -u -r1.19 funcs.h
--- usr.sbin/cron/funcs.h 6 Oct 2015 14:58:37 -0000 1.19
+++ usr.sbin/cron/funcs.h 28 Oct 2015 20:09:39 -0000
@@ -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 *),
Index: usr.sbin/cron/misc.c
===================================================================
RCS file: /cvs/src/usr.sbin/cron/misc.c,v
retrieving revision 1.60
diff -u -p -u -r1.60 misc.c
--- usr.sbin/cron/misc.c 26 Oct 2015 15:16:30 -0000 1.60
+++ usr.sbin/cron/misc.c 28 Oct 2015 20:09:47 -0000
@@ -20,33 +20,9 @@
#include "cron.h"
#include <limits.h>
-#if defined(LOG_DAEMON) && !defined(LOG_CRON)
-# define LOG_CRON LOG_DAEMON
-#endif
-
-#ifndef FACILITY
-#define FACILITY LOG_CRON
-#endif
-
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)
{
@@ -272,7 +248,7 @@ log_it(const char *username, pid_t xpid,
"END EDIT", "LIST", "MAIL", "RELOAD", "REPLACE", "STARTUP", NULL };
if (!syslog_open) {
- openlog(ProgramName, LOG_PID, FACILITY);
+ openlog(ProgramName, LOG_PID, LOG_CRON);
syslog_open = TRUE; /* assume openlog success */
}