2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1997-2009 Oracle. All rights reserved.
15 * Retrieve an environment variable.
18 __os_getenv(env, name, bpp, buflen)
25 COMPQUIET(name, NULL);
26 /* WinCE does not have a getenv implementation. */
29 _TCHAR *tname, tbuf[1024];
34 * If there's a value and the buffer is large enough:
35 * copy value into the pointer, return 0
36 * If there's a value and the buffer is too short:
37 * set pointer to NULL, return EINVAL
38 * If there's no value:
39 * set pointer to NULL, return 0
41 if ((p = getenv(name)) != NULL) {
42 if (strlen(p) < buflen) {
43 (void)strcpy(*bpp, p);
49 TO_TSTRING(env, name, tname, ret);
53 * The declared size of the tbuf buffer limits the maximum environment
54 * variable size in Berkeley DB on Windows. If that's too small, or if
55 * we need to get rid of large allocations on the BDB stack, we should
56 * malloc the tbuf memory.
58 ret = GetEnvironmentVariable(tname, tbuf, sizeof(tbuf));
59 FREE_STRING(env, tname);
62 * If GetEnvironmentVariable succeeds, the return value is the number
63 * of characters stored in the buffer pointed to by lpBuffer, not
64 * including the terminating null character. If the buffer is not
65 * large enough to hold the data, the return value is the buffer size,
66 * in characters, required to hold the string and its terminating null
67 * character. If GetEnvironmentVariable fails, the return value is
68 * zero. If the specified environment variable was not found in the
69 * environment block, GetLastError returns ERROR_ENVVAR_NOT_FOUND.
72 if ((ret = __os_get_syserr()) == ERROR_ENVVAR_NOT_FOUND) {
76 __db_syserr(env, ret, "GetEnvironmentVariable");
77 return (__os_posix_err(ret));
79 if (ret > (int)sizeof(tbuf))
82 FROM_TSTRING(env, tbuf, p, ret);
85 if (strlen(p) < buflen)
86 (void)strcpy(*bpp, p);
98 "%s: buffer too small to hold environment variable %s",