MSVCRT_getenv: compare for the length of the key and return NULL in
[wine/wine-kai.git] / dlls / msvcrt / environ.c
blob58cf5fbcf48c9de402be70321624ea5eeb56b6c0
1 /*
2 * msvcrt.dll environment functions
4 * Copyright 1996,1998 Marcus Meissner
5 * Copyright 1996 Jukka Iivonen
6 * Copyright 1997,2000 Uwe Bonnes
7 * Copyright 2000 Jon Griffiths
8 */
9 #include "wine/unicode.h"
10 #include "msvcrt.h"
12 #include "msvcrt/stdlib.h"
15 DEFAULT_DEBUG_CHANNEL(msvcrt);
17 /*********************************************************************
18 * getenv (MSVCRT.@)
20 char *MSVCRT_getenv(const char *name)
22 char *environ = GetEnvironmentStringsA();
23 char *pp,*pos = NULL;
24 unsigned int length=strlen(name);
26 for (pp = environ; (*pp); pp = pp + strlen(pp) +1)
28 pos =strchr(pp,'=');
29 if ((pos) && ((pos - pp) == length))
31 if (!strncmp(pp,name,length)) break;
34 if ((*pp)&& (pos))
36 pp = pos+1;
37 TRACE("got %s\n",pp);
39 else
40 pp = 0;
41 FreeEnvironmentStringsA( environ );
42 return pp;
45 /*********************************************************************
46 * _wgetenv (MSVCRT.@)
48 WCHAR *_wgetenv(const WCHAR *name)
50 WCHAR* environ = GetEnvironmentStringsW();
51 WCHAR* pp,*pos = NULL;
52 unsigned int length=strlenW(name);
54 for (pp = environ; (*pp); pp = pp + strlenW(pp) + 1)
56 pos = strchrW(pp,'=');
57 if ((pos) && ((pos - pp) == length))
59 if (!strncmpW(pp,name,length))
61 pp = pos+1;
62 TRACE("got %s\n",debugstr_w(pp));
63 /* can't free pointer since we are returning it */
64 /* should probably use MSVCRT_wenviron instead */
65 FIXME( "memory leak\n" );
66 return pp;
70 FreeEnvironmentStringsW( environ );
71 return NULL;
74 /*********************************************************************
75 * _putenv (MSVCRT.@)
77 int _putenv(const char *str)
79 char name[256], value[512];
80 char *dst = name;
82 TRACE("%s\n", str);
84 if (!str)
85 return -1;
86 while (*str && *str != '=')
87 *dst++ = *str++;
88 if (!*str++)
89 return -1;
90 *dst = '\0';
91 dst = value;
92 while (*str)
93 *dst++ = *str++;
94 *dst = '\0';
96 return !SetEnvironmentVariableA(name, value[0] ? value : NULL);
99 /*********************************************************************
100 * _wputenv (MSVCRT.@)
102 int _wputenv(const WCHAR *str)
104 WCHAR name[256], value[512];
105 WCHAR *dst = name;
107 TRACE("%s\n", debugstr_w(str));
109 if (!str)
110 return -1;
111 while (*str && *str != (WCHAR)L'=')
112 *dst++ = *str++;
113 if (!*str++)
114 return -1;
115 *dst = (WCHAR)L'\0';
116 dst = value;
117 while (*str)
118 *dst++ = *str++;
119 *dst = (WCHAR)L'\0';
121 return !SetEnvironmentVariableW(name, value[0] ? value : NULL);