getenv.c revision 31de2854
131de2854Smrg/*	$OpenBSD: getenv.c,v 1.10 2010/08/23 22:31:50 millert Exp $ */
231de2854Smrg/*
331de2854Smrg * Copyright (c) 1987, 1993
431de2854Smrg *	The Regents of the University of California.  All rights reserved.
531de2854Smrg *
631de2854Smrg * Redistribution and use in source and binary forms, with or without
731de2854Smrg * modification, are permitted provided that the following conditions
831de2854Smrg * are met:
931de2854Smrg * 1. Redistributions of source code must retain the above copyright
1031de2854Smrg *    notice, this list of conditions and the following disclaimer.
1131de2854Smrg * 2. Redistributions in binary form must reproduce the above copyright
1231de2854Smrg *    notice, this list of conditions and the following disclaimer in the
1331de2854Smrg *    documentation and/or other materials provided with the distribution.
1431de2854Smrg * 3. Neither the name of the University nor the names of its contributors
1531de2854Smrg *    may be used to endorse or promote products derived from this software
1631de2854Smrg *    without specific prior written permission.
1731de2854Smrg *
1831de2854Smrg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1931de2854Smrg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2031de2854Smrg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2131de2854Smrg * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2231de2854Smrg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2331de2854Smrg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2431de2854Smrg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2531de2854Smrg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2631de2854Smrg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2731de2854Smrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2831de2854Smrg * SUCH DAMAGE.
2931de2854Smrg */
3031de2854Smrg
3131de2854Smrg#include <stdlib.h>
3231de2854Smrg#include <string.h>
3331de2854Smrg
3431de2854Smrgchar *__findenv(const char *name, int len, int *offset);
3531de2854Smrg
3631de2854Smrg/*
3731de2854Smrg * __findenv --
3831de2854Smrg *	Returns pointer to value associated with name, if any, else NULL.
3931de2854Smrg *	Starts searching within the environmental array at offset.
4031de2854Smrg *	Sets offset to be the offset of the name/value combination in the
4131de2854Smrg *	environmental array, for use by putenv(3), setenv(3) and unsetenv(3).
4231de2854Smrg *	Explicitly removes '=' in argument name.
4331de2854Smrg *
4431de2854Smrg *	This routine *should* be a static; don't use it.
4531de2854Smrg */
4631de2854Smrgchar *
4731de2854Smrg__findenv(const char *name, int len, int *offset)
4831de2854Smrg{
4931de2854Smrg	extern char **environ;
5031de2854Smrg	int i;
5131de2854Smrg	const char *np;
5231de2854Smrg	char **p, *cp;
5331de2854Smrg
5431de2854Smrg	if (name == NULL || environ == NULL)
5531de2854Smrg		return (NULL);
5631de2854Smrg	for (p = environ + *offset; (cp = *p) != NULL; ++p) {
5731de2854Smrg		for (np = name, i = len; i && *cp; i--)
5831de2854Smrg			if (*cp++ != *np++)
5931de2854Smrg				break;
6031de2854Smrg		if (i == 0 && *cp++ == '=') {
6131de2854Smrg			*offset = p - environ;
6231de2854Smrg			return (cp);
6331de2854Smrg		}
6431de2854Smrg	}
6531de2854Smrg	return (NULL);
6631de2854Smrg}
6731de2854Smrg
6831de2854Smrg/*
6931de2854Smrg * getenv --
7031de2854Smrg *	Returns ptr to value associated with name, if any, else NULL.
7131de2854Smrg */
7231de2854Smrgchar *
7331de2854Smrggetenv(const char *name)
7431de2854Smrg{
7531de2854Smrg	int offset = 0;
7631de2854Smrg	const char *np;
7731de2854Smrg
7831de2854Smrg	for (np = name; *np && *np != '='; ++np)
7931de2854Smrg		;
8031de2854Smrg	return (__findenv(name, (int)(np - name), &offset));
8131de2854Smrg}
82