1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 1997-2009 Oracle.  All rights reserved.
5 *
6 * $Id$
7 */
8
9#include "db_config.h"
10
11#include "db_int.h"
12
13/*
14 * __os_getenv --
15 *	Retrieve an environment variable.
16 *
17 * PUBLIC: int __os_getenv __P((ENV *, const char *, char **, size_t));
18 */
19int
20__os_getenv(env, name, bpp, buflen)
21	ENV *env;
22	const char *name;
23	char **bpp;
24	size_t buflen;
25{
26	/*
27	 * If we have getenv, there's a value and the buffer is large enough:
28	 *	copy value into the pointer, return 0
29	 * If we have getenv, there's a value  and the buffer is too short:
30	 *	set pointer to NULL, return EINVAL
31	 * If we have getenv and there's no value:
32	 *	set pointer to NULL, return 0
33	 * If we don't have getenv:
34	 *	set pointer to NULL, return 0
35	 */
36#ifdef HAVE_GETENV
37	char *p;
38
39	if ((p = getenv(name)) != NULL) {
40		if (strlen(p) < buflen) {
41			(void)strcpy(*bpp, p);
42			return (0);
43		}
44
45		*bpp = NULL;
46		__db_errx(env,
47		    "%s: buffer too small to hold environment variable %s",
48		    name, p);
49		return (EINVAL);
50	}
51#else
52	COMPQUIET(env, NULL);
53	COMPQUIET(name, NULL);
54	COMPQUIET(buflen, 0);
55#endif
56	*bpp = NULL;
57	return (0);
58}
59