ce68245ba8743cff33d809316c89dc81ac9beb31
[reactos.git] / lib / msvcrt / stdlib / putenv.c
1 #include <windows.h>
2 #include <msvcrt/stdlib.h>
3 #include <msvcrt/string.h>
4
5 #define NDEBUG
6 #include <msvcrt/msvcrtdbg.h>
7
8
9 extern int BlockEnvToEnviron(); // defined in misc/dllmain.c
10
11 int _putenv(const char *val)
12 {
13   char *buffer;
14   char *epos;
15   int res;
16
17   DPRINT("_putenv('%s')\n", val);
18   epos = strchr(val, '=');
19   if ( epos == NULL )
20         return -1;
21   buffer = (char*)malloc(epos - val + 1);
22   if (buffer == NULL)
23         return -1;
24   strncpy(buffer, val, epos - val);
25   buffer[epos - val] = 0;
26   res = SetEnvironmentVariableA(buffer,epos+1);
27   free(buffer);
28   if (BlockEnvToEnviron()) return 0;
29   return  res;
30 }
31
32 int _wputenv(const wchar_t *val)
33 {
34   wchar_t *buffer;
35   wchar_t *epos;
36   int res;
37
38   DPRINT("_wputenv('%S')\n", val);
39   epos = wcsrchr(val, L'=');
40   if ( epos == NULL )
41         return -1;
42   buffer = (char*)malloc((epos - val + 1) * sizeof (wchar_t));
43   if (buffer == NULL)
44         return -1;
45   wcsncpy(buffer, val, epos - val);
46   buffer[epos - val] = 0;
47   res = SetEnvironmentVariableW(buffer,epos+1);
48   free(buffer);
49   if (BlockEnvToEnviron() ) return 0;
50   return  res;
51 }