environment.c revision 72445
172445Sassar/* 272445Sassar * Copyright (c) 2000 Kungliga Tekniska H�gskolan 372445Sassar * (Royal Institute of Technology, Stockholm, Sweden). 472445Sassar * All rights reserved. 572445Sassar * 672445Sassar * Redistribution and use in source and binary forms, with or without 772445Sassar * modification, are permitted provided that the following conditions 872445Sassar * are met: 972445Sassar * 1072445Sassar * 1. Redistributions of source code must retain the above copyright 1172445Sassar * notice, this list of conditions and the following disclaimer. 1272445Sassar * 1372445Sassar * 2. Redistributions in binary form must reproduce the above copyright 1472445Sassar * notice, this list of conditions and the following disclaimer in the 1572445Sassar * documentation and/or other materials provided with the distribution. 1672445Sassar * 1772445Sassar * 3. Neither the name of the Institute nor the names of its contributors 1872445Sassar * may be used to endorse or promote products derived from this software 1972445Sassar * without specific prior written permission. 2072445Sassar * 2172445Sassar * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 2272445Sassar * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2372445Sassar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2472445Sassar * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 2572445Sassar * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2672445Sassar * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2772445Sassar * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2872445Sassar * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2972445Sassar * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3072445Sassar * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3172445Sassar * SUCH DAMAGE. 3272445Sassar */ 3372445Sassar 3472445Sassar 3572445Sassar#ifdef HAVE_CONFIG_H 3672445Sassar#include <config.h> 3772445SassarRCSID("$Id: environment.c,v 1.1 2000/06/21 02:05:03 assar Exp $"); 3872445Sassar#endif 3972445Sassar 4072445Sassar#include <stdio.h> 4172445Sassar#include <string.h> 4272445Sassar#include "roken.h" 4372445Sassar 4472445Sassar/* 4572445Sassar * return count of environment assignments from `file' and 4672445Sassar * list of malloced strings in `env' 4772445Sassar */ 4872445Sassar 4972445Sassarint 5072445Sassarread_environment(const char *file, char ***env) 5172445Sassar{ 5272445Sassar int i, k; 5372445Sassar FILE *F; 5472445Sassar char **l; 5572445Sassar char buf[BUFSIZ], *p, *r; 5672445Sassar 5772445Sassar if ((F = fopen(file, "r")) == NULL) { 5872445Sassar return 0; 5972445Sassar } 6072445Sassar 6172445Sassar i = 0; 6272445Sassar if (*env) { 6372445Sassar l = *env; 6472445Sassar while (*l != NULL) { 6572445Sassar i++; 6672445Sassar l++; 6772445Sassar } 6872445Sassar } 6972445Sassar l = *env; 7072445Sassar /* This is somewhat more relaxed on what it accepts then 7172445Sassar * Wietses sysv_environ from K4 was... 7272445Sassar */ 7372445Sassar while (fgets(buf, BUFSIZ, F) != NULL) { 7472445Sassar if (buf[0] == '#') 7572445Sassar continue; 7672445Sassar 7772445Sassar p = strchr(buf, '#'); 7872445Sassar if (p != NULL) 7972445Sassar *p = '\0'; 8072445Sassar 8172445Sassar p = buf; 8272445Sassar while (*p == ' ' || *p == '\t' || *p == '\n') p++; 8372445Sassar if (*p == '\0') 8472445Sassar continue; 8572445Sassar 8672445Sassar k = strlen(p); 8772445Sassar if (p[k-1] == '\n') 8872445Sassar p[k-1] = '\0'; 8972445Sassar 9072445Sassar /* Here one should check that is is a 'valid' env string... */ 9172445Sassar r = strchr(p, '='); 9272445Sassar if (r == NULL) 9372445Sassar continue; 9472445Sassar 9572445Sassar l = realloc(l, (i+1) * sizeof (char *)); 9672445Sassar l[i++] = strdup(p); 9772445Sassar } 9872445Sassar fclose(F); 9972445Sassar l = realloc(l, (i+1) * sizeof (char *)); 10072445Sassar l[i] = NULL; 10172445Sassar *env = l; 10272445Sassar return i; 10372445Sassar} 104