mescc: Mes C Library: Prepare for M2-Planet: setenv.

* lib/posix/setenv.c: Rewrite C-constructs not supported by M2-Planet.
This commit is contained in:
Jan (janneke) Nieuwenhuizen 2020-05-17 19:28:58 +02:00
parent cf7fc8ec2b
commit 2012304dc8
No known key found for this signature in database
GPG key ID: F3C1A0D9C1D65273

View file

@ -22,25 +22,34 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
// CONSTANT M2_PTR_SIZE 4
#define M2_PTR_SIZE 1
int int
setenv (char const *s, char const *v, int overwrite_p) setenv (char const *s, char const *v, int overwrite_p)
{ {
char **p = environ; char **p = environ;
int length = strlen (s); int length = strlen (s);
while (*p)
while (p[0] != 0)
{ {
if (!strncmp (s, *p, length) && *(*p + length) == '=') if (strncmp (s, p[0], length) == 0)
{
char *q = p[0] + length;
if (q[0] == '=')
break; break;
p++; }
p = p + M2_PTR_SIZE;
} }
char *entry = malloc (length + strlen (v) + 2); char *entry = malloc (length + strlen (v) + 2);
int end_p = *p == 0; int end_p = p[0] == 0;
*p = entry; p[0] = entry;
strcpy (entry, s); strcpy (entry, s);
strcpy (entry + length, "="); strcpy (entry + length, "=");
strcpy (entry + length + 1, v); strcpy (entry + length + 1, v);
*(entry + length + strlen (v) + 2) = 0; entry[length + strlen (v) + 2] = 0;
if (end_p) if (end_p != 0)
*++p = 0; p[1] = 0;
return 0; return 0;
} }