1: /* putenv.c - (un) set an envariable */
   2: 
   3: #include "../h/mh.h"
   4: #include <stdio.h>
   5: 
   6: 
   7: extern  char **environ;
   8: 
   9: 
  10: int     putenv (name, value)
  11: register char  *name,
  12:                *value;
  13: {
  14:     register int    i;
  15:     register char **ep,
  16:                   **nep,
  17:                    *cp;
  18: 
  19:     if ((cp = malloc ((unsigned) (strlen (name) + strlen (value) + 2)))
  20:         == NULL)
  21:     return 1;
  22:     (void) sprintf (cp, "%s=%s", name, value);
  23: 
  24:     for (ep = environ, i = 0; *ep; ep++, i++)
  25:     if (nvmatch (name, *ep)) {
  26:         *ep = cp;
  27:         return 0;
  28:     }
  29: 
  30:     if ((nep = (char **) malloc ((unsigned) ((i + 2) * sizeof *nep))) == NULL)
  31:     return 1;
  32:     for (ep = environ, i = 0; *ep; nep[i++] = *ep++)
  33:     continue;
  34:     nep[i++] = cp;
  35:     nep[i] = NULL;
  36:     environ = nep;
  37:     return 0;
  38: }
  39: 
  40: 
  41: int unputenv (name)
  42: char   *name;
  43: {
  44:     char  **ep,
  45:           **nep;
  46: 
  47:     for (ep = environ; *ep; ep++)
  48:     if (nvmatch (name, *ep))
  49:         break;
  50:     if (*ep == NULL)
  51:     return 1;
  52: 
  53:     for (nep = ep + 1; *nep; nep++)
  54:     continue;
  55:     *ep = *--nep;
  56:     *nep = NULL;
  57:     return 0;
  58: }
  59: 
  60: /*  */
  61: 
  62: static nvmatch (s1, s2)
  63: register char  *s1,
  64:                *s2;
  65: {
  66:     while (*s1 == *s2++)
  67:     if (*s1++ == '=')
  68:         return 1;
  69: 
  70:     return (*s1 == '\0' && *--s2 == '=');
  71: }

Defined functions

nvmatch defined in line 62; used 2 times
putenv defined in line 10; used 13 times
unputenv defined in line 41; used 7 times
Last modified: 1985-09-08
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1042
Valid CSS Valid XHTML 1.0 Strict