Path: blob/main/crypto/krb5/src/clients/ksu/setenv.c
34907 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/*2* Copyright (c) 1987 Regents of the University of California.3* All rights reserved.4*5* Redistribution and use in source and binary forms are permitted6* provided that the above copyright notice and this paragraph are7* duplicated in all such forms and that any documentation,8* advertising materials, and other materials related to such9* distribution and use acknowledge that the software was developed10* by the University of California, Berkeley. The name of the11* University may not be used to endorse or promote products derived12* from this software without specific prior written permission.13* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR14* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED15* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.16*/17/*18* Copyright (c) 1987 Regents of the University of California.19* All rights reserved.20*21* Redistribution and use in source and binary forms are permitted22* provided that the above copyright notice and this paragraph are23* duplicated in all such forms and that any documentation,24* advertising materials, and other materials related to such25* distribution and use acknowledge that the software was developed26* by the University of California, Berkeley. The name of the27* University may not be used to endorse or promote products derived28* from this software without specific prior written permission.29* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR30* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED31* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.32*/3334/* based on @(#)setenv.c 5.2 (Berkeley) 6/27/88 */3536#include "autoconf.h"37#include <sys/types.h>38#include <stdio.h>39#include <stdlib.h>40#include <string.h>4142static char *_findenv(char *, int *);4344#ifndef HAVE_SETENV45extern int setenv(char *, char *, int);46#endif47#ifndef HAVE_UNSETENV48extern void unsetenv(char *);49#endif5051/*52* setenv --53* Set the value of the environmental variable "name" to be54* "value". If rewrite is set, replace any current value.55*/56#ifndef HAVE_SETENV57int58setenv(name, value, rewrite)59char *name, *value;60int rewrite;61{62extern char **environ;63static int alloced; /* if allocated space before */64char *C;65int l_value, offset;6667if (*value == '=') /* no `=' in value */68++value;69l_value = strlen(value);70if ((C = _findenv(name, &offset))) { /* find if already exists */71if (!rewrite)72return(0);73if (strlen(C) >= l_value) { /* old larger; copy over */74while ((*C++ = *value++));75return(0);76}77}78else { /* create new slot */79int cnt;80char **P;8182for (P = environ, cnt = 0; *P; ++P, ++cnt);83if (alloced) { /* just increase size */84environ = (char **)realloc((char *)environ,85(u_int)(sizeof(char *) * (cnt + 2)));86if (!environ)87return(-1);88}89else { /* get new space */90alloced = 1; /* copy old entries into it */91P = (char **)malloc((u_int)(sizeof(char *) *92(cnt + 2)));93if (!P)94return(-1);95memcpy(P, environ, cnt * sizeof(char *));96environ = P;97}98environ[cnt + 1] = NULL;99offset = cnt;100}101for (C = name; *C && *C != '='; ++C); /* no `=' in name */102if (!(environ[offset] = /* name + `=' + value */103malloc((u_int)((int)(C - name) + l_value + 2))))104return(-1);105for (C = environ[offset]; (*C = *name++) &&( *C != '='); ++C);106for (*C++ = '='; (*C++ = *value++) != NULL;);107return(0);108}109#endif110111/*112* unsetenv(name) --113* Delete environmental variable "name".114*/115#ifndef HAVE_UNSETENV116void117unsetenv(name)118char *name;119{120extern char **environ;121char **P;122int offset;123124while (_findenv(name, &offset)) /* if set multiple times */125for (P = &environ[offset];; ++P)126if (!(*P = *(P + 1)))127break;128}129#endif130131/* based on @(#)getenv.c 5.5 (Berkeley) 6/27/88 */132133/*134* getenv --135* Returns ptr to value associated with name, if any, else NULL.136*/137#ifndef HAVE_GETENV138char *139getenv(name)140char *name;141{142int offset;143144return(_findenv(name, &offset));145}146#endif147148/*149* _findenv --150* Returns pointer to value associated with name, if any, else NULL.151* Sets offset to be the offset of the name/value combination in the152* environmental array, for use by setenv(3) and unsetenv(3).153* Explicitly removes '=' in argument name.154*155*/156static char *157_findenv(name, offset)158char *name;159int *offset;160{161extern char **environ;162int len;163char **P, *C;164165for (C = name, len = 0; *C && *C != '='; ++C, ++len);166for (P = environ; *P; ++P)167if (!strncmp(*P, name, len))168if (*(C = *P + len) == '=') {169*offset = P - environ;170return(++C);171}172return(NULL);173}174175176