1: /*
   2:  * Copyright (c) 1987 Regents of the University of California.
   3:  * All rights reserved.  The Berkeley software License Agreement
   4:  * specifies the terms and conditions for redistribution.
   5:  */
   6: 
   7: #if defined(LIBC_SCCS) && !defined(lint)
   8: static char sccsid[] = "@(#)setenv.c	1.3 (Berkeley) 6/16/87";
   9: #endif LIBC_SCCS and not lint
  10: 
  11: #include <sys/types.h>
  12: #include <stdio.h>
  13: 
  14: /*
  15:  * setenv(name,value,rewrite)
  16:  *	Set the value of the environmental variable "name" to be
  17:  *	"value".  If rewrite is set, replace any current value.
  18:  */
  19: setenv(name,value,rewrite)
  20:     register char   *name,
  21:             *value;
  22:     int rewrite;
  23: {
  24:     extern char **environ;
  25:     static int  alloced;        /* if allocated space before */
  26:     register char   *C;
  27:     int l_value,
  28:         offset;
  29:     char    *malloc(), *realloc(), *_findenv();
  30: 
  31:     if (*value == '=')          /* no `=' in value */
  32:         ++value;
  33:     l_value = strlen(value);
  34:     if ((C = _findenv(name,&offset))) { /* find if already exists */
  35:         if (!rewrite)
  36:             return(0);
  37:         if (strlen(C) >= l_value) { /* old larger; copy over */
  38:             while (*C++ = *value++);
  39:             return(0);
  40:         }
  41:     }
  42:     else {                  /* create new slot */
  43:         register int    cnt;
  44:         register char   **P;
  45: 
  46:         for (P = environ,cnt = 0;*P;++P,++cnt);
  47:         if (alloced) {          /* just increase size */
  48:             environ = (char **)realloc((char *)environ,
  49:                 (u_int)(sizeof(char *) * (cnt + 2)));
  50:             if (!environ)
  51:                 return(-1);
  52:         }
  53:         else {              /* get new space */
  54:             alloced = 1;        /* copy old entries into it */
  55:             P = (char **)malloc((u_int)(sizeof(char *) *
  56:                 (cnt + 2)));
  57:             if (!P)
  58:                 return(-1);
  59:             bcopy(environ,P,cnt * sizeof(char *));
  60:             environ = P;
  61:         }
  62:         environ[cnt + 1] = NULL;
  63:         offset = cnt;
  64:     }
  65:     for (C = name;*C && *C != '=';++C); /* no `=' in name */
  66:     if (!(environ[offset] =         /* name + `=' + value */
  67:         malloc((u_int)((int)(C - name) + l_value + 2))))
  68:         return(-1);
  69:     for (C = environ[offset];(*C = *name++) && *C != '=';++C);
  70:     for (*C++ = '=';*C++ = *value++;);
  71:     return(0);
  72: }
  73: 
  74: /*
  75:  * unsetenv(name) --
  76:  *	Delete environmental variable "name".
  77:  */
  78: void
  79: unsetenv(name)
  80:     char    *name;
  81: {
  82:     extern  char    **environ;
  83:     register char   **P;
  84:     int offset;
  85: 
  86:     while (_findenv(name,&offset))      /* if set multiple times */
  87:         for (P = &environ[offset];;++P)
  88:             if (!(*P = *(P + 1)))
  89:                 break;
  90: }

Defined functions

Defined variables

sccsid defined in line 8; never used
Last modified: 1987-07-20
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 2325
Valid CSS Valid XHTML 1.0 Strict