17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 22*e8031f0aSraf 237c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 247c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate /* 27*e8031f0aSraf * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 28*e8031f0aSraf * Use is subject to license terms. 297c478bd9Sstevel@tonic-gate */ 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 327c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/ 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate /* 357c478bd9Sstevel@tonic-gate * NAME 367c478bd9Sstevel@tonic-gate * xsetenv, xgetenv, Xgetenv - manage an alternate environment space 377c478bd9Sstevel@tonic-gate * 387c478bd9Sstevel@tonic-gate * SYNOPSIS 397c478bd9Sstevel@tonic-gate * int ret = xsetenv(file) 407c478bd9Sstevel@tonic-gate * char *x = xgetenv("FOO"); 417c478bd9Sstevel@tonic-gate * char *x = Xgetenv("FOO"); 427c478bd9Sstevel@tonic-gate * 437c478bd9Sstevel@tonic-gate * DESCRIPTION 447c478bd9Sstevel@tonic-gate * xsetenv() reads the given file into an internal buffer 457c478bd9Sstevel@tonic-gate * and sets up an alternate environment. 467c478bd9Sstevel@tonic-gate * 477c478bd9Sstevel@tonic-gate * Return values: 1 - OKAY 487c478bd9Sstevel@tonic-gate * 0 - troubles reading the file 497c478bd9Sstevel@tonic-gate * -1 - troubles opening the file 507c478bd9Sstevel@tonic-gate * 517c478bd9Sstevel@tonic-gate * xgetenv() returns the environment value from the 527c478bd9Sstevel@tonic-gate * alternate environment. 537c478bd9Sstevel@tonic-gate * 547c478bd9Sstevel@tonic-gate * Return values: (char *)0 - no value for that variable 557c478bd9Sstevel@tonic-gate * pointer - the value 567c478bd9Sstevel@tonic-gate * 577c478bd9Sstevel@tonic-gate * Xgetenv() returns the environment value from the 587c478bd9Sstevel@tonic-gate * alternate environment. 597c478bd9Sstevel@tonic-gate * 607c478bd9Sstevel@tonic-gate * Return values: "" - no value for that variable 617c478bd9Sstevel@tonic-gate * pointer - the value 627c478bd9Sstevel@tonic-gate * 637c478bd9Sstevel@tonic-gate * LIMITATIONS 647c478bd9Sstevel@tonic-gate * Assumes the environment is < 5120 bytes, as in the UNIX 657c478bd9Sstevel@tonic-gate * System environment. Assumes < 512 lines in the file. 667c478bd9Sstevel@tonic-gate * These values may be adjusted below. 677c478bd9Sstevel@tonic-gate */ 687c478bd9Sstevel@tonic-gate 69*e8031f0aSraf #include "c_synonyms.h" 707c478bd9Sstevel@tonic-gate #include <sys/types.h> 717c478bd9Sstevel@tonic-gate #include "libmail.h" 727c478bd9Sstevel@tonic-gate #include <stdio.h> 737c478bd9Sstevel@tonic-gate #include <string.h> 747c478bd9Sstevel@tonic-gate #include <fcntl.h> 757c478bd9Sstevel@tonic-gate #include <ctype.h> 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate #include <stdlib.h> 787c478bd9Sstevel@tonic-gate #include <unistd.h> 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate #define MAXVARS 512 817c478bd9Sstevel@tonic-gate #define MAXENV 5120 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate static char **xenv = 0; 847c478bd9Sstevel@tonic-gate static char *(xenvptrs[MAXVARS]); 857c478bd9Sstevel@tonic-gate static char xbuf[MAXENV]; 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate static void reduce(char *); 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate /* 907c478bd9Sstevel@tonic-gate * set up an environment buffer 917c478bd9Sstevel@tonic-gate * and the pointers into it 927c478bd9Sstevel@tonic-gate */ 937c478bd9Sstevel@tonic-gate int 947c478bd9Sstevel@tonic-gate xsetenv(char *xfile) 957c478bd9Sstevel@tonic-gate { 967c478bd9Sstevel@tonic-gate int envctr, infd; 977c478bd9Sstevel@tonic-gate ssize_t i, nread; 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate /* Open the file */ 1007c478bd9Sstevel@tonic-gate infd = open(xfile, O_RDONLY); 1017c478bd9Sstevel@tonic-gate if (infd == -1) { 1027c478bd9Sstevel@tonic-gate return (-1); 1037c478bd9Sstevel@tonic-gate } 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate /* Read in the entire file. */ 1067c478bd9Sstevel@tonic-gate nread = read(infd, xbuf, sizeof (xbuf)); 1077c478bd9Sstevel@tonic-gate if (nread < 0) { 1087c478bd9Sstevel@tonic-gate (void) close(infd); 1097c478bd9Sstevel@tonic-gate return (0); 1107c478bd9Sstevel@tonic-gate } 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate /* 1137c478bd9Sstevel@tonic-gate * Set up pointers into the buffer. 1147c478bd9Sstevel@tonic-gate * Replace \n with \0. 1157c478bd9Sstevel@tonic-gate * Collapse white space around the = sign and at the 1167c478bd9Sstevel@tonic-gate * beginning and end of the line. 1177c478bd9Sstevel@tonic-gate */ 1187c478bd9Sstevel@tonic-gate xenv = xenvptrs; 1197c478bd9Sstevel@tonic-gate xenv[0] = &xbuf[0]; 1207c478bd9Sstevel@tonic-gate for (i = 0, envctr = 0; i < nread; i++) { 1217c478bd9Sstevel@tonic-gate if (xbuf[i] == '\n') { 1227c478bd9Sstevel@tonic-gate xbuf[i] = '\0'; 1237c478bd9Sstevel@tonic-gate reduce(xenv[envctr]); 1247c478bd9Sstevel@tonic-gate xenv[++envctr] = &xbuf[i+1]; 1257c478bd9Sstevel@tonic-gate if (envctr == MAXVARS) { 1267c478bd9Sstevel@tonic-gate break; 1277c478bd9Sstevel@tonic-gate } 1287c478bd9Sstevel@tonic-gate } 1297c478bd9Sstevel@tonic-gate } 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate xenv[envctr] = 0; 1327c478bd9Sstevel@tonic-gate (void) close(infd); 1337c478bd9Sstevel@tonic-gate return (1); 1347c478bd9Sstevel@tonic-gate } 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate /* 1377c478bd9Sstevel@tonic-gate * Let getenv() do the dirty work 1387c478bd9Sstevel@tonic-gate * of looking up the variable. We 1397c478bd9Sstevel@tonic-gate * do this by temporarily resetting 1407c478bd9Sstevel@tonic-gate * environ to point to the local area. 1417c478bd9Sstevel@tonic-gate */ 1427c478bd9Sstevel@tonic-gate char * 1437c478bd9Sstevel@tonic-gate xgetenv(char *env) 1447c478bd9Sstevel@tonic-gate { 1457c478bd9Sstevel@tonic-gate extern char **environ; 1467c478bd9Sstevel@tonic-gate char *ret, **svenviron = environ; 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate environ = xenv; 1497c478bd9Sstevel@tonic-gate ret = getenv(env); 1507c478bd9Sstevel@tonic-gate environ = svenviron; 1517c478bd9Sstevel@tonic-gate return (ret); 1527c478bd9Sstevel@tonic-gate } 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate /* 1557c478bd9Sstevel@tonic-gate * Let xgetenv() do the dirty work 1567c478bd9Sstevel@tonic-gate * of looking up the variable. 1577c478bd9Sstevel@tonic-gate */ 1587c478bd9Sstevel@tonic-gate char * 1597c478bd9Sstevel@tonic-gate Xgetenv(char *env) 1607c478bd9Sstevel@tonic-gate { 1617c478bd9Sstevel@tonic-gate char *ret = xgetenv(env); 1627c478bd9Sstevel@tonic-gate return (ret ? ret : ""); 1637c478bd9Sstevel@tonic-gate } 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate /* 1667c478bd9Sstevel@tonic-gate * Remove the spaces within the environment variable. 1677c478bd9Sstevel@tonic-gate * The variable can look like this: 1687c478bd9Sstevel@tonic-gate * 1697c478bd9Sstevel@tonic-gate * <sp1> variable <sp2> = <sp3> value <sp4> \0 1707c478bd9Sstevel@tonic-gate * 1717c478bd9Sstevel@tonic-gate * All spaces can be removed, except within 1727c478bd9Sstevel@tonic-gate * the variable name and the value. 1737c478bd9Sstevel@tonic-gate */ 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate static void 1767c478bd9Sstevel@tonic-gate reduce(char *from) 1777c478bd9Sstevel@tonic-gate { 1787c478bd9Sstevel@tonic-gate char *to = from; 1797c478bd9Sstevel@tonic-gate char *svfrom = from; 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate /* <sp1> */ 1827c478bd9Sstevel@tonic-gate while (*from &&isspace((int)*from)) 1837c478bd9Sstevel@tonic-gate from++; 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate /* variable */ 1867c478bd9Sstevel@tonic-gate while (*from && (*from != '=') && !isspace((int)*from)) 1877c478bd9Sstevel@tonic-gate *to++ = *from++; 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate /* <sp2> */ 1907c478bd9Sstevel@tonic-gate while (*from && isspace((int)*from)) 1917c478bd9Sstevel@tonic-gate from++; 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate /* = */ 1947c478bd9Sstevel@tonic-gate if (*from == '=') 1957c478bd9Sstevel@tonic-gate *to++ = *from++; 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate /* <sp3> */ 1987c478bd9Sstevel@tonic-gate while (*from && isspace((int)*from)) 1997c478bd9Sstevel@tonic-gate from++; 2007c478bd9Sstevel@tonic-gate 2017c478bd9Sstevel@tonic-gate /* value */ 2027c478bd9Sstevel@tonic-gate while (*from) 2037c478bd9Sstevel@tonic-gate *to++ = *from++; 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate /* <sp4> */ 2067c478bd9Sstevel@tonic-gate while ((to > svfrom) && isspace((int)to[-1])) 2077c478bd9Sstevel@tonic-gate to--; 2087c478bd9Sstevel@tonic-gate *to = '\0'; 2097c478bd9Sstevel@tonic-gate } 210