It was only used in env_set().  I've also removed the useless
FACILITY define and fixed a sizeof().

 - 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 28 Oct 2015 20:41:54 -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;
@@ -66,46 +66,34 @@ env_copy(char **envp)
 char **
 env_set(char **envp, char *envstr)
 {
-       int count, found;
-       char **p, *envtmp;
+       size_t count, len;
+       char **p, *envcopy;
+
+       if ((envcopy = strdup(envstr)) == NULL)
+               return (NULL);
 
        /*
-        * count the number of elements, including the null pointer;
-        * also set 'found' to -1 or index of entry if already in here.
+        * look for envstr in envp and replace it if found, else we
+        * will realloc envp and append the new entry.
         */
-       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);
+       len = strcspn(envstr, "=");
+       for (p = envp; *p != NULL; p++) {
+               if (strcspn(*p, "=") == len &&
+                   strncmp(envstr, *p, len) == 0) {
+                       free(*p);
+                       *p = envcopy;
+                       return (envp);
+               }
        }
+       count = (size_t)(p - 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 **));
+       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);
 }
 
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 */
        }
 

Reply via email to