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