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