10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*6812Sraf * Common Development and Distribution License (the "License"). 6*6812Sraf * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 211219Sraf 22*6812Sraf /* 23*6812Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24*6812Sraf * Use is subject to license terms. 25*6812Sraf */ 26*6812Sraf 270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 280Sstevel@tonic-gate /* All Rights Reserved */ 290Sstevel@tonic-gate 300Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 310Sstevel@tonic-gate 320Sstevel@tonic-gate /* 330Sstevel@tonic-gate * NAME 340Sstevel@tonic-gate * xsetenv, xgetenv, Xgetenv - manage an alternate environment space 350Sstevel@tonic-gate * 360Sstevel@tonic-gate * SYNOPSIS 370Sstevel@tonic-gate * int ret = xsetenv(file) 380Sstevel@tonic-gate * char *x = xgetenv("FOO"); 390Sstevel@tonic-gate * char *x = Xgetenv("FOO"); 400Sstevel@tonic-gate * 410Sstevel@tonic-gate * DESCRIPTION 420Sstevel@tonic-gate * xsetenv() reads the given file into an internal buffer 430Sstevel@tonic-gate * and sets up an alternate environment. 440Sstevel@tonic-gate * 450Sstevel@tonic-gate * Return values: 1 - OKAY 460Sstevel@tonic-gate * 0 - troubles reading the file 470Sstevel@tonic-gate * -1 - troubles opening the file 480Sstevel@tonic-gate * 490Sstevel@tonic-gate * xgetenv() returns the environment value from the 500Sstevel@tonic-gate * alternate environment. 510Sstevel@tonic-gate * 520Sstevel@tonic-gate * Return values: (char *)0 - no value for that variable 530Sstevel@tonic-gate * pointer - the value 540Sstevel@tonic-gate * 550Sstevel@tonic-gate * Xgetenv() returns the environment value from the 560Sstevel@tonic-gate * alternate environment. 570Sstevel@tonic-gate * 580Sstevel@tonic-gate * Return values: "" - no value for that variable 590Sstevel@tonic-gate * pointer - the value 600Sstevel@tonic-gate * 610Sstevel@tonic-gate * LIMITATIONS 620Sstevel@tonic-gate * Assumes the environment is < 5120 bytes, as in the UNIX 630Sstevel@tonic-gate * System environment. Assumes < 512 lines in the file. 640Sstevel@tonic-gate * These values may be adjusted below. 650Sstevel@tonic-gate */ 660Sstevel@tonic-gate 670Sstevel@tonic-gate #include <sys/types.h> 680Sstevel@tonic-gate #include "libmail.h" 690Sstevel@tonic-gate #include <stdio.h> 700Sstevel@tonic-gate #include <string.h> 710Sstevel@tonic-gate #include <fcntl.h> 720Sstevel@tonic-gate #include <ctype.h> 730Sstevel@tonic-gate 740Sstevel@tonic-gate #include <stdlib.h> 750Sstevel@tonic-gate #include <unistd.h> 760Sstevel@tonic-gate 770Sstevel@tonic-gate #define MAXVARS 512 780Sstevel@tonic-gate #define MAXENV 5120 790Sstevel@tonic-gate 800Sstevel@tonic-gate static char **xenv = 0; 810Sstevel@tonic-gate static char *(xenvptrs[MAXVARS]); 820Sstevel@tonic-gate static char xbuf[MAXENV]; 830Sstevel@tonic-gate 840Sstevel@tonic-gate static void reduce(char *); 850Sstevel@tonic-gate 860Sstevel@tonic-gate /* 870Sstevel@tonic-gate * set up an environment buffer 880Sstevel@tonic-gate * and the pointers into it 890Sstevel@tonic-gate */ 900Sstevel@tonic-gate int 910Sstevel@tonic-gate xsetenv(char *xfile) 920Sstevel@tonic-gate { 930Sstevel@tonic-gate int envctr, infd; 940Sstevel@tonic-gate ssize_t i, nread; 950Sstevel@tonic-gate 960Sstevel@tonic-gate /* Open the file */ 970Sstevel@tonic-gate infd = open(xfile, O_RDONLY); 980Sstevel@tonic-gate if (infd == -1) { 990Sstevel@tonic-gate return (-1); 1000Sstevel@tonic-gate } 1010Sstevel@tonic-gate 1020Sstevel@tonic-gate /* Read in the entire file. */ 1030Sstevel@tonic-gate nread = read(infd, xbuf, sizeof (xbuf)); 1040Sstevel@tonic-gate if (nread < 0) { 1050Sstevel@tonic-gate (void) close(infd); 1060Sstevel@tonic-gate return (0); 1070Sstevel@tonic-gate } 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate /* 1100Sstevel@tonic-gate * Set up pointers into the buffer. 1110Sstevel@tonic-gate * Replace \n with \0. 1120Sstevel@tonic-gate * Collapse white space around the = sign and at the 1130Sstevel@tonic-gate * beginning and end of the line. 1140Sstevel@tonic-gate */ 1150Sstevel@tonic-gate xenv = xenvptrs; 1160Sstevel@tonic-gate xenv[0] = &xbuf[0]; 1170Sstevel@tonic-gate for (i = 0, envctr = 0; i < nread; i++) { 1180Sstevel@tonic-gate if (xbuf[i] == '\n') { 1190Sstevel@tonic-gate xbuf[i] = '\0'; 1200Sstevel@tonic-gate reduce(xenv[envctr]); 1210Sstevel@tonic-gate xenv[++envctr] = &xbuf[i+1]; 1220Sstevel@tonic-gate if (envctr == MAXVARS) { 1230Sstevel@tonic-gate break; 1240Sstevel@tonic-gate } 1250Sstevel@tonic-gate } 1260Sstevel@tonic-gate } 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate xenv[envctr] = 0; 1290Sstevel@tonic-gate (void) close(infd); 1300Sstevel@tonic-gate return (1); 1310Sstevel@tonic-gate } 1320Sstevel@tonic-gate 1330Sstevel@tonic-gate /* 1340Sstevel@tonic-gate * Let getenv() do the dirty work 1350Sstevel@tonic-gate * of looking up the variable. We 1360Sstevel@tonic-gate * do this by temporarily resetting 1370Sstevel@tonic-gate * environ to point to the local area. 1380Sstevel@tonic-gate */ 1390Sstevel@tonic-gate char * 1400Sstevel@tonic-gate xgetenv(char *env) 1410Sstevel@tonic-gate { 1420Sstevel@tonic-gate extern char **environ; 1430Sstevel@tonic-gate char *ret, **svenviron = environ; 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate environ = xenv; 1460Sstevel@tonic-gate ret = getenv(env); 1470Sstevel@tonic-gate environ = svenviron; 1480Sstevel@tonic-gate return (ret); 1490Sstevel@tonic-gate } 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate /* 1520Sstevel@tonic-gate * Let xgetenv() do the dirty work 1530Sstevel@tonic-gate * of looking up the variable. 1540Sstevel@tonic-gate */ 1550Sstevel@tonic-gate char * 1560Sstevel@tonic-gate Xgetenv(char *env) 1570Sstevel@tonic-gate { 1580Sstevel@tonic-gate char *ret = xgetenv(env); 1590Sstevel@tonic-gate return (ret ? ret : ""); 1600Sstevel@tonic-gate } 1610Sstevel@tonic-gate 1620Sstevel@tonic-gate /* 1630Sstevel@tonic-gate * Remove the spaces within the environment variable. 1640Sstevel@tonic-gate * The variable can look like this: 1650Sstevel@tonic-gate * 1660Sstevel@tonic-gate * <sp1> variable <sp2> = <sp3> value <sp4> \0 1670Sstevel@tonic-gate * 1680Sstevel@tonic-gate * All spaces can be removed, except within 1690Sstevel@tonic-gate * the variable name and the value. 1700Sstevel@tonic-gate */ 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate static void 1730Sstevel@tonic-gate reduce(char *from) 1740Sstevel@tonic-gate { 1750Sstevel@tonic-gate char *to = from; 1760Sstevel@tonic-gate char *svfrom = from; 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate /* <sp1> */ 1790Sstevel@tonic-gate while (*from &&isspace((int)*from)) 1800Sstevel@tonic-gate from++; 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate /* variable */ 1830Sstevel@tonic-gate while (*from && (*from != '=') && !isspace((int)*from)) 1840Sstevel@tonic-gate *to++ = *from++; 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate /* <sp2> */ 1870Sstevel@tonic-gate while (*from && isspace((int)*from)) 1880Sstevel@tonic-gate from++; 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate /* = */ 1910Sstevel@tonic-gate if (*from == '=') 1920Sstevel@tonic-gate *to++ = *from++; 1930Sstevel@tonic-gate 1940Sstevel@tonic-gate /* <sp3> */ 1950Sstevel@tonic-gate while (*from && isspace((int)*from)) 1960Sstevel@tonic-gate from++; 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate /* value */ 1990Sstevel@tonic-gate while (*from) 2000Sstevel@tonic-gate *to++ = *from++; 2010Sstevel@tonic-gate 2020Sstevel@tonic-gate /* <sp4> */ 2030Sstevel@tonic-gate while ((to > svfrom) && isspace((int)to[-1])) 2040Sstevel@tonic-gate to--; 2050Sstevel@tonic-gate *to = '\0'; 2060Sstevel@tonic-gate } 207