1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 1987 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* Copyright (c) 1984 AT&T */ 28 /* All Rights Reserved */ 29 30 #pragma ident "%Z%%M% %I% %E% SMI" /* from S5R2 1.2 */ 31 32 /* LINTLIBRARY */ 33 /* putenv - change environment variables 34 35 input - char *change = a pointer to a string of the form 36 "name=value" 37 38 output - 0, if successful 39 1, otherwise 40 */ 41 #define NULL 0 42 extern char **environ; /* pointer to enviroment */ 43 static reall; /* flag to reallocate space, if putenv is called 44 more than once */ 45 46 int 47 putenv(change) 48 char *change; 49 { 50 char **newenv; /* points to new environment */ 51 register int which; /* index of variable to replace */ 52 char *realloc(), *malloc(); /* memory alloc routines */ 53 54 if ((which = find(change)) < 0) { 55 /* if a new variable */ 56 /* which is negative of table size, so invert and 57 count new element */ 58 which = (-which) + 1; 59 if (reall) { 60 /* we have expanded environ before */ 61 newenv = (char **)realloc(environ, 62 which*sizeof(char *)); 63 if (newenv == NULL) return -1; 64 /* now that we have space, change environ */ 65 environ = newenv; 66 } else { 67 /* environ points to the original space */ 68 reall++; 69 newenv = (char **)malloc(which*sizeof(char *)); 70 if (newenv == NULL) return -1; 71 (void)memcpy((char *)newenv, (char *)environ, 72 (int)(which*sizeof(char *))); 73 environ = newenv; 74 } 75 environ[which-2] = change; 76 environ[which-1] = NULL; 77 } else { 78 /* we are replacing an old variable */ 79 environ[which] = change; 80 } 81 return 0; 82 } 83 84 /* find - find where s2 is in environ 85 * 86 * input - str = string of form name=value 87 * 88 * output - index of name in environ that matches "name" 89 * -size of table, if none exists 90 */ 91 static 92 find(str) 93 register char *str; 94 { 95 register int ct = 0; /* index into environ */ 96 97 while(environ[ct] != NULL) { 98 if (match(environ[ct], str) != 0) 99 return ct; 100 ct++; 101 } 102 return -(++ct); 103 } 104 /* 105 * s1 is either name, or name=value 106 * s2 is name=value 107 * if names match, return value of 1, 108 * else return 0 109 */ 110 111 static 112 match(s1, s2) 113 register char *s1, *s2; 114 { 115 while(*s1 == *s2++) { 116 if (*s1 == '=') 117 return 1; 118 s1++; 119 } 120 return 0; 121 } 122