From 2012304dc849601658184d988a4935eb124deabd Mon Sep 17 00:00:00 2001 From: "Jan (janneke) Nieuwenhuizen" Date: Sun, 17 May 2020 19:28:58 +0200 Subject: [PATCH] mescc: Mes C Library: Prepare for M2-Planet: setenv. * lib/posix/setenv.c: Rewrite C-constructs not supported by M2-Planet. --- lib/posix/setenv.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/posix/setenv.c b/lib/posix/setenv.c index 1660f3fa..a5ee8ff8 100644 --- a/lib/posix/setenv.c +++ b/lib/posix/setenv.c @@ -22,25 +22,34 @@ #include #include +// CONSTANT M2_PTR_SIZE 4 +#define M2_PTR_SIZE 1 + int setenv (char const *s, char const *v, int overwrite_p) { char **p = environ; int length = strlen (s); - while (*p) + + while (p[0] != 0) { - if (!strncmp (s, *p, length) && *(*p + length) == '=') - break; - p++; + if (strncmp (s, p[0], length) == 0) + { + char *q = p[0] + length; + if (q[0] == '=') + break; + } + p = p + M2_PTR_SIZE; } char *entry = malloc (length + strlen (v) + 2); - int end_p = *p == 0; - *p = entry; + int end_p = p[0] == 0; + p[0] = entry; strcpy (entry, s); strcpy (entry + length, "="); strcpy (entry + length + 1, v); - *(entry + length + strlen (v) + 2) = 0; - if (end_p) - *++p = 0; + entry[length + strlen (v) + 2] = 0; + if (end_p != 0) + p[1] = 0; + return 0; }