1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 23*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 24*7c478bd9Sstevel@tonic-gate 25*7c478bd9Sstevel@tonic-gate 26*7c478bd9Sstevel@tonic-gate /* 27*7c478bd9Sstevel@tonic-gate * Copyright (c) 1999, by Sun Microsystems, Inc. 28*7c478bd9Sstevel@tonic-gate * All rights reserved. 29*7c478bd9Sstevel@tonic-gate */ 30*7c478bd9Sstevel@tonic-gate 31*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 32*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/ 33*7c478bd9Sstevel@tonic-gate 34*7c478bd9Sstevel@tonic-gate /* 35*7c478bd9Sstevel@tonic-gate * NAME 36*7c478bd9Sstevel@tonic-gate * xsetenv, xgetenv, Xgetenv - manage an alternate environment space 37*7c478bd9Sstevel@tonic-gate * 38*7c478bd9Sstevel@tonic-gate * SYNOPSIS 39*7c478bd9Sstevel@tonic-gate * int ret = xsetenv(file) 40*7c478bd9Sstevel@tonic-gate * char *x = xgetenv("FOO"); 41*7c478bd9Sstevel@tonic-gate * char *x = Xgetenv("FOO"); 42*7c478bd9Sstevel@tonic-gate * 43*7c478bd9Sstevel@tonic-gate * DESCRIPTION 44*7c478bd9Sstevel@tonic-gate * xsetenv() reads the given file into an internal buffer 45*7c478bd9Sstevel@tonic-gate * and sets up an alternate environment. 46*7c478bd9Sstevel@tonic-gate * 47*7c478bd9Sstevel@tonic-gate * Return values: 1 - OKAY 48*7c478bd9Sstevel@tonic-gate * 0 - troubles reading the file 49*7c478bd9Sstevel@tonic-gate * -1 - troubles opening the file 50*7c478bd9Sstevel@tonic-gate * 51*7c478bd9Sstevel@tonic-gate * xgetenv() returns the environment value from the 52*7c478bd9Sstevel@tonic-gate * alternate environment. 53*7c478bd9Sstevel@tonic-gate * 54*7c478bd9Sstevel@tonic-gate * Return values: (char *)0 - no value for that variable 55*7c478bd9Sstevel@tonic-gate * pointer - the value 56*7c478bd9Sstevel@tonic-gate * 57*7c478bd9Sstevel@tonic-gate * Xgetenv() returns the environment value from the 58*7c478bd9Sstevel@tonic-gate * alternate environment. 59*7c478bd9Sstevel@tonic-gate * 60*7c478bd9Sstevel@tonic-gate * Return values: "" - no value for that variable 61*7c478bd9Sstevel@tonic-gate * pointer - the value 62*7c478bd9Sstevel@tonic-gate * 63*7c478bd9Sstevel@tonic-gate * LIMITATIONS 64*7c478bd9Sstevel@tonic-gate * Assumes the environment is < 5120 bytes, as in the UNIX 65*7c478bd9Sstevel@tonic-gate * System environment. Assumes < 512 lines in the file. 66*7c478bd9Sstevel@tonic-gate * These values may be adjusted below. 67*7c478bd9Sstevel@tonic-gate */ 68*7c478bd9Sstevel@tonic-gate 69*7c478bd9Sstevel@tonic-gate #include "synonyms.h" 70*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 71*7c478bd9Sstevel@tonic-gate #include "libmail.h" 72*7c478bd9Sstevel@tonic-gate #include <stdio.h> 73*7c478bd9Sstevel@tonic-gate #include <string.h> 74*7c478bd9Sstevel@tonic-gate #include <fcntl.h> 75*7c478bd9Sstevel@tonic-gate #include <ctype.h> 76*7c478bd9Sstevel@tonic-gate 77*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 78*7c478bd9Sstevel@tonic-gate #include <unistd.h> 79*7c478bd9Sstevel@tonic-gate 80*7c478bd9Sstevel@tonic-gate #define MAXVARS 512 81*7c478bd9Sstevel@tonic-gate #define MAXENV 5120 82*7c478bd9Sstevel@tonic-gate 83*7c478bd9Sstevel@tonic-gate static char **xenv = 0; 84*7c478bd9Sstevel@tonic-gate static char *(xenvptrs[MAXVARS]); 85*7c478bd9Sstevel@tonic-gate static char xbuf[MAXENV]; 86*7c478bd9Sstevel@tonic-gate 87*7c478bd9Sstevel@tonic-gate static void reduce(char *); 88*7c478bd9Sstevel@tonic-gate 89*7c478bd9Sstevel@tonic-gate /* 90*7c478bd9Sstevel@tonic-gate * set up an environment buffer 91*7c478bd9Sstevel@tonic-gate * and the pointers into it 92*7c478bd9Sstevel@tonic-gate */ 93*7c478bd9Sstevel@tonic-gate int 94*7c478bd9Sstevel@tonic-gate xsetenv(char *xfile) 95*7c478bd9Sstevel@tonic-gate { 96*7c478bd9Sstevel@tonic-gate int envctr, infd; 97*7c478bd9Sstevel@tonic-gate ssize_t i, nread; 98*7c478bd9Sstevel@tonic-gate 99*7c478bd9Sstevel@tonic-gate /* Open the file */ 100*7c478bd9Sstevel@tonic-gate infd = open(xfile, O_RDONLY); 101*7c478bd9Sstevel@tonic-gate if (infd == -1) { 102*7c478bd9Sstevel@tonic-gate return (-1); 103*7c478bd9Sstevel@tonic-gate } 104*7c478bd9Sstevel@tonic-gate 105*7c478bd9Sstevel@tonic-gate /* Read in the entire file. */ 106*7c478bd9Sstevel@tonic-gate nread = read(infd, xbuf, sizeof (xbuf)); 107*7c478bd9Sstevel@tonic-gate if (nread < 0) { 108*7c478bd9Sstevel@tonic-gate (void) close(infd); 109*7c478bd9Sstevel@tonic-gate return (0); 110*7c478bd9Sstevel@tonic-gate } 111*7c478bd9Sstevel@tonic-gate 112*7c478bd9Sstevel@tonic-gate /* 113*7c478bd9Sstevel@tonic-gate * Set up pointers into the buffer. 114*7c478bd9Sstevel@tonic-gate * Replace \n with \0. 115*7c478bd9Sstevel@tonic-gate * Collapse white space around the = sign and at the 116*7c478bd9Sstevel@tonic-gate * beginning and end of the line. 117*7c478bd9Sstevel@tonic-gate */ 118*7c478bd9Sstevel@tonic-gate xenv = xenvptrs; 119*7c478bd9Sstevel@tonic-gate xenv[0] = &xbuf[0]; 120*7c478bd9Sstevel@tonic-gate for (i = 0, envctr = 0; i < nread; i++) { 121*7c478bd9Sstevel@tonic-gate if (xbuf[i] == '\n') { 122*7c478bd9Sstevel@tonic-gate xbuf[i] = '\0'; 123*7c478bd9Sstevel@tonic-gate reduce(xenv[envctr]); 124*7c478bd9Sstevel@tonic-gate xenv[++envctr] = &xbuf[i+1]; 125*7c478bd9Sstevel@tonic-gate if (envctr == MAXVARS) { 126*7c478bd9Sstevel@tonic-gate break; 127*7c478bd9Sstevel@tonic-gate } 128*7c478bd9Sstevel@tonic-gate } 129*7c478bd9Sstevel@tonic-gate } 130*7c478bd9Sstevel@tonic-gate 131*7c478bd9Sstevel@tonic-gate xenv[envctr] = 0; 132*7c478bd9Sstevel@tonic-gate (void) close(infd); 133*7c478bd9Sstevel@tonic-gate return (1); 134*7c478bd9Sstevel@tonic-gate } 135*7c478bd9Sstevel@tonic-gate 136*7c478bd9Sstevel@tonic-gate /* 137*7c478bd9Sstevel@tonic-gate * Let getenv() do the dirty work 138*7c478bd9Sstevel@tonic-gate * of looking up the variable. We 139*7c478bd9Sstevel@tonic-gate * do this by temporarily resetting 140*7c478bd9Sstevel@tonic-gate * environ to point to the local area. 141*7c478bd9Sstevel@tonic-gate */ 142*7c478bd9Sstevel@tonic-gate char * 143*7c478bd9Sstevel@tonic-gate xgetenv(char *env) 144*7c478bd9Sstevel@tonic-gate { 145*7c478bd9Sstevel@tonic-gate extern char **environ; 146*7c478bd9Sstevel@tonic-gate char *ret, **svenviron = environ; 147*7c478bd9Sstevel@tonic-gate 148*7c478bd9Sstevel@tonic-gate environ = xenv; 149*7c478bd9Sstevel@tonic-gate ret = getenv(env); 150*7c478bd9Sstevel@tonic-gate environ = svenviron; 151*7c478bd9Sstevel@tonic-gate return (ret); 152*7c478bd9Sstevel@tonic-gate } 153*7c478bd9Sstevel@tonic-gate 154*7c478bd9Sstevel@tonic-gate /* 155*7c478bd9Sstevel@tonic-gate * Let xgetenv() do the dirty work 156*7c478bd9Sstevel@tonic-gate * of looking up the variable. 157*7c478bd9Sstevel@tonic-gate */ 158*7c478bd9Sstevel@tonic-gate char * 159*7c478bd9Sstevel@tonic-gate Xgetenv(char *env) 160*7c478bd9Sstevel@tonic-gate { 161*7c478bd9Sstevel@tonic-gate char *ret = xgetenv(env); 162*7c478bd9Sstevel@tonic-gate return (ret ? ret : ""); 163*7c478bd9Sstevel@tonic-gate } 164*7c478bd9Sstevel@tonic-gate 165*7c478bd9Sstevel@tonic-gate /* 166*7c478bd9Sstevel@tonic-gate * Remove the spaces within the environment variable. 167*7c478bd9Sstevel@tonic-gate * The variable can look like this: 168*7c478bd9Sstevel@tonic-gate * 169*7c478bd9Sstevel@tonic-gate * <sp1> variable <sp2> = <sp3> value <sp4> \0 170*7c478bd9Sstevel@tonic-gate * 171*7c478bd9Sstevel@tonic-gate * All spaces can be removed, except within 172*7c478bd9Sstevel@tonic-gate * the variable name and the value. 173*7c478bd9Sstevel@tonic-gate */ 174*7c478bd9Sstevel@tonic-gate 175*7c478bd9Sstevel@tonic-gate static void 176*7c478bd9Sstevel@tonic-gate reduce(char *from) 177*7c478bd9Sstevel@tonic-gate { 178*7c478bd9Sstevel@tonic-gate char *to = from; 179*7c478bd9Sstevel@tonic-gate char *svfrom = from; 180*7c478bd9Sstevel@tonic-gate 181*7c478bd9Sstevel@tonic-gate /* <sp1> */ 182*7c478bd9Sstevel@tonic-gate while (*from &&isspace((int)*from)) 183*7c478bd9Sstevel@tonic-gate from++; 184*7c478bd9Sstevel@tonic-gate 185*7c478bd9Sstevel@tonic-gate /* variable */ 186*7c478bd9Sstevel@tonic-gate while (*from && (*from != '=') && !isspace((int)*from)) 187*7c478bd9Sstevel@tonic-gate *to++ = *from++; 188*7c478bd9Sstevel@tonic-gate 189*7c478bd9Sstevel@tonic-gate /* <sp2> */ 190*7c478bd9Sstevel@tonic-gate while (*from && isspace((int)*from)) 191*7c478bd9Sstevel@tonic-gate from++; 192*7c478bd9Sstevel@tonic-gate 193*7c478bd9Sstevel@tonic-gate /* = */ 194*7c478bd9Sstevel@tonic-gate if (*from == '=') 195*7c478bd9Sstevel@tonic-gate *to++ = *from++; 196*7c478bd9Sstevel@tonic-gate 197*7c478bd9Sstevel@tonic-gate /* <sp3> */ 198*7c478bd9Sstevel@tonic-gate while (*from && isspace((int)*from)) 199*7c478bd9Sstevel@tonic-gate from++; 200*7c478bd9Sstevel@tonic-gate 201*7c478bd9Sstevel@tonic-gate /* value */ 202*7c478bd9Sstevel@tonic-gate while (*from) 203*7c478bd9Sstevel@tonic-gate *to++ = *from++; 204*7c478bd9Sstevel@tonic-gate 205*7c478bd9Sstevel@tonic-gate /* <sp4> */ 206*7c478bd9Sstevel@tonic-gate while ((to > svfrom) && isspace((int)to[-1])) 207*7c478bd9Sstevel@tonic-gate to--; 208*7c478bd9Sstevel@tonic-gate *to = '\0'; 209*7c478bd9Sstevel@tonic-gate } 210