Implemented ILGetDisplayNameExA and ILGetDisplayNameExW and call them
[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 = GetEnvironmentStringsA();
39   char *pp,*pos = NULL;
40   unsigned int length=strlen(name);
41
42   for (pp = environ; (*pp); pp = pp + strlen(pp) +1)
43   {
44       pos =strchr(pp,'=');
45       if ((pos) && ((pos - pp) == length))
46       {
47           if (!strncasecmp(pp,name,length)) break;
48       }
49   }
50   if ((*pp)&& (pos))
51   {
52      pp = pos+1;
53      TRACE("got %s\n",pp);
54   }
55   else
56     pp = 0;
57   FreeEnvironmentStringsA( environ );
58   return pp;
59 }
60
61 /*********************************************************************
62  *              _wgetenv (MSVCRT.@)
63  */
64 MSVCRT_wchar_t *_wgetenv(const MSVCRT_wchar_t *name)
65 {
66   MSVCRT_wchar_t* environ = GetEnvironmentStringsW();
67   MSVCRT_wchar_t* pp,*pos = NULL;
68   unsigned int length=strlenW(name);
69
70   for (pp = environ; (*pp); pp = pp + strlenW(pp) + 1)
71   {
72       pos = strchrW(pp,'=');
73       if ((pos) && ((pos - pp) == length))
74       {
75           if (!strncmpiW(pp,name,length))
76           {
77               pp = pos+1;
78               TRACE("got %s\n",debugstr_w(pp));
79               /* can't free pointer since we are returning it */
80               /* should probably use MSVCRT_wenviron instead */
81               FIXME( "memory leak\n" );
82               return pp;
83           }
84       }
85   }
86   FreeEnvironmentStringsW( environ );
87   return NULL;
88 }
89
90 /*********************************************************************
91  *              _putenv (MSVCRT.@)
92  */
93 int _putenv(const char *str)
94 {
95  char name[256], value[512];
96  char *dst = name;
97  int ret;
98
99  TRACE("%s\n", str);
100
101  if (!str)
102    return -1;
103  while (*str && *str != '=')
104   *dst++ = *str++;
105  if (!*str++)
106    return -1;
107  *dst = '\0';
108  dst = value;
109  while (*str)
110   *dst++ = *str++;
111  *dst = '\0';
112
113  ret = !SetEnvironmentVariableA(name, value[0] ? value : NULL);
114  /* Update the __p__environ array only when already initialized */
115  if (MSVCRT__environ)
116    MSVCRT__environ = msvcrt_SnapshotOfEnvironmentA(MSVCRT__environ);
117  if (MSVCRT__wenviron)
118    MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(MSVCRT__wenviron);
119  return ret;
120 }
121
122 /*********************************************************************
123  *              _wputenv (MSVCRT.@)
124  */
125 int _wputenv(const MSVCRT_wchar_t *str)
126 {
127  MSVCRT_wchar_t name[256], value[512];
128  MSVCRT_wchar_t *dst = name;
129  int ret;
130
131  TRACE("%s\n", debugstr_w(str));
132
133  if (!str)
134    return -1;
135  while (*str && *str != '=')
136   *dst++ = *str++;
137  if (!*str++)
138    return -1;
139  *dst = 0;
140  dst = value;
141  while (*str)
142   *dst++ = *str++;
143  *dst = 0;
144
145  ret = !SetEnvironmentVariableW(name, value[0] ? value : NULL);
146  /* Update the __p__environ array only when already initialized */
147  if (MSVCRT__environ)
148    MSVCRT__environ = msvcrt_SnapshotOfEnvironmentA(MSVCRT__environ);
149  if (MSVCRT__wenviron)
150    MSVCRT__wenviron = msvcrt_SnapshotOfEnvironmentW(MSVCRT__wenviron);
151  return ret;
152 }