home *** CD-ROM | disk | FTP | other *** search
/ ftp.uv.es / 2014.11.ftp.uv.es.tar / ftp.uv.es / pub / unix / elm-2.4-pl20.tar.Z / elm-2.4-pl20.tar / lib / putenv.c < prev    next >
C/C++ Source or Header  |  1992-10-03  |  2KB  |  82 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: putenv.c,v 5.1 1992/10/03 22:41:36 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 5.1 $   $State: Exp $
  6.  *
  7.  *            Copyright (c) 1992 USENET Community Trust
  8.  *******************************************************************************
  9.  * Bug reports, patches, comments, suggestions should be sent to:
  10.  *
  11.  *    Syd Weinstein, Elm Coordinator
  12.  *    elm@DSI.COM            dsinc!elm
  13.  *
  14.  *******************************************************************************
  15.  * $Log: putenv.c,v $
  16.  * Revision 5.1  1992/10/03  22:41:36  syd
  17.  * Initial checkin as of 2.4 Release at PL0
  18.  *
  19.  *
  20.  ******************************************************************************/
  21.  
  22. /*
  23.  * This code was stolen from cnews.  Modified to make "newenv" static so
  24.  * that realloc() can be used on subsequent calls to avoid memory leaks.
  25.  *
  26.  * We only need this if Configure said there isn't a putenv() in libc.
  27.  */
  28.  
  29. #include "headers.h"
  30.  
  31. #ifndef PUTENV /*{*/
  32.  
  33. /* peculiar return values */
  34. #define WORKED 0
  35. #define FAILED 1
  36.  
  37. int
  38. putenv(var)            /* put var in the environment */
  39. char *var;
  40. {
  41.     register char **envp;
  42.     register int oldenvcnt;
  43.     extern char **environ;
  44.     static char **newenv = NULL;
  45.  
  46.     /* count variables, look for var */
  47.     for (envp = environ; *envp != 0; envp++) {
  48.         register char *varp = var, *ep = *envp;
  49.         register int namesame;
  50.  
  51.         namesame = NO;
  52.         for (; *varp == *ep && *varp != '\0'; ++ep, ++varp)
  53.             if (*varp == '=')
  54.                 namesame = YES;
  55.         if (*varp == *ep && *ep == '\0')
  56.             return WORKED;    /* old & new var's are the same */
  57.         if (namesame) {
  58.             *envp = var;    /* replace var with new value */
  59.             return WORKED;
  60.         }
  61.     }
  62.     oldenvcnt = envp - environ;
  63.  
  64.     /* allocate new environment with room for one more variable */
  65.     if (newenv == NULL)
  66.         newenv = (char **)malloc((unsigned)((oldenvcnt+1+1)*sizeof(*envp)));
  67.     else
  68.         newenv = (char **)realloc((char *)newenv, (unsigned)((oldenvcnt+1+1)*sizeof(*envp)));
  69.     if (newenv == NULL)
  70.         return FAILED;
  71.  
  72.     /* copy old environment pointers, add var, switch environments */
  73.     (void) bcopy((char *)environ, (char *)newenv, oldenvcnt*sizeof(*envp));
  74.     newenv[oldenvcnt] = var;
  75.     newenv[oldenvcnt+1] = NULL;
  76.     environ = newenv;
  77.     return WORKED;
  78. }
  79.  
  80. #endif /*}PUTENV*/
  81.  
  82.