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