www.delorie.com/djgpp/bugs/show.cgi   search  
Bug 000047

When Created: 01/09/1996 02:11:25
Against DJGPP version: 2.00.beta3
By whom: eliz@is.elta.co.il
Abstract: `setenv' library function doesn't work (in beta4)
The `setenv' library function incorrectly jmp's to putenv.  But `putenv'
expects a single argument, while `setenv' accepts 3, so it doesn't work.

Solution added: 01/09/1996 02:14:43
By whom: eliz@is.elta.co.il
#include <stdlib.h>
#include <string.h>
#include <libc/stubs.h>

int
setenv (const char *var, const char *val, int replace)
{
  char *prev;

  if (var == (char *)0 || val == (char *)0)
    return -1;

  if ((prev  = getenv (var)) && !replace)
    return 0;
  else
    {
      size_t l_var = strlen (var);
      char *envstr = (char *)alloca (l_var + strlen (val) + 2);
      char *peq    = strchr (var, '=');

      if (*val == '=')
        ++val;
      if (peq)
        l_var = peq - var;

      strncpy (envstr, var, l_var);
      envstr[l_var++] = '=';
      strcpy (envstr + l_var, val);

      return putenv (envstr);
    }
}

#ifdef  TEST

#include <stdio.h>

int
main(void)
{
  char p[] = "ENVTEST=set-with-setenv-in-a-single-string";

  printf("ENVTEST=%s\n", getenv("ENVTEST"));
  putenv("ENVTEST=set-with-putenv");
  printf("ENVTEST=%s\n", getenv("ENVTEST"));
  setenv("ENVTEST", "set-with-setenv-safely", 0);
  printf("ENVTEST=%s\n", getenv("ENVTEST"));
  setenv("ENVTEST", "set-with-setenv-forcibly", 1);
  printf("ENVTEST=%s\n", getenv("ENVTEST"));
  setenv("ENVTEST", "=set-with-setenv-with-equals", 1);
  printf("ENVTEST=%s\n", getenv("ENVTEST"));
  setenv(p, p + 8, 1);
  printf("ENVTEST=%s\n", getenv("ENVTEST"));
  setenv("ENVTEST", "", 1);
  printf("ENVTEST=%s\n", getenv("ENVTEST"));

  return 0;
}

#endif

Fixed in version 2.00.beta5 on 01/23/1996 21:58:54
By whom: dj@delorie.com



  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2010   by DJ Delorie     Updated Jul 2010