Annotation of coherent/b/lib/libc/gen/putenv.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Change or add value to environment.
                      3:  *
                      4:  * $Log:       putenv.c,v $
                      5:  * Revision 1.3  93/02/10  16:53:40  bin
                      6:  * *** empty log message ***
                      7:  * 
                      8:  * 87/02/05    Allan Cornish
                      9:  * Initial revision.
                     10:  */
                     11: #include <stdio.h>
                     12: #include <errno.h>
                     13: 
                     14: /**
                     15:  *
                     16:  * int
                     17:  * putenv( s )         - change or add value to environment
                     18:  * char * s;
                     19:  *
                     20:  *     Input:  s = pointer to string of the form 'NAME=value'
                     21:  *
                     22:  *     Action: The function putenv makes the value of the environment
                     23:  *             variable 'name' equal to 'value' by altering an existing
                     24:  *             variable or creating a new one.  In either case, the string
                     25:  *             created by 'string' becomes part of the environment,
                     26:  *             so altering the string will change the environment.
                     27:  *             The space used by string is no longer used once a new
                     28:  *             string-defining name is passed to the function putenv.
                     29:  *
                     30:  *     Return: 0 = environment updated.
                     31:  *             * = insufficient memory, or invalid argument.
                     32:  *
                     33:  *     Notes:  The third argument to main [envp] is not changed.
                     34:  */
                     35: 
                     36: int
                     37: putenv( string )
                     38: char * string;
                     39: {
                     40:        register char **epp;
                     41:        register int    len;
                     42:        static char ** lastenv;
                     43:        extern char ** environ;
                     44: 
                     45:        /*
                     46:         * Paranoia.
                     47:         */
                     48:        if ( string == NULL ) {
                     49:                errno = EFAULT;
                     50:                return -1;
                     51:        }
                     52: 
                     53:        /*
                     54:         * Validate string, which must be of form NAME=value.
                     55:         */
                     56:        for ( len = 0; string[len] != '='; len++ ) {
                     57:                if ( string[len] == '\0' ) {
                     58:                        errno = EINVAL;
                     59:                        return -1;
                     60:                }
                     61:        }
                     62: 
                     63:        /*
                     64:         * Update len to include the '='.
                     65:         */
                     66:        len++;
                     67: 
                     68:        /*
                     69:         * Search for existing value.
                     70:         */
                     71:        for ( epp = environ; *epp != NULL; epp++ ) {
                     72: 
                     73:                /*
                     74:                 * Variable already in environment.
                     75:                 */
                     76:                if ( strncmp( string, *epp, len ) == 0 ) {
                     77: 
                     78:                        /*
                     79:                         * Update environment.
                     80:                         * NOTE: should release previous value if malloc'ed.
                     81:                         */
                     82:                        *epp = string;
                     83:                        return 0;
                     84:                }
                     85:        }
                     86: 
                     87:        /*
                     88:         * Allocate new environment array.
                     89:         */
                     90:        len = (epp - environ + 2) * sizeof(*epp);
                     91:        if ( (epp = malloc(len)) == NULL ) {
                     92:                errno = ENOMEM;
                     93:                return -1;
                     94:        }
                     95: 
                     96:        /*
                     97:         * Copy old environment to new environment.
                     98:         */
                     99:        len = 0;
                    100:        while ( epp[len] = environ[len] )
                    101:                len++;
                    102: 
                    103:        /*
                    104:         * Append new variable and NULL terminator.
                    105:         */
                    106:        epp[len++] = string;
                    107:        epp[len++] = NULL;
                    108: 
                    109:        /*
                    110:         * Release last malloc'ed environment array.
                    111:         */
                    112:        if ( lastenv != NULL )
                    113:                free( lastenv );
                    114: 
                    115:        /*
                    116:         * Install new environment.
                    117:         */
                    118:        environ = epp;
                    119:        lastenv = epp;
                    120: 
                    121:        return 0;
                    122: }

unix.superglobalmegacorp.com

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