[CRT]
authorAmine Khaldi <amine.khaldi@reactos.org>
Mon, 12 May 2014 20:10:38 +0000 (20:10 +0000)
committerAmine Khaldi <amine.khaldi@reactos.org>
Mon, 12 May 2014 20:10:38 +0000 (20:10 +0000)
* Reorder some functions. No code changes!
CORE-8080

svn path=/trunk/; revision=63267

reactos/lib/sdk/crt/stdio/file.c

index 401979c..0c7533e 100644 (file)
@@ -1657,24 +1657,20 @@ static int check_bom(HANDLE h, int oflags, BOOL seek)
 }
 
 /*********************************************************************
- *              _sopen_s (MSVCRT.@)
+ *              _wsopen_s (MSVCRT.@)
  */
-int CDECL _sopen_s( int *fd, const char *path, int oflags, int shflags, int pmode )
+int CDECL _wsopen_s( int *fd, const wchar_t* path, int oflags, int shflags, int pmode )
 {
   DWORD access = 0, creation = 0, attrib;
+  SECURITY_ATTRIBUTES sa;
   DWORD sharing;
   int wxflag;
   HANDLE hand;
-  SECURITY_ATTRIBUTES sa;
 
-  TRACE("fd*: %p file: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
-        fd, path, oflags, shflags, pmode);
+  TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
+        fd, debugstr_w(path), oflags, shflags, pmode);
 
-  if (!fd)
-  {
-    MSVCRT_INVALID_PMT("null out fd pointer", EINVAL);
-    return EINVAL;
-  }
+  if (!MSVCRT_CHECK_PMT( fd != NULL )) return EINVAL;
 
   *fd = -1;
   wxflag = split_oflags(oflags);
@@ -1736,25 +1732,89 @@ int CDECL _sopen_s( int *fd, const char *path, int oflags, int shflags, int pmod
 
   sa.nLength              = sizeof( SECURITY_ATTRIBUTES );
   sa.lpSecurityDescriptor = NULL;
-  sa.bInheritHandle       = (oflags & _O_NOINHERIT) ? FALSE : TRUE;
+  sa.bInheritHandle       = !(oflags & _O_NOINHERIT);
 
-  hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 0);
+  if ((oflags&(_O_WTEXT|_O_U16TEXT|_O_U8TEXT))
+          && (creation==OPEN_ALWAYS || creation==OPEN_EXISTING)
+          && !(access&GENERIC_READ))
+  {
+      hand = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
+              &sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
+      if (hand != INVALID_HANDLE_VALUE)
+      {
+          oflags = check_bom(hand, oflags, FALSE);
+          CloseHandle(hand);
+      }
+      else
+          oflags &= ~(_O_WTEXT|_O_U16TEXT|_O_U8TEXT);
+  }
+
+  hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
   if (hand == INVALID_HANDLE_VALUE)  {
-    WARN(":failed-last error (%d)\n", GetLastError());
+    WARN(":failed-last error (%d)\n",GetLastError());
     _dosmaperr(GetLastError());
     return *_errno();
   }
 
+  if (oflags & (_O_WTEXT|_O_U16TEXT|_O_U8TEXT))
+  {
+      if ((access & GENERIC_WRITE) && (creation==CREATE_NEW
+                  || creation==CREATE_ALWAYS || creation==TRUNCATE_EXISTING
+                  || (creation==OPEN_ALWAYS && GetLastError()==ERROR_ALREADY_EXISTS)))
+      {
+          if (oflags & _O_U8TEXT)
+          {
+              DWORD written = 0, tmp;
+
+              while(written!=sizeof(utf8_bom) && WriteFile(hand, (char*)utf8_bom+written,
+                          sizeof(utf8_bom)-written, &tmp, NULL))
+                  written += tmp;
+              if (written != sizeof(utf8_bom)) {
+                  WARN("error writing BOM\n");
+                  CloseHandle(hand);
+                  _dosmaperr(GetLastError());
+                  return *_errno();
+              }
+          }
+          else
+          {
+              DWORD written = 0, tmp;
+
+              while(written!=sizeof(utf16_bom) && WriteFile(hand, (char*)utf16_bom+written,
+                          sizeof(utf16_bom)-written, &tmp, NULL))
+                  written += tmp;
+              if (written != sizeof(utf16_bom))
+              {
+                  WARN("error writing BOM\n");
+                  CloseHandle(hand);
+                  _dosmaperr(GetLastError());
+                  return *_errno();
+              }
+          }
+      }
+      else if (access & GENERIC_READ)
+          oflags = check_bom(hand, oflags, TRUE);
+  }
+
   *fd = alloc_fd(hand, wxflag);
+  if (*fd == -1)
+      return *_errno();
+
+  if (oflags & _O_WTEXT)
+      get_ioinfo(*fd)->exflag |= EF_UTF16|EF_UNK_UNICODE;
+  else if (oflags & _O_U16TEXT)
+      get_ioinfo(*fd)->exflag |= EF_UTF16;
+  else if (oflags & _O_U8TEXT)
+      get_ioinfo(*fd)->exflag |= EF_UTF8;
 
   TRACE(":fd (%d) handle (%p)\n", *fd, hand);
   return 0;
 }
 
 /*********************************************************************
- *              _sopen (MSVCRT.@)
+ *              _wsopen (MSVCRT.@)
  */
-int CDECL _sopen( const char *path, int oflags, int shflags, ... )
+int CDECL _wsopen( const wchar_t *path, int oflags, int shflags, ... )
 {
   int pmode;
   int fd;
@@ -1770,25 +1830,29 @@ int CDECL _sopen( const char *path, int oflags, int shflags, ... )
   else
     pmode = 0;
 
-  _sopen_s(&fd, path, oflags, shflags, pmode);
+  _wsopen_s(&fd, path, oflags, shflags, pmode);
   return fd;
 }
 
 /*********************************************************************
- *              _wsopen_s (MSVCRT.@)
+ *              _sopen_s (MSVCRT.@)
  */
-int CDECL _wsopen_s( int *fd, const wchar_t* path, int oflags, int shflags, int pmode )
+int CDECL _sopen_s( int *fd, const char *path, int oflags, int shflags, int pmode )
 {
   DWORD access = 0, creation = 0, attrib;
-  SECURITY_ATTRIBUTES sa;
   DWORD sharing;
   int wxflag;
   HANDLE hand;
+  SECURITY_ATTRIBUTES sa;
 
-  TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
-        fd, debugstr_w(path), oflags, shflags, pmode);
+  TRACE("fd*: %p file: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
+        fd, path, oflags, shflags, pmode);
 
-  if (!MSVCRT_CHECK_PMT( fd != NULL )) return EINVAL;
+  if (!fd)
+  {
+    MSVCRT_INVALID_PMT("null out fd pointer", EINVAL);
+    return EINVAL;
+  }
 
   *fd = -1;
   wxflag = split_oflags(oflags);
@@ -1850,89 +1914,25 @@ int CDECL _wsopen_s( int *fd, const wchar_t* path, int oflags, int shflags, int
 
   sa.nLength              = sizeof( SECURITY_ATTRIBUTES );
   sa.lpSecurityDescriptor = NULL;
-  sa.bInheritHandle       = !(oflags & _O_NOINHERIT);
-
-  if ((oflags&(_O_WTEXT|_O_U16TEXT|_O_U8TEXT))
-          && (creation==OPEN_ALWAYS || creation==OPEN_EXISTING)
-          && !(access&GENERIC_READ))
-  {
-      hand = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
-              &sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
-      if (hand != INVALID_HANDLE_VALUE)
-      {
-          oflags = check_bom(hand, oflags, FALSE);
-          CloseHandle(hand);
-      }
-      else
-          oflags &= ~(_O_WTEXT|_O_U16TEXT|_O_U8TEXT);
-  }
+  sa.bInheritHandle       = (oflags & _O_NOINHERIT) ? FALSE : TRUE;
 
-  hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
+  hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 0);
   if (hand == INVALID_HANDLE_VALUE)  {
-    WARN(":failed-last error (%d)\n",GetLastError());
+    WARN(":failed-last error (%d)\n", GetLastError());
     _dosmaperr(GetLastError());
     return *_errno();
   }
 
-  if (oflags & (_O_WTEXT|_O_U16TEXT|_O_U8TEXT))
-  {
-      if ((access & GENERIC_WRITE) && (creation==CREATE_NEW
-                  || creation==CREATE_ALWAYS || creation==TRUNCATE_EXISTING
-                  || (creation==OPEN_ALWAYS && GetLastError()==ERROR_ALREADY_EXISTS)))
-      {
-          if (oflags & _O_U8TEXT)
-          {
-              DWORD written = 0, tmp;
-
-              while(written!=sizeof(utf8_bom) && WriteFile(hand, (char*)utf8_bom+written,
-                          sizeof(utf8_bom)-written, &tmp, NULL))
-                  written += tmp;
-              if (written != sizeof(utf8_bom)) {
-                  WARN("error writing BOM\n");
-                  CloseHandle(hand);
-                  _dosmaperr(GetLastError());
-                  return *_errno();
-              }
-          }
-          else
-          {
-              DWORD written = 0, tmp;
-
-              while(written!=sizeof(utf16_bom) && WriteFile(hand, (char*)utf16_bom+written,
-                          sizeof(utf16_bom)-written, &tmp, NULL))
-                  written += tmp;
-              if (written != sizeof(utf16_bom))
-              {
-                  WARN("error writing BOM\n");
-                  CloseHandle(hand);
-                  _dosmaperr(GetLastError());
-                  return *_errno();
-              }
-          }
-      }
-      else if (access & GENERIC_READ)
-          oflags = check_bom(hand, oflags, TRUE);
-  }
-
   *fd = alloc_fd(hand, wxflag);
-  if (*fd == -1)
-      return *_errno();
-
-  if (oflags & _O_WTEXT)
-      get_ioinfo(*fd)->exflag |= EF_UTF16|EF_UNK_UNICODE;
-  else if (oflags & _O_U16TEXT)
-      get_ioinfo(*fd)->exflag |= EF_UTF16;
-  else if (oflags & _O_U8TEXT)
-      get_ioinfo(*fd)->exflag |= EF_UTF8;
 
   TRACE(":fd (%d) handle (%p)\n", *fd, hand);
   return 0;
 }
 
 /*********************************************************************
- *              _wsopen (MSVCRT.@)
+ *              _sopen (MSVCRT.@)
  */
-int CDECL _wsopen( const wchar_t *path, int oflags, int shflags, ... )
+int CDECL _sopen( const char *path, int oflags, int shflags, ... )
 {
   int pmode;
   int fd;
@@ -1948,7 +1948,7 @@ int CDECL _wsopen( const wchar_t *path, int oflags, int shflags, ... )
   else
     pmode = 0;
 
-  _wsopen_s(&fd, path, oflags, shflags, pmode);
+  _sopen_s(&fd, path, oflags, shflags, pmode);
   return fd;
 }