In getenv.c, the __findenv function is changed to use size_t for its
length parameter, and ptrdiff_t for its offset parameter.
The getenv function is modified accordingly.

In setenv.c, the setenv function is also modified to match the changes
to __findenv, as well as to use size_t for all variables representing
object sizes.
---
 getenv.c |   11 ++++++-----
 setenv.c |   31 +++++++++++++++++--------------
 2 files changed, 23 insertions(+), 19 deletions(-)

diff --git a/getenv.c b/getenv.c
index fd8482e..0b7a5f6 100644
--- a/getenv.c
+++ b/getenv.c
@@ -29,9 +29,10 @@
  */
 
 #include <stdlib.h>
+#include <stddef.h>
 #include <string.h>
 
-char *__findenv(const char *name, int len, int *offset);
+char *__findenv(const char *name, size_t len, ptrdiff_t *offset);
 
 /*
  * __findenv --
@@ -44,10 +45,10 @@ char *__findenv(const char *name, int len, int *offset);
  *     This routine *should* be a static; don't use it.
  */
 char *
-__findenv(const char *name, int len, int *offset)
+__findenv(const char *name, size_t len, ptrdiff_t *offset)
 {
        extern char **environ;
-       int i;
+       size_t i;
        const char *np;
        char **p, *cp;
 
@@ -72,10 +73,10 @@ __findenv(const char *name, int len, int *offset)
 char *
 getenv(const char *name)
 {
-       int offset = 0;
+       ptrdiff_t offset = 0;
        const char *np;
 
        for (np = name; *np && *np != '='; ++np)
                ;
-       return (__findenv(name, (int)(np - name), &offset));
+       return (__findenv(name, (size_t)(np - name), &offset));
 }
diff --git a/setenv.c b/setenv.c
index 089ab92..4f2b915 100644
--- a/setenv.c
+++ b/setenv.c
@@ -30,9 +30,10 @@
 
 #include <errno.h>
 #include <stdlib.h>
+#include <stddef.h>
 #include <string.h>
 
-char *__findenv(const char *name, int len, int *offset);
+char *__findenv(const char *name, size_t len, ptrdiff_t *offset);
 
 extern char **environ;
 static char **lastenv;                         /* last value of environ */
@@ -47,7 +48,7 @@ putenv(char *str)
 {
        char **P, *cp;
        size_t cnt;
-       int offset = 0;
+       ptrdiff_t offset = 0;
 
        for (cp = str; *cp && *cp != '='; ++cp)
                ;
@@ -56,10 +57,10 @@ putenv(char *str)
                return (-1);                    /* missing `=' in string */
        }
 
-       if (__findenv(str, (int)(cp - str), &offset) != NULL) {
+       if (__findenv(str, (size_t)(cp - str), &offset) != NULL) {
                environ[offset++] = str;
                /* could be set multiple times */
-               while (__findenv(str, (int)(cp - str), &offset)) {
+               while (__findenv(str, (size_t)(cp - str), &offset)) {
                        for (P = &environ[offset];; ++P)
                                if (!(*P = *(P + 1)))
                                        break;
@@ -92,7 +93,8 @@ setenv(const char *name, const char *value, int rewrite)
 {
        char *C, **P;
        const char *np;
-       int l_value, offset = 0;
+       size_t value_len, name_len;
+       ptrdiff_t offset = 0;
 
        for (np = name; *np && *np != '='; ++np)
                ;
@@ -102,21 +104,22 @@ setenv(const char *name, const char *value, int rewrite)
                return (-1);                    /* has `=' in name */
        }
 #endif
+       name_len = (size_t)(np - name);
 
-       l_value = strlen(value);
-       if ((C = __findenv(name, (int)(np - name), &offset)) != NULL) {
-               int tmpoff = offset + 1;
+       value_len = strlen(value);
+       if ((C = __findenv(name, name_len, &offset)) != NULL) {
+               ptrdiff_t tmpoff = offset + 1;
                if (!rewrite)
                        return (0);
 #if 0 /* XXX - existing entry may not be writable */
-               if (strlen(C) >= l_value) {     /* old larger; copy over */
+               if (strlen(C) >= value_len) {   /* old larger; copy over */
                        while ((*C++ = *value++))
                                ;
                        return (0);
                }
 #endif
                /* could be set multiple times */
-               while (__findenv(name, (int)(np - name), &tmpoff)) {
+               while (__findenv(name, name_len, &tmpoff)) {
                        for (P = &environ[tmpoff];; ++P)
                                if (!(*P = *(P + 1)))
                                        break;
@@ -136,8 +139,8 @@ setenv(const char *name, const char *value, int rewrite)
                offset = cnt;
                environ[cnt + 1] = NULL;
        }
-       if (!(environ[offset] =                 /* name + `=' + value */
-           malloc((size_t)((int)(np - name) + l_value + 2))))
+       /* name + `=' + value */
+       if (!(environ[offset] = malloc(name_len + value_len + 2)))
                return (-1);
        for (C = environ[offset]; (*C = *name++) && *C != '='; ++C)
                ;
@@ -155,7 +158,7 @@ unsetenv(const char *name)
 {
        char **P;
        const char *np;
-       int offset = 0;
+       ptrdiff_t offset = 0;
 
        if (!name || !*name) {
                errno = EINVAL;
@@ -169,7 +172,7 @@ unsetenv(const char *name)
        }
 
        /* could be set multiple times */
-       while (__findenv(name, (int)(np - name), &offset)) {
+       while (__findenv(name, (size_t)(np - name), &offset)) {
                for (P = &environ[offset];; ++P)
                        if (!(*P = *(P + 1)))
                                break;
-- 
1.7.7.5 (Apple Git-26)

Reply via email to