Annotation of 43BSDReno/contrib/mh/sbr/putenv.c, revision 1.1.1.1

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.