📄 setenv.c
字号:
/* * Copyright (c) 1987 Regents of the University of California. * All rights reserved. The Berkeley software License Agreement * specifies the terms and conditions for redistribution. */#ifndef lintstatic char *sccsid = "@(#)setenv.c 4.1 (ULTRIX) 7/3/90";#endif lint/************************************************************************ * * * Copyright (c) 1984,86 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. * * * ************************************************************************//************************************************************************ * Modification History * ************************************************************************//* * Jon Reeves, 1989-Sept-15 * Changed environ to __environ for ANSI compliance */#include <sys/types.h>#include <stdio.h>/* * setenv(name,value,rewrite) * Set the value of the environmental variable "name" to be * "value". If rewrite is set, replace any current value. */setenv(name,value,rewrite) register char *name, *value; int rewrite;{ extern char **__environ; static int alloced; /* if allocated space before */ register char *C; int l_value, offset; char *malloc(), *realloc(), *_findenv(); if (*value == '=') /* no `=' in value */ ++value; l_value = strlen(value); if ((C = _findenv(name,&offset))) { /* find if already exists */ if (!rewrite) return(0); if (strlen(C) >= l_value) { /* old larger; copy over */ while (*C++ = *value++); return(0); } } else { /* create new slot */ register int cnt; register char **P; for (P = __environ,cnt = 0;*P;++P,++cnt); if (alloced) { /* just increase size */ __environ = (char **)realloc((char *)__environ, (u_int)(sizeof(char *) * (cnt + 2))); if (!__environ) return(-1); } else { /* get new space */ alloced = 1; /* copy old entries into it */ P = (char **)malloc((u_int)(sizeof(char *) * (cnt + 2))); if (!P) return(-1); bcopy(__environ,P,cnt * sizeof(char *)); __environ = P; } __environ[cnt + 1] = NULL; offset = cnt; } for (C = name;*C && *C != '=';++C); /* no `=' in name */ if (!(__environ[offset] = /* name + `=' + value */ malloc((u_int)((int)(C - name) + l_value + 2)))) return(-1); for (C = __environ[offset];(*C = *name++) && *C != '=';++C); for (*C++ = '=';*C++ = *value++;); return(0);}/* * unsetenv(name) -- * Delete environmental variable "name". */voidunsetenv(name) char *name;{ extern char **__environ; register char **P; int offset; while (_findenv(name,&offset)) /* if set multiple times */ for (P = &__environ[offset];;++P) if (!(*P = *(P + 1))) break;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -