- Remove unnecessary line from FreeEnvironmentStringsW.
[reactos.git] / reactos / dll / win32 / kernel32 / misc / env.c
index 150c54f..b132abf 100644 (file)
@@ -12,7 +12,7 @@
 #include <k32.h>
 
 #define NDEBUG
-#include "../include/debug.h"
+#include <debug.h>
 
 
 /* FUNCTIONS ******************************************************************/
@@ -21,7 +21,7 @@
  * @implemented
  */
 DWORD
-STDCALL
+WINAPI
 GetEnvironmentVariableA (
        LPCSTR  lpName,
        LPSTR   lpBuffer,
@@ -43,14 +43,14 @@ GetEnvironmentVariableA (
 
        /* initialize ansi variable value string */
        VarValue.Length = 0;
-       VarValue.MaximumLength = nSize;
+       VarValue.MaximumLength = (USHORT)nSize;
        VarValue.Buffer = lpBuffer;
 
        /* initialize unicode variable value string and allocate buffer */
        VarValueU.Length = 0;
        if (nSize != 0)
        {
-           VarValueU.MaximumLength = (nSize - 1) * sizeof(WCHAR);
+           VarValueU.MaximumLength = (USHORT)(nSize - 1) * sizeof(WCHAR);
            VarValueU.Buffer = RtlAllocateHeap (RtlGetProcessHeap (),
                                                0,
                                                nSize * sizeof(WCHAR));
@@ -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 = (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
        )
@@ -298,10 +308,10 @@ GetEnvironmentStringsA (
        DPRINT("EnvPtr %p\n", EnvPtr);
 
        /* convert unicode environment to ansi */
-       UnicodeString.MaximumLength = Length * sizeof(WCHAR) + sizeof(WCHAR);
+       UnicodeString.MaximumLength = (USHORT)Length * sizeof(WCHAR) + sizeof(WCHAR);
        UnicodeString.Buffer = EnvU;
 
-       AnsiString.MaximumLength = Length + 1;
+       AnsiString.MaximumLength = (USHORT)Length + 1;
        AnsiString.Length = 0;
        AnsiString.Buffer = EnvPtr;
 
@@ -314,7 +324,7 @@ GetEnvironmentStringsA (
                if (UnicodeString.Length > 0)
                {
                        AnsiString.Length = 0;
-                       AnsiString.MaximumLength = Length + 1 - (AnsiString.Buffer - EnvPtr);
+                       AnsiString.MaximumLength = (USHORT)Length + 1 - (AnsiString.Buffer - EnvPtr);
 
                        RtlUnicodeStringToAnsiString (&AnsiString,
                                                      &UnicodeString,
@@ -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,12 +415,16 @@ ExpandEnvironmentStringsA (
             return 0;
         }
 
+    /* make sure we don't overflow the maximum ANSI_STRING size */
+    if (nSize > 0x7fff)
+        nSize = 0x7fff;
+
        Destination.Length = 0;
-       Destination.MaximumLength = nSize;
+       Destination.MaximumLength = (USHORT)nSize;
        Destination.Buffer = lpDst;
 
        DestinationU.Length = 0;
-       DestinationU.MaximumLength = nSize * sizeof(WCHAR);
+       DestinationU.MaximumLength = (USHORT)nSize * sizeof(WCHAR);
        DestinationU.Buffer = RtlAllocateHeap (RtlGetProcessHeap (),
                                               0,
                                               DestinationU.MaximumLength);
@@ -457,7 +470,7 @@ ExpandEnvironmentStringsA (
  * @implemented
  */
 DWORD
-STDCALL
+WINAPI
 ExpandEnvironmentStringsW (
        LPCWSTR lpSrc,
        LPWSTR  lpDst,
@@ -472,8 +485,12 @@ 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 = nSize * sizeof(WCHAR);
+       Destination.MaximumLength = (USHORT)nSize * sizeof(WCHAR);
        Destination.Buffer = lpDst;
 
        Status = RtlExpandEnvironmentStrings_U (NULL,