Updated.
[wine] / dlls / msvcrt / environ.c
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 #include "wine/unicode.h"
10 #include "msvcrt.h"
11
12 DEFAULT_DEBUG_CHANNEL(msvcrt);
13
14 LPWSTR __cdecl wcsrchr( LPWSTR str, WCHAR ch );
15
16 /*********************************************************************
17  *              getenv (MSVCRT.@)
18  */
19 char *__cdecl MSVCRT_getenv(const char *name)
20 {
21   char *environ = GetEnvironmentStringsA();
22   char *pp,*pos = NULL;
23   unsigned int length;
24
25   for (pp = environ; (*pp); pp = pp + strlen(pp) +1)
26   {
27     pos =strchr(pp,'=');
28     if (pos)
29       length = pos -pp;
30     else
31       length = strlen(pp);
32     if (!strncmp(pp,name,length)) break;
33   }
34   if ((pp)&& (pos))
35   {
36      pp = pos+1;
37      TRACE("got %s\n",pp);
38   }
39   FreeEnvironmentStringsA( environ );
40   return pp;
41 }
42
43 /*********************************************************************
44  *              _wgetenv (MSVCRT.@)
45  */
46 WCHAR *__cdecl MSVCRT__wgetenv(const WCHAR *name)
47 {
48   WCHAR* environ = GetEnvironmentStringsW();
49   WCHAR* pp,*pos = NULL;
50   unsigned int length;
51
52   for (pp = environ; (*pp); pp = pp + strlenW(pp) + 1)
53   {
54     pos =wcsrchr(pp,'=');
55     if (pos)
56       length = pos -pp;
57     else
58       length = strlenW(pp);
59     if (!strncmpW(pp,name,length)) break;
60   }
61   if ((pp)&& (pos))
62   {
63      pp = pos+1;
64      TRACE("got %s\n",debugstr_w(pp));
65   }
66   FreeEnvironmentStringsW( environ );
67   return pp;
68 }
69
70 /*********************************************************************
71  *              _putenv (MSVCRT.@)
72  */
73 int __cdecl MSVCRT__putenv(const char *str)
74 {
75  char name[256], value[512];
76  char *dst = name;
77
78  TRACE("%s\n", str);
79
80  if (!str)
81    return -1;
82  while (*str && *str != '=')
83   *dst++ = *str++;
84  if (!*str++)
85    return -1;
86  *dst = '\0';
87  dst = value;
88  while (*str)
89   *dst++ = *str++;
90  *dst = '\0';
91
92  return !SetEnvironmentVariableA(name, value[0] ? value : NULL);
93 }
94
95 /*********************************************************************
96  *              _wputenv (MSVCRT.@)
97  */
98 int __cdecl MSVCRT__wputenv(const WCHAR *str)
99 {
100  WCHAR name[256], value[512];
101  WCHAR *dst = name;
102
103  TRACE("%s\n", debugstr_w(str));
104
105  if (!str)
106    return -1;
107  while (*str && *str != (WCHAR)L'=')
108   *dst++ = *str++;
109  if (!*str++)
110    return -1;
111  *dst = (WCHAR)L'\0';
112  dst = value;
113  while (*str)
114   *dst++ = *str++;
115  *dst = (WCHAR)L'\0';
116
117  return !SetEnvironmentVariableW(name, value[0] ? value : NULL);
118 }