- Remove unnecessary line from FreeEnvironmentStringsW.
[reactos.git] / reactos / dll / win32 / kernel32 / misc / env.c
index b5aa8c5..b132abf 100644 (file)
@@ -21,7 +21,7 @@
  * @implemented
  */
 DWORD
-STDCALL
+WINAPI
 GetEnvironmentVariableA (
        LPCSTR  lpName,
        LPSTR   lpBuffer,
@@ -124,7 +124,7 @@ GetEnvironmentVariableA (
  * @implemented
  */
 DWORD
-STDCALL
+WINAPI
 GetEnvironmentVariableW (
        LPCWSTR lpName,
        LPWSTR  lpBuffer,
@@ -139,7 +139,7 @@ GetEnvironmentVariableW (
                              lpName);
 
        VarValue.Length = 0;
-       VarValue.MaximumLength = (USHORT)(nSize != 0 ? (nSize - 1) * sizeof(WCHAR) : 0);
+       VarValue.MaximumLength = (USHORT) (nSize ? nSize - 1 : 0) * sizeof(WCHAR);
        VarValue.Buffer = lpBuffer;
 
        Status = RtlQueryEnvironmentVariable_U (NULL,
@@ -147,13 +147,13 @@ GetEnvironmentVariableW (
                                                &VarValue);
        if (!NT_SUCCESS(Status))
        {
-               SetLastErrorByStatus (Status);
                if (Status == STATUS_BUFFER_TOO_SMALL)
                {
                        return (VarValue.Length / sizeof(WCHAR)) + 1;
                }
                else
                {
+                       SetLastErrorByStatus (Status);
                        return 0;
                }
        }
@@ -162,7 +162,7 @@ GetEnvironmentVariableW (
         {
             /* make sure the string is NULL-terminated! RtlQueryEnvironmentVariable_U
                only terminates it if MaximumLength < Length */
-           VarValue.Buffer[VarValue.Length / sizeof(WCHAR)] = L'\0';
+           lpBuffer[VarValue.Length / sizeof(WCHAR)] = L'\0';
        }
 
        return (VarValue.Length / sizeof(WCHAR));
@@ -173,7 +173,7 @@ GetEnvironmentVariableW (
  * @implemented
  */
 BOOL
-STDCALL
+WINAPI
 SetEnvironmentVariableA (
        LPCSTR  lpName,
        LPCSTR  lpValue
@@ -193,18 +193,27 @@ SetEnvironmentVariableA (
                                      &VarName,
                                      TRUE);
 
-       RtlInitAnsiString (&VarValue,
-                          (LPSTR)lpValue);
-       RtlAnsiStringToUnicodeString (&VarValueU,
-                                     &VarValue,
-                                     TRUE);
+       if (lpValue)
+       {
+               RtlInitAnsiString (&VarValue,
+                                  (LPSTR)lpValue);
+               RtlAnsiStringToUnicodeString (&VarValueU,
+                                             &VarValue,
+                                             TRUE);
 
-       Status = RtlSetEnvironmentVariable (NULL,
-                                           &VarNameU,
-                                           &VarValueU);
+               Status = RtlSetEnvironmentVariable (NULL,
+                                                   &VarNameU,
+                                                   &VarValueU);
 
+               RtlFreeUnicodeString (&VarValueU);
+       }
+       else
+       {
+               Status = RtlSetEnvironmentVariable (NULL,
+                                                   &VarNameU,
+                                                   NULL);
+       }
        RtlFreeUnicodeString (&VarNameU);
-       RtlFreeUnicodeString (&VarValueU);
 
        if (!NT_SUCCESS(Status))
        {
@@ -220,7 +229,7 @@ SetEnvironmentVariableA (
  * @implemented
  */
 BOOL
-STDCALL
+WINAPI
 SetEnvironmentVariableW (
        LPCWSTR lpName,
        LPCWSTR lpValue
@@ -241,6 +250,7 @@ SetEnvironmentVariableW (
        Status = RtlSetEnvironmentVariable (NULL,
                                            &VarName,
                                            &VarValue);
+
        if (!NT_SUCCESS(Status))
        {
                SetLastErrorByStatus (Status);
@@ -255,7 +265,7 @@ SetEnvironmentVariableW (
  * @implemented
  */
 LPSTR
-STDCALL
+WINAPI
 GetEnvironmentStringsA (
        VOID
        )
@@ -334,7 +344,7 @@ GetEnvironmentStringsA (
  * @implemented
  */
 LPWSTR
-STDCALL
+WINAPI
 GetEnvironmentStringsW (
        VOID
        )
@@ -347,7 +357,7 @@ GetEnvironmentStringsW (
  * @implemented
  */
 BOOL
-STDCALL
+WINAPI
 FreeEnvironmentStringsA (
        LPSTR   EnvironmentStrings
        )
@@ -367,12 +377,11 @@ FreeEnvironmentStringsA (
  * @implemented
  */
 BOOL
-STDCALL
+WINAPI
 FreeEnvironmentStringsW (
        LPWSTR  EnvironmentStrings
        )
 {
- (void)EnvironmentStrings;
  return TRUE;
 }
 
@@ -381,7 +390,7 @@ FreeEnvironmentStringsW (
  * @implemented
  */
 DWORD
-STDCALL
+WINAPI
 ExpandEnvironmentStringsA (
        LPCSTR  lpSrc,
        LPSTR   lpDst,
@@ -406,6 +415,10 @@ ExpandEnvironmentStringsA (
             return 0;
         }
 
+    /* make sure we don't overflow the maximum ANSI_STRING size */
+    if (nSize > 0x7fff)
+        nSize = 0x7fff;
+
        Destination.Length = 0;
        Destination.MaximumLength = (USHORT)nSize;
        Destination.Buffer = lpDst;
@@ -457,7 +470,7 @@ ExpandEnvironmentStringsA (
  * @implemented
  */
 DWORD
-STDCALL
+WINAPI
 ExpandEnvironmentStringsW (
        LPCWSTR lpSrc,
        LPWSTR  lpDst,
@@ -472,6 +485,10 @@ ExpandEnvironmentStringsW (
        RtlInitUnicodeString (&Source,
                              (LPWSTR)lpSrc);
 
+    /* make sure we don't overflow the maximum UNICODE_STRING size */
+    if (nSize > 0x7fff)
+        nSize = 0x7fff;
+
        Destination.Length = 0;
        Destination.MaximumLength = (USHORT)nSize * sizeof(WCHAR);
        Destination.Buffer = lpDst;