1 | /* $Id: kHlpBareEnv.c 2 2007-11-16 16:07:14Z bird $ */
|
---|
2 | /** @file
|
---|
3 | * kHlpBare - Environment Manipulation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2006-2007 knut st. osmundsen <[email protected]>
|
---|
8 | *
|
---|
9 | * This file is part of kStuff.
|
---|
10 | *
|
---|
11 | * kStuff is free software; you can redistribute it and/or
|
---|
12 | * modify it under the terms of the GNU Lesser General Public
|
---|
13 | * License as published by the Free Software Foundation; either
|
---|
14 | * version 2.1 of the License, or (at your option) any later version.
|
---|
15 | *
|
---|
16 | * In addition to the permissions in the GNU Lesser General Public
|
---|
17 | * License, you are granted unlimited permission to link the compiled
|
---|
18 | * version of this file into combinations with other programs, and to
|
---|
19 | * distribute those combinations without any restriction coming from
|
---|
20 | * the use of this file.
|
---|
21 | *
|
---|
22 | * kStuff is distributed in the hope that it will be useful,
|
---|
23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
25 | * Lesser General Public License for more details.
|
---|
26 | *
|
---|
27 | * You should have received a copy of the GNU Lesser General Public
|
---|
28 | * License along with kStuff; if not, write to the Free Software
|
---|
29 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
---|
30 | * 02110-1301, USA
|
---|
31 | */
|
---|
32 |
|
---|
33 | /*******************************************************************************
|
---|
34 | * Header Files *
|
---|
35 | *******************************************************************************/
|
---|
36 | #include <k/kHlpEnv.h>
|
---|
37 | #include <k/kHlpString.h>
|
---|
38 | #include <k/kErrors.h>
|
---|
39 |
|
---|
40 | #if K_OS == K_OS_DARWIN
|
---|
41 |
|
---|
42 | #elif K_OS == K_OS_LINUX
|
---|
43 |
|
---|
44 | #elif K_OS == K_OS_OS2
|
---|
45 | # define INCL_BASE
|
---|
46 | # define INCL_ERRORS
|
---|
47 | # include <os2.h>
|
---|
48 |
|
---|
49 | #elif K_OS == K_OS_WINDOWS
|
---|
50 | # include <Windows.h>
|
---|
51 |
|
---|
52 | #else
|
---|
53 | # error "port me"
|
---|
54 | #endif
|
---|
55 |
|
---|
56 |
|
---|
57 | KHLP_DECL(int) kHlpGetEnv(const char *pszVar, char *pszVal, KSIZE cchVal)
|
---|
58 | {
|
---|
59 | #if K_OS == K_OS_DARWIN
|
---|
60 | /** @todo need to figure out where the stuff is or how it's inherited on darwin ... */
|
---|
61 | return KERR_ENVVAR_NOT_FOUND;
|
---|
62 |
|
---|
63 | #elif K_OS == K_OS_LINUX
|
---|
64 | /** @todo either read /proc/self/environ or figure out where in the memory the initial environment is... */
|
---|
65 | return KERR_ENVVAR_NOT_FOUND;
|
---|
66 |
|
---|
67 | #elif K_OS == K_OS_OS2
|
---|
68 | PSZ pszValue = NULL;
|
---|
69 | int rc;
|
---|
70 |
|
---|
71 | *pszVal = '\0';
|
---|
72 | rc = DosScanEnv((PCSZ)pszVar, &pszValue);
|
---|
73 | if (!rc)
|
---|
74 | {
|
---|
75 | KSIZE cch = kHlpStrLen((const char *)pszValue);
|
---|
76 | if (cchVal > cch)
|
---|
77 | kHlpMemCopy(pszVal, pszValue, cch + 1);
|
---|
78 | else
|
---|
79 | rc = KERR_BUFFER_OVERFLOW;
|
---|
80 | }
|
---|
81 | else
|
---|
82 | rc = KERR_ENVVAR_NOT_FOUND;
|
---|
83 | return rc;
|
---|
84 |
|
---|
85 | #elif K_OS == K_OS_WINDOWS
|
---|
86 | DWORD cch;
|
---|
87 |
|
---|
88 | SetLastError(0);
|
---|
89 | cch = GetEnvironmentVariable(pszVar, pszVal, cchVal);
|
---|
90 | if (cch > 0 && cch < cchVal)
|
---|
91 | return 0;
|
---|
92 |
|
---|
93 | *pszVal = '\0';
|
---|
94 | if (cch >= cchVal)
|
---|
95 | return KERR_BUFFER_OVERFLOW;
|
---|
96 | if (GetLastError() == ERROR_ENVVAR_NOT_FOUND)
|
---|
97 | return KERR_ENVVAR_NOT_FOUND;
|
---|
98 | return GetLastError();
|
---|
99 |
|
---|
100 | #else
|
---|
101 | # error "Port me"
|
---|
102 | #endif
|
---|
103 | }
|
---|
104 |
|
---|