[BACK]Return to setenv.c CVS log [TXT][DIR] Up to [local] / prex-old / usr / lib / libc / stdlib

Annotation of prex-old/usr/lib/libc/stdlib/setenv.c, Revision 1.1

1.1     ! nbrk        1: /*
        !             2:  * Copyright (c) 1987, 1993
        !             3:  *     The Regents of the University of California.  All rights reserved.
        !             4:  *
        !             5:  * Redistribution and use in source and binary forms, with or without
        !             6:  * modification, are permitted provided that the following conditions
        !             7:  * are met:
        !             8:  * 1. Redistributions of source code must retain the above copyright
        !             9:  *    notice, this list of conditions and the following disclaimer.
        !            10:  * 2. Redistributions in binary form must reproduce the above copyright
        !            11:  *    notice, this list of conditions and the following disclaimer in the
        !            12:  *    documentation and/or other materials provided with the distribution.
        !            13:  * 3. Neither the name of the University nor the names of its contributors
        !            14:  *    may be used to endorse or promote products derived from this software
        !            15:  *    without specific prior written permission.
        !            16:  *
        !            17:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
        !            18:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        !            19:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        !            20:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
        !            21:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        !            22:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
        !            23:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
        !            24:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
        !            25:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
        !            26:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
        !            27:  * SUCH DAMAGE.
        !            28:  */
        !            29:
        !            30: #include <stddef.h>
        !            31: #include <stdlib.h>
        !            32: #include <string.h>
        !            33:
        !            34: extern char **environ;
        !            35:
        !            36: char *__findenv(const char *, int *);
        !            37:
        !            38: /*
        !            39:  * setenv --
        !            40:  *     Set the value of the environmental variable "name" to be
        !            41:  *     "value".  If rewrite is set, replace any current value.
        !            42:  */
        !            43: int
        !            44: setenv(name, value, rewrite)
        !            45:        const char *name;
        !            46:        const char *value;
        !            47:        int rewrite;
        !            48: {
        !            49:        static int alloced;                     /* if allocated space before */
        !            50:        char *c;
        !            51:        int l_value, offset;
        !            52:
        !            53:        if (*value == '=')                      /* no `=' in value */
        !            54:                ++value;
        !            55:        l_value = strlen(value);
        !            56:        if ((c = __findenv(name, &offset))) {   /* find if already exists */
        !            57:                if (!rewrite)
        !            58:                        return (0);
        !            59:                if ((int)strlen(c) >= l_value) {        /* old larger; copy over */
        !            60:                        while ( (*c++ = *value++) );
        !            61:                        return (0);
        !            62:                }
        !            63:        } else {                                        /* create new slot */
        !            64:                int cnt;
        !            65:                char **p;
        !            66:
        !            67:                for (p = environ, cnt = 0; *p; ++p, ++cnt);
        !            68:                if (alloced) {                  /* just increase size */
        !            69:                        environ = (char **)realloc((char *)environ,
        !            70:                            (size_t)(sizeof(char *) * (cnt + 2)));
        !            71:                        if (!environ)
        !            72:                                return (-1);
        !            73:                }
        !            74:                else {                          /* get new space */
        !            75:                        alloced = 1;            /* copy old entries into it */
        !            76:                        p = malloc((size_t)(sizeof(char *) * (cnt + 2)));
        !            77:                        if (!p)
        !            78:                                return (-1);
        !            79:                        bcopy(environ, p, cnt * sizeof(char *));
        !            80:                        environ = p;
        !            81:                }
        !            82:                environ[cnt + 1] = NULL;
        !            83:                offset = cnt;
        !            84:        }
        !            85:        for (c = (char *)name; *c && *c != '='; ++c);   /* no `=' in name */
        !            86:        if (!(environ[offset] =                 /* name + `=' + value */
        !            87:            malloc((size_t)((int)(c - name) + l_value + 2))))
        !            88:                return (-1);
        !            89:        for (c = environ[offset]; (*c = *name++) && *c != '='; ++c);
        !            90:        for (*c++ = '='; (*c++ = *value++) != 0;);
        !            91:        return (0);
        !            92: }
        !            93:
        !            94: /*
        !            95:  * unsetenv(name) --
        !            96:  *     Delete environmental variable "name".
        !            97:  */
        !            98: void
        !            99: unsetenv(name)
        !           100:        const char *name;
        !           101: {
        !           102:        char **p;
        !           103:        int offset;
        !           104:
        !           105:        while (__findenv(name, &offset))        /* if set multiple times */
        !           106:                for (p = &environ[offset];; ++p)
        !           107:                        if (!(*p = *(p + 1)))
        !           108:                                break;
        !           109: }

CVSweb