1 | /*
|
---|
2 | * msvcrt.dll environment functions
|
---|
3 | *
|
---|
4 | * Copyright 1996,1998 Marcus Meissner
|
---|
5 | * Copyright 1996 Jukka Iivonen
|
---|
6 | * Copyright 1997,2000 Uwe Bonnes
|
---|
7 | * Copyright 2000 Jon Griffiths
|
---|
8 | *
|
---|
9 | * This library is free software; you can redistribute it and/or
|
---|
10 | * modify it under the terms of the GNU Lesser General Public
|
---|
11 | * License as published by the Free Software Foundation; either
|
---|
12 | * version 2.1 of the License, or (at your option) any later version.
|
---|
13 | *
|
---|
14 | * This library is distributed in the hope that it will be useful,
|
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
17 | * Lesser General Public License for more details.
|
---|
18 | *
|
---|
19 | * You should have received a copy of the GNU Lesser General Public
|
---|
20 | * License along with this library; if not, write to the Free Software
|
---|
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
22 | */
|
---|
23 | #ifdef __WIN32OS2__
|
---|
24 | #include <winbase.h>
|
---|
25 | #include <string.h>
|
---|
26 | #endif
|
---|
27 |
|
---|
28 | #include "wine/unicode.h"
|
---|
29 | #include "msvcrt.h"
|
---|
30 |
|
---|
31 | #include "msvcrt/stddef.h"
|
---|
32 | #include "msvcrt/stdlib.h"
|
---|
33 |
|
---|
34 |
|
---|
35 | #include "wine/debug.h"
|
---|
36 |
|
---|
37 | WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
|
---|
38 |
|
---|
39 | /*********************************************************************
|
---|
40 | * getenv (MSVCRT.@)
|
---|
41 | */
|
---|
42 | char *MSVCRT_getenv(const char *name)
|
---|
43 | {
|
---|
44 | char *environ = GetEnvironmentStringsA();
|
---|
45 | char *pp,*pos = NULL;
|
---|
46 | unsigned int length=strlen(name);
|
---|
47 |
|
---|
48 | dprintf(("MSVCRT: %s",name));
|
---|
49 |
|
---|
50 | for (pp = environ; (*pp); pp = pp + strlen(pp) +1)
|
---|
51 | {
|
---|
52 | pos =strchr(pp,'=');
|
---|
53 | if ((pos) && ((pos - pp) == length))
|
---|
54 | {
|
---|
55 | if (!_strnicmp(pp,name,length)) break;
|
---|
56 | }
|
---|
57 | }
|
---|
58 | if ((*pp)&& (pos))
|
---|
59 | {
|
---|
60 | pp = pos+1;
|
---|
61 | TRACE("got %s\n",pp);
|
---|
62 | }
|
---|
63 | else
|
---|
64 | pp = 0;
|
---|
65 | FreeEnvironmentStringsA( environ );
|
---|
66 | return pp;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /*********************************************************************
|
---|
70 | * _wgetenv (MSVCRT.@)
|
---|
71 | */
|
---|
72 | MSVCRT_wchar_t *_wgetenv(const MSVCRT_wchar_t *name)
|
---|
73 | {
|
---|
74 | MSVCRT_wchar_t* environ = GetEnvironmentStringsW();
|
---|
75 | MSVCRT_wchar_t* pp,*pos = NULL;
|
---|
76 | unsigned int length=strlenW(name);
|
---|
77 |
|
---|
78 | dprintf(("MSVCRT: _wgetenv %s",debugstr_w(name)));
|
---|
79 |
|
---|
80 | for (pp = environ; (*pp); pp = pp + strlenW(pp) + 1)
|
---|
81 | {
|
---|
82 | pos = strchrW(pp,'=');
|
---|
83 | if ((pos) && ((pos - pp) == length))
|
---|
84 | {
|
---|
85 | if (!strncmpiW(pp,name,length))
|
---|
86 | {
|
---|
87 | pp = pos+1;
|
---|
88 | TRACE("got %s\n",debugstr_w(pp));
|
---|
89 | /* can't free pointer since we are returning it */
|
---|
90 | /* should probably use MSVCRT_wenviron instead */
|
---|
91 | FIXME( "memory leak\n" );
|
---|
92 | return pp;
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|
96 | FreeEnvironmentStringsW( environ );
|
---|
97 | return NULL;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /*********************************************************************
|
---|
101 | * _putenv (MSVCRT.@)
|
---|
102 | */
|
---|
103 | int MSVCRT__putenv(const char *str)
|
---|
104 | {
|
---|
105 | char name[256], value[512];
|
---|
106 | char *dst = name;
|
---|
107 | int ret;
|
---|
108 |
|
---|
109 | TRACE("%s\n", str);
|
---|
110 |
|
---|
111 | if (!str)
|
---|
112 | return -1;
|
---|
113 | while (*str && *str != '=')
|
---|
114 | *dst++ = *str++;
|
---|
115 | if (!*str++)
|
---|
116 | return -1;
|
---|
117 | *dst = '\0';
|
---|
118 | dst = value;
|
---|
119 | while (*str)
|
---|
120 | *dst++ = *str++;
|
---|
121 | *dst = '\0';
|
---|
122 |
|
---|
123 | ret = !SetEnvironmentVariableA(name, value[0] ? value : NULL);
|
---|
124 | /* Update the __p__environ array only when already initialized */
|
---|
125 | if (MSVCRT__environ)
|
---|
126 | MSVCRT__environ = msvcrt_SnapshotOfEnvironmentA(MSVCRT__environ);
|
---|
127 | if (MSVCRT__wenviron)
|
---|
128 | MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(MSVCRT__wenviron);
|
---|
129 | return ret;
|
---|
130 | }
|
---|
131 |
|
---|
132 | /*********************************************************************
|
---|
133 | * _wputenv (MSVCRT.@)
|
---|
134 | */
|
---|
135 | int _wputenv(const MSVCRT_wchar_t *str)
|
---|
136 | {
|
---|
137 | MSVCRT_wchar_t name[256], value[512];
|
---|
138 | MSVCRT_wchar_t *dst = name;
|
---|
139 | int ret;
|
---|
140 |
|
---|
141 | TRACE("%s\n", debugstr_w(str));
|
---|
142 |
|
---|
143 | if (!str)
|
---|
144 | return -1;
|
---|
145 | while (*str && *str != '=')
|
---|
146 | *dst++ = *str++;
|
---|
147 | if (!*str++)
|
---|
148 | return -1;
|
---|
149 | *dst = 0;
|
---|
150 | dst = value;
|
---|
151 | while (*str)
|
---|
152 | *dst++ = *str++;
|
---|
153 | *dst = 0;
|
---|
154 |
|
---|
155 | ret = !SetEnvironmentVariableW(name, value[0] ? value : NULL);
|
---|
156 | /* Update the __p__environ array only when already initialized */
|
---|
157 | if (MSVCRT__environ)
|
---|
158 | MSVCRT__environ = msvcrt_SnapshotOfEnvironmentA(MSVCRT__environ);
|
---|
159 | if (MSVCRT__wenviron)
|
---|
160 | MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(MSVCRT__wenviron);
|
---|
161 | return ret;
|
---|
162 | }
|
---|