setenv.cc revision 10d63b7db37a83b39c7f511cf9426c9d03ea0760
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe/*
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * CDDL HEADER START
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe *
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * The contents of this file are subject to the terms of the
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Common Development and Distribution License (the "License").
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * You may not use this file except in compliance with the License.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe *
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * or http://www.opensolaris.org/os/licensing.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * See the License for the specific language governing permissions
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * and limitations under the License.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe *
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * When distributing Covered Code, include this CDDL HEADER in each
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * If applicable, add the following below this CDDL HEADER, with the
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * fields enclosed by brackets "[]" replaced with your own identifying
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * information: Portions Copyright [yyyy] [name of copyright owner]
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe *
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * CDDL HEADER END
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe/*
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Copyright 1994 Sun Microsystems, Inc. All rights reserved.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Use is subject to license terms.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#include <stdlib.h>
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#include <string.h>
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#include <unistd.h>
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Loweextern char **environ;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowestatic short setenv_made_new_vector= 0;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowechar *setenv(char *name, char *value)
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe{ char *p= NULL, **q;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe int length= 0, vl;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe if ((p= getenv(name)) == NULL) { /* Allocate new vector */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe for (q= environ; *q != NULL; q++, length++);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe q= (char **)malloc((unsigned)(sizeof(char *)*(length+2)));
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe memcpy(((char *)q)+sizeof(char *), (char *)environ, sizeof(char *)*(length+1));
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe if (setenv_made_new_vector++)
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe free((char *)environ);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe length= strlen(name);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe environ= q;}
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe else { /* Find old slot */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe length= strlen(name);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe for (q= environ; *q != NULL; q++)
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe if (!strncmp(*q, name, length))
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe break;};
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe vl= strlen(value);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe if (!p || (length+vl+1 > strlen(p)))
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe *q= p= (char *) malloc((unsigned)(length+vl+2));
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe else
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe p= *q;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe (void)strcpy(p, name); p+= length;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe *p++= '=';
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe (void)strcpy(p, value);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe return(value);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe}