/*1* Copyright (c) 2000 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include "login_locl.h"34RCSID("$Id$");3536/*37* the environment we will send to execle and the shell.38*/3940char **env;41int num_env;4243void44extend_env(char *str)45{46env = realloc(env, (num_env + 1) * sizeof(*env));47if(env == NULL)48errx(1, "Out of memory!");49env[num_env++] = str;50}5152void53add_env(const char *var, const char *value)54{55int i;56char *str;57asprintf(&str, "%s=%s", var, value);58if(str == NULL)59errx(1, "Out of memory!");60for(i = 0; i < num_env; i++)61if(strncmp(env[i], var, strlen(var)) == 0 &&62env[i][strlen(var)] == '='){63free(env[i]);64env[i] = str;65return;66}6768extend_env(str);69}7071#if !HAVE_DECL_ENVIRON72extern char **environ;73#endif747576void77copy_env(void)78{79char **p;80for(p = environ; *p; p++)81extend_env(*p);82}8384void85login_read_env(const char *file)86{87char **newenv;88char *p;89int i, j;9091newenv = NULL;92i = read_environment(file, &newenv);93for (j = 0; j < i; j++) {94p = strchr(newenv[j], '=');95if (p == NULL)96errx(1, "%s: missing = in string %s",97file, newenv[j]);98*p++ = 0;99add_env(newenv[j], p);100*--p = '=';101free(newenv[j]);102}103free(newenv);104}105106107