home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / DMAKE38C.ZIP / TOS / PUTENV.C < prev    next >
C/C++ Source or Header  |  1992-01-23  |  3KB  |  88 lines

  1. /* RCS      -- $Header: /u2/dvadura/src/generic/dmake/src/tos/putenv.c,v 1.1 1992/01/24 03:27:17 dvadura Exp $
  2. -- SYNOPSIS -- my own putenv for BSD like systems.
  3. -- 
  4. -- DESCRIPTION
  5. --     This originally came from MKS, but I rewrote it to fix a bug with
  6. --    replacing existing strings, probably never happened but the code
  7. --    was wrong nonetheless.
  8. --
  9. -- AUTHOR
  10. --      Dennis Vadura, dvadura@watdragon.uwaterloo.ca
  11. --      CS DEPT, University of Waterloo, Waterloo, Ont., Canada
  12. --
  13. -- COPYRIGHT
  14. --      Copyright (c) 1990 by Dennis Vadura.  All rights reserved.
  15. -- 
  16. --      This program is free software; you can redistribute it and/or
  17. --      modify it under the terms of the GNU General Public License
  18. --      (version 1), as published by the Free Software Foundation, and
  19. --      found in the file 'LICENSE' included with this distribution.
  20. -- 
  21. --      This program is distributed in the hope that it will be useful,
  22. --      but WITHOUT ANY WARRANTY; without even the implied warrant of
  23. --      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24. --      GNU General Public License for more details.
  25. -- 
  26. --      You should have received a copy of the GNU General Public License
  27. --      along with this program;  if not, write to the Free Software
  28. --      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29. --
  30. -- LOG
  31. --     $Log: putenv.c,v $
  32.  * Revision 1.1  1992/01/24  03:27:17  dvadura
  33.  * dmake Version 3.8, Initial revision
  34.  *
  35. */
  36.  
  37. #include <stdio.h>
  38. #include <string.h>
  39.  
  40. int
  41. putenv( str )/*
  42. ===============
  43.    Take a string of the form NAME=value and stick it into the environment.
  44.    We do this by allocating a new set of pointers if we have to add a new
  45.    string and by replacing an existing pointer if the value replaces the value
  46.    of an existing string. */
  47. char *str;
  48. {
  49.    extern char **environ;        /* The current environment. */
  50.    static char **ourenv = NULL;        /* A new environment        */
  51.    register char **p;
  52.    register char *q;
  53.    int      size;
  54.  
  55.    /* First search the current environment and see if we can replace a
  56.     * string. */
  57.    for( p=environ; *p; p++ ) {
  58.       register char *s = str;
  59.  
  60.       for( q = *p; *q && *s && *s == *q; q++, s++ )
  61.      if( *s == '=' ) {
  62.         *p = str;
  63.         return(0);            /* replaced it so go away */
  64.      }
  65.    }
  66.  
  67.    /* Ok, can't replace a string so need to grow the environment. */
  68.    size = p - environ + 2;    /* size of new environment */
  69.                 /* size of old is size-1   */
  70.  
  71.    /* It's the first time, so allocate a new environment since we don't know
  72.     * where the old one is comming from. */
  73.    if( ourenv == NULL ) {
  74.       if( (ourenv = (char **) malloc( sizeof(char *)*size )) == NULL )
  75.      return(1);
  76.  
  77.       memcpy( (char *)ourenv, (char *)environ, (size-2)*sizeof(char *) );
  78.    }
  79.    else if( (ourenv = (char **)realloc( ourenv, size*sizeof(char *))) == NULL )
  80.       return(1);
  81.  
  82.    ourenv[--size] = NULL;
  83.    ourenv[--size] = str;
  84.  
  85.    environ = ourenv;
  86.    return(0);
  87. }
  88.