From d454fe905d17eb852206f0ae9a3193e1159a125f Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Sun, 20 Oct 2019 19:28:51 +0200 Subject: [PATCH] mescc: Mes C Library: Prepare for M2-Planet: getenv. * lib/posix/getenv.c: Rewrite C-constructs not supported by M2-Planet. --- lib/posix/getenv.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/posix/getenv.c b/lib/posix/getenv.c index b8c58baa..cf630e25 100644 --- a/lib/posix/getenv.c +++ b/lib/posix/getenv.c @@ -1,4 +1,4 @@ -/* -*-comment-start: "//";comment-end:""-*- +/* * GNU Mes --- Maxwell Equations of Software * Copyright © 2016,2017,2018,2019 Jan (janneke) Nieuwenhuizen * @@ -22,16 +22,30 @@ #include #include +// CONSTANT M2_PTR_SIZE 4 +#define M2_PTR_SIZE 1 + char * getenv (char const *s) { + /* eputs ("\ngetenv s="); eputs (s); eputs ("\n"); */ char **p = environ; int length = strlen (s); - while (*p) + + while (p[0] != 0) { - if (!strncmp (s, *p, length) && *(*p + length) == '=') - return (*p + length + 1); - p++; + /* eputs ("getenv p[0]="); eputs (p[0]); eputs ("\n"); */ + if (strncmp (s, p[0], length) == 0) + { + /* eputs ("found!\n"); */ + char *q = p[0] + length; + if (q[0] == '=') + return q + 1; + } + /* else */ + /* eputs ("not found!\n"); */ + p = p + M2_PTR_SIZE; } + return 0; }