15e9cd1aeSAssar Westerlund /* 25e9cd1aeSAssar Westerlund * Copyright (c) 2000 Kungliga Tekniska H�gskolan 35e9cd1aeSAssar Westerlund * (Royal Institute of Technology, Stockholm, Sweden). 45e9cd1aeSAssar Westerlund * All rights reserved. 55e9cd1aeSAssar Westerlund * 65e9cd1aeSAssar Westerlund * Redistribution and use in source and binary forms, with or without 75e9cd1aeSAssar Westerlund * modification, are permitted provided that the following conditions 85e9cd1aeSAssar Westerlund * are met: 95e9cd1aeSAssar Westerlund * 105e9cd1aeSAssar Westerlund * 1. Redistributions of source code must retain the above copyright 115e9cd1aeSAssar Westerlund * notice, this list of conditions and the following disclaimer. 125e9cd1aeSAssar Westerlund * 135e9cd1aeSAssar Westerlund * 2. Redistributions in binary form must reproduce the above copyright 145e9cd1aeSAssar Westerlund * notice, this list of conditions and the following disclaimer in the 155e9cd1aeSAssar Westerlund * documentation and/or other materials provided with the distribution. 165e9cd1aeSAssar Westerlund * 175e9cd1aeSAssar Westerlund * 3. Neither the name of the Institute nor the names of its contributors 185e9cd1aeSAssar Westerlund * may be used to endorse or promote products derived from this software 195e9cd1aeSAssar Westerlund * without specific prior written permission. 205e9cd1aeSAssar Westerlund * 215e9cd1aeSAssar Westerlund * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 225e9cd1aeSAssar Westerlund * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 235e9cd1aeSAssar Westerlund * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 245e9cd1aeSAssar Westerlund * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 255e9cd1aeSAssar Westerlund * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 265e9cd1aeSAssar Westerlund * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 275e9cd1aeSAssar Westerlund * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 285e9cd1aeSAssar Westerlund * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 295e9cd1aeSAssar Westerlund * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 305e9cd1aeSAssar Westerlund * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 315e9cd1aeSAssar Westerlund * SUCH DAMAGE. 325e9cd1aeSAssar Westerlund */ 335e9cd1aeSAssar Westerlund 345e9cd1aeSAssar Westerlund 355e9cd1aeSAssar Westerlund #ifdef HAVE_CONFIG_H 365e9cd1aeSAssar Westerlund #include <config.h> 375e9cd1aeSAssar Westerlund RCSID("$Id: environment.c,v 1.1 2000/06/21 02:05:03 assar Exp $"); 385e9cd1aeSAssar Westerlund #endif 395e9cd1aeSAssar Westerlund 405e9cd1aeSAssar Westerlund #include <stdio.h> 415e9cd1aeSAssar Westerlund #include <string.h> 425e9cd1aeSAssar Westerlund #include "roken.h" 435e9cd1aeSAssar Westerlund 445e9cd1aeSAssar Westerlund /* 455e9cd1aeSAssar Westerlund * return count of environment assignments from `file' and 465e9cd1aeSAssar Westerlund * list of malloced strings in `env' 475e9cd1aeSAssar Westerlund */ 485e9cd1aeSAssar Westerlund 495e9cd1aeSAssar Westerlund int 505e9cd1aeSAssar Westerlund read_environment(const char *file, char ***env) 515e9cd1aeSAssar Westerlund { 525e9cd1aeSAssar Westerlund int i, k; 535e9cd1aeSAssar Westerlund FILE *F; 545e9cd1aeSAssar Westerlund char **l; 555e9cd1aeSAssar Westerlund char buf[BUFSIZ], *p, *r; 565e9cd1aeSAssar Westerlund 575e9cd1aeSAssar Westerlund if ((F = fopen(file, "r")) == NULL) { 585e9cd1aeSAssar Westerlund return 0; 595e9cd1aeSAssar Westerlund } 605e9cd1aeSAssar Westerlund 615e9cd1aeSAssar Westerlund i = 0; 625e9cd1aeSAssar Westerlund if (*env) { 635e9cd1aeSAssar Westerlund l = *env; 645e9cd1aeSAssar Westerlund while (*l != NULL) { 655e9cd1aeSAssar Westerlund i++; 665e9cd1aeSAssar Westerlund l++; 675e9cd1aeSAssar Westerlund } 685e9cd1aeSAssar Westerlund } 695e9cd1aeSAssar Westerlund l = *env; 705e9cd1aeSAssar Westerlund /* This is somewhat more relaxed on what it accepts then 715e9cd1aeSAssar Westerlund * Wietses sysv_environ from K4 was... 725e9cd1aeSAssar Westerlund */ 735e9cd1aeSAssar Westerlund while (fgets(buf, BUFSIZ, F) != NULL) { 745e9cd1aeSAssar Westerlund if (buf[0] == '#') 755e9cd1aeSAssar Westerlund continue; 765e9cd1aeSAssar Westerlund 775e9cd1aeSAssar Westerlund p = strchr(buf, '#'); 785e9cd1aeSAssar Westerlund if (p != NULL) 795e9cd1aeSAssar Westerlund *p = '\0'; 805e9cd1aeSAssar Westerlund 815e9cd1aeSAssar Westerlund p = buf; 825e9cd1aeSAssar Westerlund while (*p == ' ' || *p == '\t' || *p == '\n') p++; 835e9cd1aeSAssar Westerlund if (*p == '\0') 845e9cd1aeSAssar Westerlund continue; 855e9cd1aeSAssar Westerlund 865e9cd1aeSAssar Westerlund k = strlen(p); 875e9cd1aeSAssar Westerlund if (p[k-1] == '\n') 885e9cd1aeSAssar Westerlund p[k-1] = '\0'; 895e9cd1aeSAssar Westerlund 905e9cd1aeSAssar Westerlund /* Here one should check that is is a 'valid' env string... */ 915e9cd1aeSAssar Westerlund r = strchr(p, '='); 925e9cd1aeSAssar Westerlund if (r == NULL) 935e9cd1aeSAssar Westerlund continue; 945e9cd1aeSAssar Westerlund 955e9cd1aeSAssar Westerlund l = realloc(l, (i+1) * sizeof (char *)); 965e9cd1aeSAssar Westerlund l[i++] = strdup(p); 975e9cd1aeSAssar Westerlund } 985e9cd1aeSAssar Westerlund fclose(F); 995e9cd1aeSAssar Westerlund l = realloc(l, (i+1) * sizeof (char *)); 1005e9cd1aeSAssar Westerlund l[i] = NULL; 1015e9cd1aeSAssar Westerlund *env = l; 1025e9cd1aeSAssar Westerlund return i; 1035e9cd1aeSAssar Westerlund } 104