putenv.c

来自「<B>Digital的Unix操作系统VAX 4.2源码</B>」· C语言 代码 · 共 136 行

C
136
字号
#ifndef lintstatic char *sccsid = "@(#)putenv.c	4.1      (ULTRIX)        7/2/90";#endif lint/************************************************************************ *									* *			Copyright (c) 1986 by				* *		Digital Equipment Corporation, Maynard, MA		* *			All rights reserved.				* *									* *   This software is furnished under a license and may be used and	* *   copied  only  in accordance with the terms of such license and	* *   with the  inclusion  of  the  above  copyright  notice.   This	* *   software  or  any  other copies thereof may not be provided or	* *   otherwise made available to any other person.  No title to and	* *   ownership of the software is hereby transferred.			* *									* *   This software is  derived  from  software  received  from  the	* *   University    of   California,   Berkeley,   and   from   Bell	* *   Laboratories.  Use, duplication, or disclosure is  subject  to	* *   restrictions  under  license  agreements  with  University  of	* *   California and with AT&T.						* *									* *   The information in this software is subject to change  without	* *   notice  and should not be construed as a commitment by Digital	* *   Equipment Corporation.						* *									* *   Digital assumes no responsibility for the use  or  reliability	* *   of its software on equipment which is not supplied by Digital.	* *									* ************************************************************************//* *	Portions of this software have been licensed to  *	Digital Equipment Company, Maynard, MA. *	Copyright (c) 1987 Sun Microsystems, Inc.  ALL RIGHTS RESERVED. *//* *	Modification History: *  *      10 Nov 89 -- lebel *		Added copyright header. * *//*	LINTLIBRARY	*//*	putenv - change environment variables	input - char *change = a pointer to a string of the form			       "name=value"	output - 0, if successful		 1, otherwise*/#define NULL 0extern char **environ;		/* pointer to enviroment */static reall;		/* flag to reallocate space, if putenv is called				   more than once */intputenv(change)char *change;{	char **newenv;		    /* points to new environment */	register int which;	    /* index of variable to replace */	char *realloc(), *malloc(); /* memory alloc routines */	if ((which = find(change)) < 0)  {		/* if a new variable */		/* which is negative of table size, so invert and		   count new element */		which = (-which) + 1;		if (reall)  {			/* we have expanded environ before */			newenv = (char **)realloc(environ,				  which*sizeof(char *));			if (newenv == NULL)  return -1;			/* now that we have space, change environ */			environ = newenv;		} else {			/* environ points to the original space */			reall++;			newenv = (char **)malloc(which*sizeof(char *));			if (newenv == NULL)  return -1;			(void)memcpy((char *)newenv, (char *)environ, 				(int)(which*sizeof(char *)));			environ = newenv;		}		environ[which-2] = change;		environ[which-1] = NULL;	}  else  {		/* we are replacing an old variable */		environ[which] = change;	}	return 0;}/*	find - find where s2 is in environ * *	input - str = string of form name=value * *	output - index of name in environ that matches "name" *		 -size of table, if none exists*/staticfind(str)register char *str;{	register int ct = 0;	/* index into environ */	while(environ[ct] != NULL)   {		if (match(environ[ct], str)  != 0)			return ct;		ct++;	}	return -(++ct);}/* *	s1 is either name, or name=value *	s2 is name=value *	if names match, return value of 1, *	else return 0 */staticmatch(s1, s2)register char *s1, *s2;{	while(*s1 == *s2++)  {		if (*s1 == '=')			return 1;		s1++;	}	return 0;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?