17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 237c478bd9Sstevel@tonic-gate * Copyright 1987 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate 30*5d54f3d8Smuffin #pragma ident "%Z%%M% %I% %E% SMI" 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate /* LINTLIBRARY */ 337c478bd9Sstevel@tonic-gate /* putenv - change environment variables 34*5d54f3d8Smuffin * 35*5d54f3d8Smuffin * input - char *change = a pointer to a string of the form 36*5d54f3d8Smuffin * "name=value" 37*5d54f3d8Smuffin * 38*5d54f3d8Smuffin * output - 0, if successful 39*5d54f3d8Smuffin * 1, otherwise 407c478bd9Sstevel@tonic-gate */ 41*5d54f3d8Smuffin 42*5d54f3d8Smuffin #include <stdio.h> 43*5d54f3d8Smuffin #include <stdlib.h> 44*5d54f3d8Smuffin 457c478bd9Sstevel@tonic-gate extern char **environ; /* pointer to enviroment */ 46*5d54f3d8Smuffin static int reall; /* flag to reallocate space, if putenv is called 477c478bd9Sstevel@tonic-gate more than once */ 48*5d54f3d8Smuffin static int find(char *); 49*5d54f3d8Smuffin static int match(char *, char *); 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate int 52*5d54f3d8Smuffin putenv(char *change) 537c478bd9Sstevel@tonic-gate { 547c478bd9Sstevel@tonic-gate char **newenv; /* points to new environment */ 55*5d54f3d8Smuffin int which; /* index of variable to replace */ 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate if ((which = find(change)) < 0) { 587c478bd9Sstevel@tonic-gate /* if a new variable */ 597c478bd9Sstevel@tonic-gate /* which is negative of table size, so invert and 607c478bd9Sstevel@tonic-gate count new element */ 617c478bd9Sstevel@tonic-gate which = (-which) + 1; 627c478bd9Sstevel@tonic-gate if (reall) { 637c478bd9Sstevel@tonic-gate /* we have expanded environ before */ 647c478bd9Sstevel@tonic-gate newenv = (char **)realloc(environ, 657c478bd9Sstevel@tonic-gate which*sizeof(char *)); 66*5d54f3d8Smuffin if (newenv == NULL) return (-1); 677c478bd9Sstevel@tonic-gate /* now that we have space, change environ */ 687c478bd9Sstevel@tonic-gate environ = newenv; 697c478bd9Sstevel@tonic-gate } else { 707c478bd9Sstevel@tonic-gate /* environ points to the original space */ 717c478bd9Sstevel@tonic-gate reall++; 727c478bd9Sstevel@tonic-gate newenv = (char **)malloc(which*sizeof(char *)); 73*5d54f3d8Smuffin if (newenv == NULL) return (-1); 747c478bd9Sstevel@tonic-gate (void)memcpy((char *)newenv, (char *)environ, 757c478bd9Sstevel@tonic-gate (int)(which*sizeof(char *))); 767c478bd9Sstevel@tonic-gate environ = newenv; 777c478bd9Sstevel@tonic-gate } 787c478bd9Sstevel@tonic-gate environ[which-2] = change; 797c478bd9Sstevel@tonic-gate environ[which-1] = NULL; 807c478bd9Sstevel@tonic-gate } else { 817c478bd9Sstevel@tonic-gate /* we are replacing an old variable */ 827c478bd9Sstevel@tonic-gate environ[which] = change; 837c478bd9Sstevel@tonic-gate } 84*5d54f3d8Smuffin return (0); 857c478bd9Sstevel@tonic-gate } 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate /* find - find where s2 is in environ 887c478bd9Sstevel@tonic-gate * 897c478bd9Sstevel@tonic-gate * input - str = string of form name=value 907c478bd9Sstevel@tonic-gate * 917c478bd9Sstevel@tonic-gate * output - index of name in environ that matches "name" 927c478bd9Sstevel@tonic-gate * -size of table, if none exists 937c478bd9Sstevel@tonic-gate */ 94*5d54f3d8Smuffin static int 95*5d54f3d8Smuffin find(char *str) 967c478bd9Sstevel@tonic-gate { 97*5d54f3d8Smuffin int ct = 0; /* index into environ */ 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate while(environ[ct] != NULL) { 1007c478bd9Sstevel@tonic-gate if (match(environ[ct], str) != 0) 101*5d54f3d8Smuffin return (ct); 1027c478bd9Sstevel@tonic-gate ct++; 1037c478bd9Sstevel@tonic-gate } 104*5d54f3d8Smuffin return (-(++ct)); 1057c478bd9Sstevel@tonic-gate } 1067c478bd9Sstevel@tonic-gate /* 1077c478bd9Sstevel@tonic-gate * s1 is either name, or name=value 1087c478bd9Sstevel@tonic-gate * s2 is name=value 1097c478bd9Sstevel@tonic-gate * if names match, return value of 1, 1107c478bd9Sstevel@tonic-gate * else return 0 1117c478bd9Sstevel@tonic-gate */ 1127c478bd9Sstevel@tonic-gate 113*5d54f3d8Smuffin static int 114*5d54f3d8Smuffin match(char *s1, char *s2) 1157c478bd9Sstevel@tonic-gate { 1167c478bd9Sstevel@tonic-gate while(*s1 == *s2++) { 1177c478bd9Sstevel@tonic-gate if (*s1 == '=') 118*5d54f3d8Smuffin return (1); 1197c478bd9Sstevel@tonic-gate s1++; 1207c478bd9Sstevel@tonic-gate } 121*5d54f3d8Smuffin return (0); 1227c478bd9Sstevel@tonic-gate } 123