Various cosmetic changes.
[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 #include "msvcrt/stdlib.h"
13
14
15 #include "wine/debug.h"
16
17 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
18
19 /*********************************************************************
20  *              getenv (MSVCRT.@)
21  */
22 char *MSVCRT_getenv(const char *name)
23 {
24   char *environ = GetEnvironmentStringsA();
25   char *pp,*pos = NULL;
26   unsigned int length=strlen(name);
27
28   for (pp = environ; (*pp); pp = pp + strlen(pp) +1)
29   {
30       pos =strchr(pp,'=');
31       if ((pos) && ((pos - pp) == length))
32       {
33           if (!strncasecmp(pp,name,length)) break;
34       }
35   }
36   if ((*pp)&& (pos))
37   {
38      pp = pos+1;
39      TRACE("got %s\n",pp);
40   }
41   else
42     pp = 0;
43   FreeEnvironmentStringsA( environ );
44   return pp;
45 }
46
47 /*********************************************************************
48  *              _wgetenv (MSVCRT.@)
49  */
50 WCHAR *_wgetenv(const WCHAR *name)
51 {
52   WCHAR* environ = GetEnvironmentStringsW();
53   WCHAR* pp,*pos = NULL;
54   unsigned int length=strlenW(name);
55
56   for (pp = environ; (*pp); pp = pp + strlenW(pp) + 1)
57   {
58       pos = strchrW(pp,'=');
59       if ((pos) && ((pos - pp) == length))
60       {
61           if (!strncmpiW(pp,name,length))
62           {
63               pp = pos+1;
64               TRACE("got %s\n",debugstr_w(pp));
65               /* can't free pointer since we are returning it */
66               /* should probably use MSVCRT_wenviron instead */
67               FIXME( "memory leak\n" );
68               return pp;
69           }
70       }
71   }
72   FreeEnvironmentStringsW( environ );
73   return NULL;
74 }
75
76 /*********************************************************************
77  *              _putenv (MSVCRT.@)
78  */
79 int _putenv(const char *str)
80 {
81  char name[256], value[512];
82  char *dst = name;
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  return !SetEnvironmentVariableA(name, value[0] ? value : NULL);
99 }
100
101 /*********************************************************************
102  *              _wputenv (MSVCRT.@)
103  */
104 int _wputenv(const WCHAR *str)
105 {
106  WCHAR name[256], value[512];
107  WCHAR *dst = name;
108
109  TRACE("%s\n", debugstr_w(str));
110
111  if (!str)
112    return -1;
113  while (*str && *str != (WCHAR)L'=')
114   *dst++ = *str++;
115  if (!*str++)
116    return -1;
117  *dst = (WCHAR)L'\0';
118  dst = value;
119  while (*str)
120   *dst++ = *str++;
121  *dst = (WCHAR)L'\0';
122
123  return !SetEnvironmentVariableW(name, value[0] ? value : NULL);
124 }