environ.c 4.26 KB
Newer Older
1 2 3 4 5 6 7
/*
 * msvcrt.dll environment functions
 *
 * Copyright 1996,1998 Marcus Meissner
 * Copyright 1996 Jukka Iivonen
 * Copyright 1997,2000 Uwe Bonnes
 * Copyright 2000 Jon Griffiths
8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
21
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 23 24
 */
#include "wine/unicode.h"
#include "msvcrt.h"
25 26 27
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
28 29 30 31

/*********************************************************************
 *		getenv (MSVCRT.@)
 */
32
char * CDECL MSVCRT_getenv(const char *name)
33
{
34 35
    char **environ;
    unsigned int length=strlen(name);
36

37 38 39 40 41 42 43 44 45 46 47
    for (environ = *__p__environ(); *environ; environ++)
    {
        char *str = *environ;
        char *pos = strchr(str,'=');
        if (pos && ((pos - str) == length) && !strncasecmp(str,name,length))
        {
            TRACE("(%s): got %s\n", debugstr_a(name), debugstr_a(pos + 1));
            return pos + 1;
        }
    }
    return NULL;
48 49 50 51 52
}

/*********************************************************************
 *		_wgetenv (MSVCRT.@)
 */
53
MSVCRT_wchar_t * CDECL _wgetenv(const MSVCRT_wchar_t *name)
54
{
55 56
    MSVCRT_wchar_t **environ;
    unsigned int length=strlenW(name);
57

58 59 60 61 62 63 64 65 66 67 68
    for (environ = *__p__wenviron(); *environ; environ++)
    {
        MSVCRT_wchar_t *str = *environ;
        MSVCRT_wchar_t *pos = strchrW(str,'=');
        if (pos && ((pos - str) == length) && !strncmpiW(str,name,length))
        {
            TRACE("(%s): got %s\n", debugstr_w(name), debugstr_w(pos + 1));
            return pos + 1;
        }
    }
    return NULL;
69
}
70 71 72 73

/*********************************************************************
 *		_putenv (MSVCRT.@)
 */
74
int CDECL _putenv(const char *str)
75
{
76 77
 char *name, *value;
 char *dst;
78
 int ret;
79

80
 TRACE("%s\n", debugstr_a(str));
81 82 83

 if (!str)
   return -1;
84 85 86 87 88
   
 name = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
 if (!name)
   return -1;
 dst = name;
89 90 91
 while (*str && *str != '=')
  *dst++ = *str++;
 if (!*str++)
92 93 94 95 96 97
 {
   ret = -1;
   goto finish;
 }
 *dst++ = '\0';
 value = dst;
98 99 100 101
 while (*str)
  *dst++ = *str++;
 *dst = '\0';

102 103
 ret = SetEnvironmentVariableA(name, value[0] ? value : NULL) ? 0 : -1;

104
 /* _putenv returns success on deletion of nonexistent variable, unlike [Rtl]SetEnvironmentVariable */
105 106
 if ((ret == -1) && (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) ret = 0;

107
 /* Update the __p__environ array only when already initialized */
108 109
 if (MSVCRT__environ)
   MSVCRT__environ = msvcrt_SnapshotOfEnvironmentA(MSVCRT__environ);
110 111
 if (_wenviron)
   _wenviron = msvcrt_SnapshotOfEnvironmentW(_wenviron);
112 113 114
   
finish:
 HeapFree(GetProcessHeap(), 0, name);
115
 return ret;
116 117 118 119 120
}

/*********************************************************************
 *		_wputenv (MSVCRT.@)
 */
121
int CDECL _wputenv(const MSVCRT_wchar_t *str)
122
{
123 124
 MSVCRT_wchar_t *name, *value;
 MSVCRT_wchar_t *dst;
125
 int ret;
126 127 128 129 130

 TRACE("%s\n", debugstr_w(str));

 if (!str)
   return -1;
131 132 133 134
 name = HeapAlloc(GetProcessHeap(), 0, (strlenW(str) + 1) * sizeof(MSVCRT_wchar_t));
 if (!name)
   return -1;
 dst = name;
135
 while (*str && *str != '=')
136 137
  *dst++ = *str++;
 if (!*str++)
138 139 140 141 142 143
 {
   ret = -1;
   goto finish;
 }
 *dst++ = 0;
 value = dst;
144 145
 while (*str)
  *dst++ = *str++;
146
 *dst = 0;
147

148 149
 ret = SetEnvironmentVariableW(name, value[0] ? value : NULL) ? 0 : -1;

150
 /* _putenv returns success on deletion of nonexistent variable, unlike [Rtl]SetEnvironmentVariable */
151 152
 if ((ret == -1) && (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) ret = 0;

153
 /* Update the __p__environ array only when already initialized */
154 155
 if (MSVCRT__environ)
   MSVCRT__environ = msvcrt_SnapshotOfEnvironmentA(MSVCRT__environ);
156 157
 if (_wenviron)
   _wenviron = msvcrt_SnapshotOfEnvironmentW(_wenviron);
158 159 160

finish:
 HeapFree(GetProcessHeap(), 0, name);
161
 return ret;
162
}