Annotation of embedaddon/sudo/compat/setenv.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
        !             3:  *
        !             4:  * Permission to use, copy, modify, and distribute this software for any
        !             5:  * purpose with or without fee is hereby granted, provided that the above
        !             6:  * copyright notice and this permission notice appear in all copies.
        !             7:  *
        !             8:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
        !             9:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
        !            10:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
        !            11:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
        !            12:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
        !            13:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
        !            14:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
        !            15:  */
        !            16: 
        !            17: #include <config.h>
        !            18: 
        !            19: #include <sys/types.h>
        !            20: 
        !            21: #include <stdio.h>
        !            22: #ifdef STDC_HEADERS
        !            23: # include <stdlib.h>
        !            24: # include <stddef.h>
        !            25: #else
        !            26: # ifdef HAVE_STDLIB_H
        !            27: #  include <stdlib.h>
        !            28: # endif
        !            29: #endif /* STDC_HEADERS */
        !            30: #ifdef HAVE_STRING_H
        !            31: # include <string.h>
        !            32: #endif /* HAVE_STRING_H */
        !            33: #ifdef HAVE_STRINGS_H
        !            34: # include <strings.h>
        !            35: #endif /* HAVE_STRINGS_H */
        !            36: #if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
        !            37: # include <malloc.h>
        !            38: #endif /* HAVE_MALLOC_H && !STDC_HEADERS */
        !            39: #include <errno.h>
        !            40: 
        !            41: #include "missing.h"
        !            42: 
        !            43: int
        !            44: setenv(const char *var, const char *val, int overwrite)
        !            45: {
        !            46:     char *envstr, *dst;
        !            47:     const char *src;
        !            48:     size_t esize;
        !            49: 
        !            50:     if (!var || *var == '\0') {
        !            51:        errno = EINVAL;
        !            52:        return -1;
        !            53:     }
        !            54: 
        !            55:     /*
        !            56:      * POSIX says a var name with '=' is an error but BSD
        !            57:      * just ignores the '=' and anything after it.
        !            58:      */
        !            59:     for (src = var; *src != '\0' && *src != '='; src++)
        !            60:        ;
        !            61:     esize = (size_t)(src - var) + 2;
        !            62:     if (val) {
        !            63:         esize += strlen(val);  /* glibc treats a NULL val as "" */
        !            64:     }
        !            65: 
        !            66:     /* Allocate and fill in envstr. */
        !            67:     if ((envstr = malloc(esize)) == NULL)
        !            68:        return -1;
        !            69:     for (src = var, dst = envstr; *src != '\0' && *src != '=';)
        !            70:        *dst++ = *src++;
        !            71:     *dst++ = '=';
        !            72:     if (val) {
        !            73:        for (src = val; *src != '\0';)
        !            74:            *dst++ = *src++;
        !            75:     }
        !            76:     *dst = '\0';
        !            77: 
        !            78:     if (!overwrite && getenv(var) != NULL) {
        !            79:        free(envstr);
        !            80:        return 0;
        !            81:     }
        !            82:     return putenv(envstr);
        !            83: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>