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 #pragma ident "%Z%%M% %I% %E% SMI"
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate * NAME
347c478bd9Sstevel@tonic-gate * xsetenv, xgetenv, Xgetenv - manage an alternate environment space
357c478bd9Sstevel@tonic-gate *
367c478bd9Sstevel@tonic-gate * SYNOPSIS
377c478bd9Sstevel@tonic-gate * int ret = xsetenv(file)
387c478bd9Sstevel@tonic-gate * char *x = xgetenv("FOO");
397c478bd9Sstevel@tonic-gate * char *x = Xgetenv("FOO");
407c478bd9Sstevel@tonic-gate *
417c478bd9Sstevel@tonic-gate * DESCRIPTION
427c478bd9Sstevel@tonic-gate * xsetenv() reads the given file into an internal buffer
437c478bd9Sstevel@tonic-gate * and sets up an alternate environment.
447c478bd9Sstevel@tonic-gate *
457c478bd9Sstevel@tonic-gate * Return values: 1 - OKAY
467c478bd9Sstevel@tonic-gate * 0 - troubles reading the file
477c478bd9Sstevel@tonic-gate * -1 - troubles opening the file
487c478bd9Sstevel@tonic-gate *
497c478bd9Sstevel@tonic-gate * xgetenv() returns the environment value from the
507c478bd9Sstevel@tonic-gate * alternate environment.
517c478bd9Sstevel@tonic-gate *
527c478bd9Sstevel@tonic-gate * Return values: (char *)0 - no value for that variable
537c478bd9Sstevel@tonic-gate * pointer - the value
547c478bd9Sstevel@tonic-gate *
557c478bd9Sstevel@tonic-gate * Xgetenv() returns the environment value from the
567c478bd9Sstevel@tonic-gate * alternate environment.
577c478bd9Sstevel@tonic-gate *
587c478bd9Sstevel@tonic-gate * Return values: "" - no value for that variable
597c478bd9Sstevel@tonic-gate * pointer - the value
607c478bd9Sstevel@tonic-gate *
617c478bd9Sstevel@tonic-gate * LIMITATIONS
627c478bd9Sstevel@tonic-gate * Assumes the environment is < 5120 bytes, as in the UNIX
637c478bd9Sstevel@tonic-gate * System environment. Assumes < 512 lines in the file.
647c478bd9Sstevel@tonic-gate * These values may be adjusted below.
657c478bd9Sstevel@tonic-gate */
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate #include <sys/types.h>
687c478bd9Sstevel@tonic-gate #include "libmail.h"
697c478bd9Sstevel@tonic-gate #include <stdio.h>
707c478bd9Sstevel@tonic-gate #include <string.h>
717c478bd9Sstevel@tonic-gate #include <fcntl.h>
727c478bd9Sstevel@tonic-gate #include <ctype.h>
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate #include <stdlib.h>
757c478bd9Sstevel@tonic-gate #include <unistd.h>
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate #define MAXVARS 512
787c478bd9Sstevel@tonic-gate #define MAXENV 5120
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate static char **xenv = 0;
817c478bd9Sstevel@tonic-gate static char *(xenvptrs[MAXVARS]);
827c478bd9Sstevel@tonic-gate static char xbuf[MAXENV];
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate static void reduce(char *);
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate * set up an environment buffer
887c478bd9Sstevel@tonic-gate * and the pointers into it
897c478bd9Sstevel@tonic-gate */
907c478bd9Sstevel@tonic-gate int
xsetenv(char * xfile)917c478bd9Sstevel@tonic-gate xsetenv(char *xfile)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate int envctr, infd;
947c478bd9Sstevel@tonic-gate ssize_t i, nread;
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate /* Open the file */
977c478bd9Sstevel@tonic-gate infd = open(xfile, O_RDONLY);
987c478bd9Sstevel@tonic-gate if (infd == -1) {
997c478bd9Sstevel@tonic-gate return (-1);
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate /* Read in the entire file. */
1037c478bd9Sstevel@tonic-gate nread = read(infd, xbuf, sizeof (xbuf));
1047c478bd9Sstevel@tonic-gate if (nread < 0) {
1057c478bd9Sstevel@tonic-gate (void) close(infd);
1067c478bd9Sstevel@tonic-gate return (0);
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate * Set up pointers into the buffer.
1117c478bd9Sstevel@tonic-gate * Replace \n with \0.
1127c478bd9Sstevel@tonic-gate * Collapse white space around the = sign and at the
1137c478bd9Sstevel@tonic-gate * beginning and end of the line.
1147c478bd9Sstevel@tonic-gate */
1157c478bd9Sstevel@tonic-gate xenv = xenvptrs;
1167c478bd9Sstevel@tonic-gate xenv[0] = &xbuf[0];
1177c478bd9Sstevel@tonic-gate for (i = 0, envctr = 0; i < nread; i++) {
1187c478bd9Sstevel@tonic-gate if (xbuf[i] == '\n') {
1197c478bd9Sstevel@tonic-gate xbuf[i] = '\0';
1207c478bd9Sstevel@tonic-gate reduce(xenv[envctr]);
1217c478bd9Sstevel@tonic-gate xenv[++envctr] = &xbuf[i+1];
1227c478bd9Sstevel@tonic-gate if (envctr == MAXVARS) {
1237c478bd9Sstevel@tonic-gate break;
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate xenv[envctr] = 0;
1297c478bd9Sstevel@tonic-gate (void) close(infd);
1307c478bd9Sstevel@tonic-gate return (1);
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate /*
1347c478bd9Sstevel@tonic-gate * Let getenv() do the dirty work
1357c478bd9Sstevel@tonic-gate * of looking up the variable. We
1367c478bd9Sstevel@tonic-gate * do this by temporarily resetting
1377c478bd9Sstevel@tonic-gate * environ to point to the local area.
1387c478bd9Sstevel@tonic-gate */
1397c478bd9Sstevel@tonic-gate char *
xgetenv(char * env)1407c478bd9Sstevel@tonic-gate xgetenv(char *env)
1417c478bd9Sstevel@tonic-gate {
1427c478bd9Sstevel@tonic-gate extern char **environ;
1437c478bd9Sstevel@tonic-gate char *ret, **svenviron = environ;
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate environ = xenv;
1467c478bd9Sstevel@tonic-gate ret = getenv(env);
1477c478bd9Sstevel@tonic-gate environ = svenviron;
1487c478bd9Sstevel@tonic-gate return (ret);
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate /*
1527c478bd9Sstevel@tonic-gate * Let xgetenv() do the dirty work
1537c478bd9Sstevel@tonic-gate * of looking up the variable.
1547c478bd9Sstevel@tonic-gate */
1557c478bd9Sstevel@tonic-gate char *
Xgetenv(char * env)1567c478bd9Sstevel@tonic-gate Xgetenv(char *env)
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate char *ret = xgetenv(env);
1597c478bd9Sstevel@tonic-gate return (ret ? ret : "");
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate /*
1637c478bd9Sstevel@tonic-gate * Remove the spaces within the environment variable.
1647c478bd9Sstevel@tonic-gate * The variable can look like this:
1657c478bd9Sstevel@tonic-gate *
1667c478bd9Sstevel@tonic-gate * <sp1> variable <sp2> = <sp3> value <sp4> \0
1677c478bd9Sstevel@tonic-gate *
1687c478bd9Sstevel@tonic-gate * All spaces can be removed, except within
1697c478bd9Sstevel@tonic-gate * the variable name and the value.
1707c478bd9Sstevel@tonic-gate */
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate static void
reduce(char * from)1737c478bd9Sstevel@tonic-gate reduce(char *from)
1747c478bd9Sstevel@tonic-gate {
1757c478bd9Sstevel@tonic-gate char *to = from;
1767c478bd9Sstevel@tonic-gate char *svfrom = from;
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate /* <sp1> */
1797c478bd9Sstevel@tonic-gate while (*from &&isspace((int)*from))
1807c478bd9Sstevel@tonic-gate from++;
1817c478bd9Sstevel@tonic-gate
1827c478bd9Sstevel@tonic-gate /* variable */
1837c478bd9Sstevel@tonic-gate while (*from && (*from != '=') && !isspace((int)*from))
1847c478bd9Sstevel@tonic-gate *to++ = *from++;
1857c478bd9Sstevel@tonic-gate
1867c478bd9Sstevel@tonic-gate /* <sp2> */
1877c478bd9Sstevel@tonic-gate while (*from && isspace((int)*from))
1887c478bd9Sstevel@tonic-gate from++;
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate /* = */
1917c478bd9Sstevel@tonic-gate if (*from == '=')
1927c478bd9Sstevel@tonic-gate *to++ = *from++;
1937c478bd9Sstevel@tonic-gate
1947c478bd9Sstevel@tonic-gate /* <sp3> */
1957c478bd9Sstevel@tonic-gate while (*from && isspace((int)*from))
1967c478bd9Sstevel@tonic-gate from++;
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate /* value */
1997c478bd9Sstevel@tonic-gate while (*from)
2007c478bd9Sstevel@tonic-gate *to++ = *from++;
2017c478bd9Sstevel@tonic-gate
2027c478bd9Sstevel@tonic-gate /* <sp4> */
2037c478bd9Sstevel@tonic-gate while ((to > svfrom) && isspace((int)to[-1]))
2047c478bd9Sstevel@tonic-gate to--;
2057c478bd9Sstevel@tonic-gate *to = '\0';
2067c478bd9Sstevel@tonic-gate }
207