home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / munetd / setenv.c < prev   
Encoding:
C/C++ Source or Header  |  1992-05-08  |  2.9 KB  |  102 lines

  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 permitted
  6.  * provided that: (1) source distributions retain this entire copyright
  7.  * notice and comment, and (2) distributions including binaries display
  8.  * the following acknowledgement:  ``This product includes software
  9.  * developed by the University of California, Berkeley and its contributors''
  10.  * in the documentation or other materials provided with the distribution
  11.  * and in all advertising materials mentioning features or use of this
  12.  * software. Neither the name of the University nor the names of its
  13.  * contributors may be used to endorse or promote products derived
  14.  * from this software without specific prior written permission.
  15.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  16.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  17.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18.  */
  19.  
  20. #if defined(LIBC_SCCS) && !defined(lint)
  21. static char sccsid[] = "@(#)setenv.c    5.4 (Berkeley) 6/1/90";
  22. #endif /* LIBC_SCCS and not lint */
  23.  
  24. #include <stddef.h>
  25. #include <stdlib.h>
  26.  
  27. /*
  28.  * setenv --
  29.  *    Set the value of the environmental variable "name" to be
  30.  *    "value".  If rewrite is set, replace any current value.
  31.  */
  32. setenv(name, value, rewrite)
  33.     register char *name, *value;
  34.     int rewrite;
  35. {
  36.     extern char **environ;
  37.     static int alloced;            /* if allocated space before */
  38.     register char *C;
  39.     int l_value, offset;
  40.     char *_findenv();
  41.  
  42.     if (*value == '=')            /* no `=' in value */
  43.         ++value;
  44.     l_value = strlen(value);
  45.     if ((C = _findenv(name, &offset))) {    /* find if already exists */
  46.         if (!rewrite)
  47.             return(0);
  48.         if (strlen(C) >= l_value) {    /* old larger; copy over */
  49.             while (*C++ = *value++);
  50.             return(0);
  51.         }
  52.     }
  53.     else {                    /* create new slot */
  54.         register int    cnt;
  55.         register char    **P;
  56.  
  57.         for (P = environ, cnt = 0; *P; ++P, ++cnt);
  58.         if (alloced) {            /* just increase size */
  59.             environ = (char **)realloc((char *)environ,
  60.                 (size_t)(sizeof(char *) * (cnt + 2)));
  61.             if (!environ)
  62.                 return(-1);
  63.         }
  64.         else {                /* get new space */
  65.             alloced = 1;        /* copy old entries into it */
  66.             P = (char **)malloc((size_t)(sizeof(char *) *
  67.                 (cnt + 2)));
  68.             if (!P)
  69.                 return(-1);
  70.             bcopy(environ, P, cnt * sizeof(char *));
  71.             environ = P;
  72.         }
  73.         environ[cnt + 1] = NULL;
  74.         offset = cnt;
  75.     }
  76.     for (C = name; *C && *C != '='; ++C);    /* no `=' in name */
  77.     if (!(environ[offset] =            /* name + `=' + value */
  78.         malloc((size_t)((int)(C - name) + l_value + 2))))
  79.         return(-1);
  80.     for (C = environ[offset]; (*C = *name++) && *C != '='; ++C);
  81.     for (*C++ = '='; *C++ = *value++;);
  82.     return(0);
  83. }
  84.  
  85. /*
  86.  * unsetenv(name) --
  87.  *    Delete environmental variable "name".
  88.  */
  89. void
  90. unsetenv(name)
  91.     char    *name;
  92. {
  93.     extern char **environ;
  94.     register char **P;
  95.     int offset;
  96.  
  97.     while (_findenv(name, &offset))        /* if set multiple times */
  98.         for (P = &environ[offset];; ++P)
  99.             if (!(*P = *(P + 1)))
  100.                 break;
  101. }
  102.