Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / dmake / unix / sysvr1 / putenv.c
blob1453852710c240a0fd67863cb4fb9215dd3cfc37
1 /* RCS $Id: putenv.c,v 1.1.1.1 2000-09-22 15:33:35 hr Exp $
2 --
3 -- SYNOPSIS
4 -- My own putenv for BSD like systems.
5 --
6 -- DESCRIPTION
7 -- This originally came from MKS, but I rewrote it to fix a bug with
8 -- replacing existing strings, probably never happened but the code
9 -- was wrong nonetheless.
11 -- AUTHOR
12 -- Dennis Vadura, dvadura@dmake.wticorp.com
14 -- WWW
15 -- http://dmake.wticorp.com/
17 -- COPYRIGHT
18 -- Copyright (c) 1996,1997 by WTI Corp. All rights reserved.
20 -- This program is NOT free software; you can redistribute it and/or
21 -- modify it under the terms of the Software License Agreement Provided
22 -- in the file <distribution-root>/readme/license.txt.
24 -- LOG
25 -- Use cvs log to obtain detailed change logs.
28 #include <stdio.h>
29 #include <string.h>
31 int
32 putenv( str )/*
33 ===============
34 Take a string of the form NAME=value and stick it into the environment.
35 We do this by allocating a new set of pointers if we have to add a new
36 string and by replacing an existing pointer if the value replaces the value
37 of an existing string. */
38 char *str;
40 extern char **environ; /* The current environment. */
41 static char **ourenv = NULL; /* A new environment */
42 register char **p;
43 register char *q;
44 int size;
46 /* First search the current environment and see if we can replace a
47 * string. */
48 for( p=environ; *p; p++ ) {
49 register char *s = str;
51 for( q = *p; *q && *s && *s == *q; q++, s++ )
52 if( *s == '=' ) {
53 *p = str;
54 return(0); /* replaced it so go away */
58 /* Ok, can't replace a string so need to grow the environment. */
59 size = p - environ + 2; /* size of new environment */
60 /* size of old is size-1 */
62 /* It's the first time, so allocate a new environment since we don't know
63 * where the old one is comming from. */
64 if( ourenv == NULL ) {
65 if( (ourenv = (char **) malloc( sizeof(char *)*size )) == NULL )
66 return(1);
68 memcpy( (char *)ourenv, (char *)environ, (size-2)*sizeof(char *) );
70 else if( (ourenv = (char **)realloc( ourenv, size*sizeof(char *))) == NULL )
71 return(1);
73 ourenv[--size] = NULL;
74 ourenv[--size] = str;
76 environ = ourenv;
77 return(0);