environ.c 3.85 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 21
 *
 * 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
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 *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 *_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 _putenv(const char *str)
75 76 77
{
 char name[256], value[512];
 char *dst = name;
78
 int ret;
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

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

 if (!str)
   return -1;
 while (*str && *str != '=')
  *dst++ = *str++;
 if (!*str++)
   return -1;
 *dst = '\0';
 dst = value;
 while (*str)
  *dst++ = *str++;
 *dst = '\0';

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

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

99
 /* Update the __p__environ array only when already initialized */
100 101 102 103
 if (_environ)
   _environ = msvcrt_SnapshotOfEnvironmentA(_environ);
 if (_wenviron)
   _wenviron = msvcrt_SnapshotOfEnvironmentW(_wenviron);
104
 return ret;
105 106 107 108 109
}

/*********************************************************************
 *		_wputenv (MSVCRT.@)
 */
110
int _wputenv(const MSVCRT_wchar_t *str)
111
{
112 113
 MSVCRT_wchar_t name[256], value[512];
 MSVCRT_wchar_t *dst = name;
114
 int ret;
115 116 117 118 119

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

 if (!str)
   return -1;
120
 while (*str && *str != '=')
121 122 123
  *dst++ = *str++;
 if (!*str++)
   return -1;
124
 *dst = 0;
125 126 127
 dst = value;
 while (*str)
  *dst++ = *str++;
128
 *dst = 0;
129

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

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

135
 /* Update the __p__environ array only when already initialized */
136 137 138 139
 if (_environ)
   _environ = msvcrt_SnapshotOfEnvironmentA(_environ);
 if (_wenviron)
   _wenviron = msvcrt_SnapshotOfEnvironmentW(_wenviron);
140
 return ret;
141
}