2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
2N/A/* All Rights Reserved */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A * NAME
2N/A * xsetenv, xgetenv, Xgetenv - manage an alternate environment space
2N/A *
2N/A * SYNOPSIS
2N/A * int ret = xsetenv(file)
2N/A * char *x = xgetenv("FOO");
2N/A * char *x = Xgetenv("FOO");
2N/A *
2N/A * DESCRIPTION
2N/A * xsetenv() reads the given file into an internal buffer
2N/A * and sets up an alternate environment.
2N/A *
2N/A * Return values: 1 - OKAY
2N/A * 0 - troubles reading the file
2N/A * -1 - troubles opening the file
2N/A *
2N/A * xgetenv() returns the environment value from the
2N/A * alternate environment.
2N/A *
2N/A * Return values: (char *)0 - no value for that variable
2N/A * pointer - the value
2N/A *
2N/A * Xgetenv() returns the environment value from the
2N/A * alternate environment.
2N/A *
2N/A * Return values: "" - no value for that variable
2N/A * pointer - the value
2N/A *
2N/A * LIMITATIONS
2N/A * Assumes the environment is < 5120 bytes, as in the UNIX
2N/A * System environment. Assumes < 512 lines in the file.
2N/A * These values may be adjusted below.
2N/A */
2N/A
2N/A#include <sys/types.h>
2N/A#include "libmail.h"
2N/A#include <stdio.h>
2N/A#include <string.h>
2N/A#include <fcntl.h>
2N/A#include <ctype.h>
2N/A
2N/A#include <stdlib.h>
2N/A#include <unistd.h>
2N/A
2N/A#define MAXVARS 512
2N/A#define MAXENV 5120
2N/A
2N/Astatic char **xenv = 0;
2N/Astatic char *(xenvptrs[MAXVARS]);
2N/Astatic char xbuf[MAXENV];
2N/A
2N/Astatic void reduce(char *);
2N/A
2N/A/*
2N/A * set up an environment buffer
2N/A * and the pointers into it
2N/A */
2N/Aint
2N/Axsetenv(char *xfile)
2N/A{
2N/A int envctr, infd;
2N/A ssize_t i, nread;
2N/A
2N/A /* Open the file */
2N/A infd = open(xfile, O_RDONLY);
2N/A if (infd == -1) {
2N/A return (-1);
2N/A }
2N/A
2N/A /* Read in the entire file. */
2N/A nread = read(infd, xbuf, sizeof (xbuf));
2N/A if (nread < 0) {
2N/A (void) close(infd);
2N/A return (0);
2N/A }
2N/A
2N/A /*
2N/A * Set up pointers into the buffer.
2N/A * Replace \n with \0.
2N/A * Collapse white space around the = sign and at the
2N/A * beginning and end of the line.
2N/A */
2N/A xenv = xenvptrs;
2N/A xenv[0] = &xbuf[0];
2N/A for (i = 0, envctr = 0; i < nread; i++) {
2N/A if (xbuf[i] == '\n') {
2N/A xbuf[i] = '\0';
2N/A reduce(xenv[envctr]);
2N/A xenv[++envctr] = &xbuf[i+1];
2N/A if (envctr == MAXVARS) {
2N/A break;
2N/A }
2N/A }
2N/A }
2N/A
2N/A xenv[envctr] = 0;
2N/A (void) close(infd);
2N/A return (1);
2N/A}
2N/A
2N/A/*
2N/A * Let getenv() do the dirty work
2N/A * of looking up the variable. We
2N/A * do this by temporarily resetting
2N/A * environ to point to the local area.
2N/A */
2N/Achar *
2N/Axgetenv(char *env)
2N/A{
2N/A extern char **environ;
2N/A char *ret, **svenviron = environ;
2N/A
2N/A environ = xenv;
2N/A ret = getenv(env);
2N/A environ = svenviron;
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * Let xgetenv() do the dirty work
2N/A * of looking up the variable.
2N/A */
2N/Achar *
2N/AXgetenv(char *env)
2N/A{
2N/A char *ret = xgetenv(env);
2N/A return (ret ? ret : "");
2N/A}
2N/A
2N/A/*
2N/A * Remove the spaces within the environment variable.
2N/A * The variable can look like this:
2N/A *
2N/A * <sp1> variable <sp2> = <sp3> value <sp4> \0
2N/A *
2N/A * All spaces can be removed, except within
2N/A * the variable name and the value.
2N/A */
2N/A
2N/Astatic void
2N/Areduce(char *from)
2N/A{
2N/A char *to = from;
2N/A char *svfrom = from;
2N/A
2N/A /* <sp1> */
2N/A while (*from &&isspace((int)*from))
2N/A from++;
2N/A
2N/A /* variable */
2N/A while (*from && (*from != '=') && !isspace((int)*from))
2N/A *to++ = *from++;
2N/A
2N/A /* <sp2> */
2N/A while (*from && isspace((int)*from))
2N/A from++;
2N/A
2N/A /* = */
2N/A if (*from == '=')
2N/A *to++ = *from++;
2N/A
2N/A /* <sp3> */
2N/A while (*from && isspace((int)*from))
2N/A from++;
2N/A
2N/A /* value */
2N/A while (*from)
2N/A *to++ = *from++;
2N/A
2N/A /* <sp4> */
2N/A while ((to > svfrom) && isspace((int)to[-1]))
2N/A to--;
2N/A *to = '\0';
2N/A}