Correct return value in a couple of error cases.
[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  * 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 #include "wine/unicode.h"
24 #include "msvcrt.h"
25
26 #include "msvcrt/stdlib.h"
27
28
29 #include "wine/debug.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
32
33 /*********************************************************************
34  *              getenv (MSVCRT.@)
35  */
36 char *MSVCRT_getenv(const char *name)
37 {
38     char **environ;
39     unsigned int length=strlen(name);
40
41     for (environ = *__p__environ(); *environ; environ++)
42     {
43         char *str = *environ;
44         char *pos = strchr(str,'=');
45         if (pos && ((pos - str) == length) && !strncasecmp(str,name,length))
46         {
47             TRACE("(%s): got %s\n", debugstr_a(name), debugstr_a(pos + 1));
48             return pos + 1;
49         }
50     }
51     return NULL;
52 }
53
54 /*********************************************************************
55  *              _wgetenv (MSVCRT.@)
56  */
57 MSVCRT_wchar_t *_wgetenv(const MSVCRT_wchar_t *name)
58 {
59     MSVCRT_wchar_t **environ;
60     unsigned int length=strlenW(name);
61
62     for (environ = *__p__wenviron(); *environ; environ++)
63     {
64         MSVCRT_wchar_t *str = *environ;
65         MSVCRT_wchar_t *pos = strchrW(str,'=');
66         if (pos && ((pos - str) == length) && !strncmpiW(str,name,length))
67         {
68             TRACE("(%s): got %s\n", debugstr_w(name), debugstr_w(pos + 1));
69             return pos + 1;
70         }
71     }
72     return NULL;
73 }
74
75 /*********************************************************************
76  *              _putenv (MSVCRT.@)
77  */
78 int _putenv(const char *str)
79 {
80  char name[256], value[512];
81  char *dst = name;
82  int ret;
83
84  TRACE("%s\n", str);
85
86  if (!str)
87    return -1;
88  while (*str && *str != '=')
89   *dst++ = *str++;
90  if (!*str++)
91    return -1;
92  *dst = '\0';
93  dst = value;
94  while (*str)
95   *dst++ = *str++;
96  *dst = '\0';
97
98  ret = !SetEnvironmentVariableA(name, value[0] ? value : NULL);
99  /* Update the __p__environ array only when already initialized */
100  if (MSVCRT__environ)
101    MSVCRT__environ = msvcrt_SnapshotOfEnvironmentA(MSVCRT__environ);
102  if (MSVCRT__wenviron)
103    MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(MSVCRT__wenviron);
104  return ret;
105 }
106
107 /*********************************************************************
108  *              _wputenv (MSVCRT.@)
109  */
110 int _wputenv(const MSVCRT_wchar_t *str)
111 {
112  MSVCRT_wchar_t name[256], value[512];
113  MSVCRT_wchar_t *dst = name;
114  int ret;
115
116  TRACE("%s\n", debugstr_w(str));
117
118  if (!str)
119    return -1;
120  while (*str && *str != '=')
121   *dst++ = *str++;
122  if (!*str++)
123    return -1;
124  *dst = 0;
125  dst = value;
126  while (*str)
127   *dst++ = *str++;
128  *dst = 0;
129
130  ret = !SetEnvironmentVariableW(name, value[0] ? value : NULL);
131  /* Update the __p__environ array only when already initialized */
132  if (MSVCRT__environ)
133    MSVCRT__environ = msvcrt_SnapshotOfEnvironmentA(MSVCRT__environ);
134  if (MSVCRT__wenviron)
135    MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(MSVCRT__wenviron);
136  return ret;
137 }