xref: /onnv-gate/usr/src/lib/libbc/libc/gen/common/putenv.c (revision 722:636b850d4ee9)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 1987 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*      Copyright (c) 1984 AT&T */
280Sstevel@tonic-gate /*        All Rights Reserved   */
290Sstevel@tonic-gate 
30*722Smuffin #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*	LINTLIBRARY	*/
330Sstevel@tonic-gate /*	putenv - change environment variables
34*722Smuffin  *
35*722Smuffin  *	input - char *change = a pointer to a string of the form
36*722Smuffin  *			       "name=value"
37*722Smuffin  *
38*722Smuffin  *	output - 0, if successful
39*722Smuffin  *		 1, otherwise
40*722Smuffin  */
410Sstevel@tonic-gate 
42*722Smuffin #include <stdio.h>
43*722Smuffin #include <stdlib.h>
44*722Smuffin 
450Sstevel@tonic-gate extern char **environ;		/* pointer to enviroment */
46*722Smuffin static int	reall;		/* flag to reallocate space, if putenv is called
470Sstevel@tonic-gate 				   more than once */
48*722Smuffin static int	find(char *);
49*722Smuffin static int	match(char *, char *);
500Sstevel@tonic-gate 
510Sstevel@tonic-gate int
putenv(char * change)52*722Smuffin putenv(char *change)
530Sstevel@tonic-gate {
540Sstevel@tonic-gate 	char **newenv;		    /* points to new environment */
55*722Smuffin 	int which;	    /* index of variable to replace */
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 	if ((which = find(change)) < 0)  {
580Sstevel@tonic-gate 		/* if a new variable */
590Sstevel@tonic-gate 		/* which is negative of table size, so invert and
600Sstevel@tonic-gate 		   count new element */
610Sstevel@tonic-gate 		which = (-which) + 1;
620Sstevel@tonic-gate 		if (reall)  {
630Sstevel@tonic-gate 			/* we have expanded environ before */
640Sstevel@tonic-gate 			newenv = (char **)realloc(environ,
650Sstevel@tonic-gate 				  which*sizeof(char *));
66*722Smuffin 			if (newenv == NULL)  return (-1);
670Sstevel@tonic-gate 			/* now that we have space, change environ */
680Sstevel@tonic-gate 			environ = newenv;
690Sstevel@tonic-gate 		} else {
700Sstevel@tonic-gate 			/* environ points to the original space */
710Sstevel@tonic-gate 			reall++;
720Sstevel@tonic-gate 			newenv = (char **)malloc(which*sizeof(char *));
73*722Smuffin 			if (newenv == NULL)  return (-1);
740Sstevel@tonic-gate 			(void)memcpy((char *)newenv, (char *)environ,
750Sstevel@tonic-gate  				(int)(which*sizeof(char *)));
760Sstevel@tonic-gate 			environ = newenv;
770Sstevel@tonic-gate 		}
780Sstevel@tonic-gate 		environ[which-2] = change;
790Sstevel@tonic-gate 		environ[which-1] = NULL;
800Sstevel@tonic-gate 	}  else  {
810Sstevel@tonic-gate 		/* we are replacing an old variable */
820Sstevel@tonic-gate 		environ[which] = change;
830Sstevel@tonic-gate 	}
84*722Smuffin 	return (0);
850Sstevel@tonic-gate }
860Sstevel@tonic-gate 
870Sstevel@tonic-gate /*	find - find where s2 is in environ
880Sstevel@tonic-gate  *
890Sstevel@tonic-gate  *	input - str = string of form name=value
900Sstevel@tonic-gate  *
910Sstevel@tonic-gate  *	output - index of name in environ that matches "name"
920Sstevel@tonic-gate  *		 -size of table, if none exists
930Sstevel@tonic-gate */
94*722Smuffin static int
find(char * str)95*722Smuffin find(char *str)
960Sstevel@tonic-gate {
97*722Smuffin 	int ct = 0;	/* index into environ */
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	while(environ[ct] != NULL)   {
1000Sstevel@tonic-gate 		if (match(environ[ct], str)  != 0)
101*722Smuffin 			return (ct);
1020Sstevel@tonic-gate 		ct++;
1030Sstevel@tonic-gate 	}
104*722Smuffin 	return (-(++ct));
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate /*
1070Sstevel@tonic-gate  *	s1 is either name, or name=value
1080Sstevel@tonic-gate  *	s2 is name=value
1090Sstevel@tonic-gate  *	if names match, return value of 1,
1100Sstevel@tonic-gate  *	else return 0
1110Sstevel@tonic-gate  */
1120Sstevel@tonic-gate 
113*722Smuffin static int
match(char * s1,char * s2)114*722Smuffin match(char *s1, char *s2)
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate 	while(*s1 == *s2++)  {
1170Sstevel@tonic-gate 		if (*s1 == '=')
118*722Smuffin 			return (1);
1190Sstevel@tonic-gate 		s1++;
1200Sstevel@tonic-gate 	}
121*722Smuffin 	return (0);
1220Sstevel@tonic-gate }
123