[crt]
[reactos.git] / reactos / lib / sdk / crt / stdio / file.c
index c00a459..a08d59e 100644 (file)
@@ -7,6 +7,14 @@
  *                  Ported to ReactOS by Aleksey Bragin (aleksey@reactos.org)
  */
 
+/*********************************************
+ * This file contains ReactOS changes!!
+ * Don't blindly sync it with Wine code!
+ *
+ * If you break Unicode output on the console again, please update this counter:
+ *   int hours_wasted_on_this = 42;
+ *********************************************/
+
 /*
  * msvcrt.dll file functions
  *
@@ -309,7 +317,7 @@ static FILE* alloc_fp(void)
   unsigned int i;
   FILE *file;
 
-  for (i = 3; i < max_streams; i++)
+  for (i = 3; i < (unsigned int)max_streams; i++)
   {
     file = get_file(i);
     if (!file)
@@ -386,17 +394,17 @@ unsigned create_io_inherit_block(WORD *size, BYTE **block)
       *handle_ptr = INVALID_HANDLE_VALUE;
     }
     wxflag_ptr++; handle_ptr++;
-  } 
+  }
   return TRUE;
 }
 
-/* INTERNAL: Set up all file descriptors, 
- * as well as default streams (stdin, stderr and stdout) 
+/* INTERNAL: Set up all file descriptors,
+ * as well as default streams (stdin, stderr and stdout)
  */
 void msvcrt_init_io(void)
 {
   STARTUPINFOA  si;
-  int           i;
+  unsigned int  i;
   ioinfo        *fdinfo;
 
   InitializeCriticalSection(&file_cs);
@@ -726,10 +734,8 @@ int CDECL _wunlink(const wchar_t *path)
 /* _flushall calls fflush which calls _flushall */
 int CDECL fflush(FILE* file);
 
-/*********************************************************************
- *             _flushall (MSVCRT.@)
- */
-int CDECL _flushall(void)
+/* INTERNAL: Flush all stream buffer */
+static int flush_all_buffers(int mask)
 {
   int i, num_flushed = 0;
   FILE *file;
@@ -740,8 +746,8 @@ int CDECL _flushall(void)
 
     if (file->_flag)
     {
-      if(file->_flag & _IOWRT) {
-       fflush(file);
+      if(file->_flag & mask) {
+        fflush(file);
         num_flushed++;
       }
     }
@@ -752,22 +758,41 @@ int CDECL _flushall(void)
   return num_flushed;
 }
 
+/*********************************************************************
+ *             _flushall (MSVCRT.@)
+ */
+int CDECL _flushall(void)
+{
+    return flush_all_buffers(_IOWRT | _IOREAD);
+}
+
 /*********************************************************************
  *             fflush (MSVCRT.@)
  */
 int CDECL fflush(FILE* file)
 {
     if(!file) {
-        _flushall();
+        flush_all_buffers(_IOWRT);
     } else if(file->_flag & _IOWRT) {
         int res;
 
         _lock_file(file);
         res = flush_buffer(file);
+        /* FIXME
+        if(!res && (file->_flag & _IOCOMMIT))
+            res = _commit(file->_file) ? EOF : 0;
+        */
         _unlock_file(file);
 
         return res;
-    }
+    } else if(file->_flag & _IOREAD) {
+        _lock_file(file);
+        file->_cnt = 0;
+        file->_ptr = file->_base;
+        _unlock_file(file);
+
+        return 0;
+    }    
     return 0;
 }
 
@@ -1017,7 +1042,7 @@ __int64 CDECL _lseeki64(int fd, __int64 offset, int whence)
  */
 LONG CDECL _lseek(int fd, LONG offset, int whence)
 {
-    return _lseeki64(fd, offset, whence);
+    return (LONG)_lseeki64(fd, offset, whence);
 }
 
 /*********************************************************************
@@ -1240,7 +1265,7 @@ static int get_flags(const wchar_t* mode, int *open_flags, int* stream_flags)
       *open_flags |=  _O_BINARY;
       *open_flags &= ~_O_TEXT;
       break;
-    case 'T': case 't':
+    case 't':
       *open_flags |=  _O_TEXT;
       *open_flags &= ~_O_BINARY;
       break;
@@ -1563,7 +1588,7 @@ int CDECL _sopen_s( int *fd, const char *path, int oflags, int shflags, int pmod
     else
       creation = OPEN_EXISTING;
   }
-  
+
   switch( shflags )
   {
     case _SH_DENYRW:
@@ -2339,12 +2364,12 @@ wint_t CDECL fgetwc(FILE* file)
       wcp = (char *)&wc;
       for(i=0; i<sizeof(wc); i++)
       {
-        if (file->_cnt>0) 
+        if (file->_cnt>0)
         {
           file->_cnt--;
           chp = file->_ptr++;
           wcp[i] = *chp;
-        } 
+        }
         else
         {
           j = _filbuf(file);
@@ -2363,7 +2388,7 @@ wint_t CDECL fgetwc(FILE* file)
       _unlock_file(file);
       return wc;
     }
-    
+
   c = fgetc(file);
   if ((__mb_cur_max > 1) && isleadbyte(c))
     {
@@ -2470,7 +2495,7 @@ size_t CDECL fwrite(const void *ptr, size_t size, size_t nmemb, FILE* file)
 
     _lock_file(file);
     if(file->_cnt) {
-        int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
+        int pcnt=((unsigned)file->_cnt>wrcnt)? wrcnt: file->_cnt;
         memcpy(file->_ptr, ptr, pcnt);
         file->_cnt -= pcnt;
         file->_ptr += pcnt;
@@ -2505,13 +2530,38 @@ size_t CDECL fwrite(const void *ptr, size_t size, size_t nmemb, FILE* file)
 
 /*********************************************************************
  *             fputwc (MSVCRT.@)
+ * FORKED for ReactOS, don't sync with Wine!
+ * References:
+ *   - http://jira.reactos.org/browse/CORE-6495
+ *   - http://bugs.winehq.org/show_bug.cgi?id=8598
  */
-wint_t CDECL fputwc(wint_t wc, FILE* file)
+wint_t CDECL fputwc(wchar_t c, FILE* stream)
 {
-  wchar_t mwc=wc;
-  if (fwrite( &mwc, sizeof(mwc), 1, file) != 1)
-    return WEOF;
-  return wc;
+    /* If this is a real file stream (and not some temporary one for
+       sprintf-like functions), check whether it is opened in text mode.
+       In this case, we have to perform an implicit conversion to ANSI. */
+    if (!(stream->_flag & _IOSTRG) && get_ioinfo(stream->_file)->wxflag & WX_TEXT)
+    {
+        /* Convert to multibyte in text mode */
+        char mbc[MB_LEN_MAX];
+        int mb_return;
+
+        mb_return = wctomb(mbc, c);
+
+        if(mb_return == -1)
+            return WEOF;
+
+        /* Output all characters */
+        if (fwrite(mbc, mb_return, 1, stream) != 1)
+            return WEOF;
+    }
+    else
+    {
+        if (fwrite(&c, sizeof(c), 1, stream) != 1)
+            return WEOF;
+    }
+
+    return c;
 }
 
 /*********************************************************************
@@ -2693,7 +2743,7 @@ size_t CDECL fread(void *ptr, size_t size, size_t nmemb, FILE* file)
 
   /* first buffered data */
   if(file->_cnt>0) {
-       int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
+       int pcnt= (rcnt>(unsigned int)file->_cnt)? file->_cnt:rcnt;
        memcpy(ptr, file->_ptr, pcnt);
        file->_cnt -= pcnt;
        file->_ptr += pcnt;
@@ -2720,7 +2770,7 @@ size_t CDECL fread(void *ptr, size_t size, size_t nmemb, FILE* file)
       }
       file->_cnt = _read(file->_file, file->_base, file->_bufsiz);
       file->_ptr = file->_base;
-      i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
+      i = ((unsigned int)file->_cnt<rcnt) ? file->_cnt : rcnt;
       /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
       if (i > 0 && i < file->_cnt) {
         get_ioinfo(file->_file)->wxflag &= ~WX_ATEOF;
@@ -2830,7 +2880,7 @@ int CDECL fsetpos(FILE* file, const fpos_t *pos)
   /* Discard buffered input */
   file->_cnt = 0;
   file->_ptr = file->_base;
-  
+
   /* Reset direction of i/o */
   if(file->_flag & _IORW) {
         file->_flag &= ~(_IOREAD|_IOWRT);
@@ -2884,7 +2934,7 @@ __int64 CDECL _ftelli64(FILE* file)
  */
 LONG CDECL ftell(FILE* file)
 {
-  return _ftelli64(file);
+  return (LONG)_ftelli64(file);
 }
 
 /*********************************************************************