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