Glad you figured this out ! Can you please open a PR with your improvements?

- Alon



On Fri, Apr 26, 2019 at 11:07 AM <[email protected]> wrote:

> At Friday, 26. April 2019 10:32:34 UTC+2 [email protected] wrote:
>
>     Hello
>
>     I made progress in calling a C function from JavaScript.
>
>    ...
>
>     I had not the time to make tests with getenv() and setenv(), but
>     "It compiles so it must be okay". (Copyright Thomas Mertes) :-)
>     Feel free to use it for emscripten.
>
>
> Famous last words ...
> Now I had time to do some tests with my C implementations of
> getenv() and setenv(). And I have to admit: There was actually
> a bug in setenv(). A multiplication with sizeof(char *) was
> missing in the realloc...
>
> Below are the newest versions of getenv() and setenv().
> Feel free to use them for Emscripten.
>
> -------------------- Begin of code snippet --------------------
> char **environ = NULL;
>
>
> char *getenv (const char *name)
>
>   {
>     size_t len;
>     char **p, *c;
>
>   /* getenv */
>     if (name == NULL || environ == NULL || strchr(name, '=') != NULL) {
>       return NULL;
>     } else {
>       len = strlen(name);
>       for (p = environ; (c = *p) != NULL; ++p) {
>         if (strncmp(c, name, len) == 0 && c[len] == '=') {
>           return &c[len + 1];
>         } /* if */
>       } /* for */
>       return NULL;
>     } /* if */
>   } /* getenv */
>
>
> int setenv (const char *name, const char *value, int overwrite)
>
>   {
>     size_t len;
>     char **p, *c;
>     size_t nameCount = 0;
>
>   /* setenv */
>     if (name == NULL || name[0] == '\0' || strchr(name, '=') != NULL) {
>       errno = EINVAL;
>       return -1;
>     } else {
>       len = strlen(name);
>       if (environ != NULL) {
>         for (p = environ; (c = *p) != NULL; ++p) {
>           nameCount ++;
>           if (strncmp(c, name, len) == 0 && c[len] == '=') {
>             if (overwrite) {
>               if ((*p = realloc(*p, len + strlen(value) + 2)) == NULL) {
>                 errno = ENOMEM;
>                 return -1;
>               } else {
>                 strcpy(&c[len + 1], value);
>               } /* if */
>             } /* if **/
>             return 0;
>           } /* if */
>         } /* for */
>       } /* if */
>       if ((environ = realloc(environ7, sizeof(char *) * (nameCount + 2)))
> == NULL ||
>           (c = malloc(len + strlen(value) + 2)) == NULL) {
>         errno = ENOMEM;
>         return -1;
>       } else {
>         memcpy(c, name, len);
>         c[len] = '=';
>         strcpy(&c[len + 1], value);
>         environ[nameCount] = c;
>         environ[nameCount + 1] = NULL;
>         return 0;
>       } /* if */
>     } /* if */
>   } /* setenv */
> -------------------- End of code snippet --------------------
>
> I would really prefer, when the runtime library of emcc
> would have a working getenv() and setenv() without
>
>   Error: Environment size exceeded TOTAL_ENV_SIZE!
>
> This is not a way a C run-time function should work.
> Artificial limitations like: "The size of the environment
> must be less than 1024", should be a thing of the past.
>
> Sorry for the rant. Emscripten is great and I succeed in
> compiling the Seed7 interpreter with it. The test programs
> for the Seed7 interpreter succeed. The latest release of
> Seed7 contains the changes necessary to compile Seed7
> with emcc. You can download seed7_05_20190407.tgz from
>
>   https://sourceforge.net/projects/seed7/files/
>
> The file seed7/src/read_me.txt contains detailed
> information how to compile Seed7 with emcc.
>
> Many thanks to the developers of Emscripten.
> Keep on with the good work.
>
>
> Regards,
> Thomas Mertes
>
> --
> Seed7 Homepage:  http://seed7.sourceforge.net
> Seed7 - The extensible programming language: User defined statements
> and operators, abstract data types, templates without special
> syntax, OO with interfaces and multiple dispatch, statically typed,
> interpreted or compiled, portable, runs under linux/unix/windows.
>
> --
> You received this message because you are subscribed to the Google Groups
> "emscripten-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to