OSDN Git Service

* Makefile.in (CRT0S): Add txtmode.o binmode.o.
authordannysmith <dannysmith>
Sat, 19 Oct 2002 20:26:26 +0000 (20:26 +0000)
committerdannysmith <dannysmith>
Sat, 19 Oct 2002 20:26:26 +0000 (20:26 +0000)
(MINGW_OBJS): Add txtmode.o.
(SRCDIST_FILES): Add txtmode.c binmode.c.
crt1.c: Don't include fcntrl.h, stdlib.h.
(_fmode): Declare, without dllimport attribute.
(__p__fmode): Declare access function for dll's _fmode.
(_mingw32_init_fmode): Sync dll _fmode with staticly linked
_fmode for app.
* txtmode.c: New file.
* binmode.c: New file.
* samples/fmode/test2.c: New file.
* samples/fmode/jamfile: Add test2.exe target.

winsup/mingw/ChangeLog
winsup/mingw/Makefile.in
winsup/mingw/binmode.c [new file with mode: 0644]
winsup/mingw/crt1.c
winsup/mingw/samples/fmode/jamfile
winsup/mingw/samples/fmode/test2.c [new file with mode: 0644]
winsup/mingw/txtmode.c [new file with mode: 0644]

index 9808a1f..33f053b 100644 (file)
@@ -1,3 +1,18 @@
+2002-10-19  Danny Smith  <dannysmith@users.sourceforge.net>
+
+       * Makefile.in (CRT0S): Add txtmode.o binmode.o.
+       (MINGW_OBJS): Add txtmode.o.
+       (SRCDIST_FILES): Add txtmode.c binmode.c.
+       crt1.c: Don't include fcntrl.h, stdlib.h.
+       (_fmode): Declare, without dllimport attribute.
+       (__p__fmode): Declare access function for dll's _fmode.
+       (_mingw32_init_fmode): Sync dll _fmode with staticly linked
+       _fmode for app.
+       * txtmode.c: New file.
+       * binmode.c: New file.
+       * samples/fmode/test2.c: New file.
+       * samples/fmode/jamfile: Add test2.exe target.
+
 2002-10-11  Danny Smith  <dannysmith@users.sourceforge.net>
 
        * include/stdint.h (INT64_C, UINT64_C ): Append suffix to let
index 332fb17..f2e5d25 100644 (file)
@@ -148,9 +148,9 @@ FLAGS_TO_PASS:=\
        TARFILEEXT="$(TARFILEEXT)"
 
 CRT0S = crt1.o dllcrt1.o crt2.o dllcrt2.o CRT_noglob.o crtmt.o crtst.o \
-       CRT_fp8.o CRT_fp10.o
+       CRT_fp8.o CRT_fp10.o txtmode.o binmode.o
 MINGW_OBJS = CRTglob.o CRTfmode.o CRTinit.o  dllmain.o gccmain.o \
-            main.o crtst.o mthr_stub.o CRT_fp10.o
+            main.o crtst.o mthr_stub.o CRT_fp10.o txtmode.o
 MOLD_OBJS = ctype_old.o string_old.o
 
 LIBS = libcrtdll.a libmsvcrt.a libmsvcrt20.a libmsvcrt40.a libmingw32.a \
@@ -164,7 +164,7 @@ crt1.c crtdll.def crtmt.c crtst.c ctype_old.c  dllcrt1.c dllmain.c \
 gccmain.c init.c install-sh jamfile main.c mkinstalldirs moldname-crtdll.def \
 moldname-msvcrt.def moldname.def moldname.def.in msvcrt.def msvcrt20.def \
 msvcrt40.def mthr.c mthr_init.c mthr_stub.c readme.txt string_old.c \
-CRT_fp8.c CRT_fp10.c test_headers.c
+CRT_fp8.c CRT_fp10.c test_headers.c txtmode.c binmode.c
 
 all_dlls_host = @all_dlls_host@
 install_dlls_host = @install_dlls_host@
diff --git a/winsup/mingw/binmode.c b/winsup/mingw/binmode.c
new file mode 100644 (file)
index 0000000..1cc3ded
--- /dev/null
@@ -0,0 +1,5 @@
+#include <fcntl.h>
+
+/* Set default file mode to binary */
+
+int _fmode = _O_BINARY; 
index 07aa9c5..40c8d46 100644 (file)
  *
  */
 
-#include <stdlib.h>
 #include <stdio.h>
 #include <io.h>
-#include <fcntl.h>
 #include <process.h>
 #include <float.h>
 #include <windows.h>
@@ -55,6 +53,12 @@ extern int main (int, char **, char **);
 __MINGW_IMPORT void __set_app_type(int);
 #endif /* __MSVCRT__ */
 
+/*  Global _fmode for this .exe, not the one in msvcrt.dll,
+    The default is set in txtmode.o in libmingw32.a */
+#undef _fmode
+extern int _fmode;  
+extern int* __p__fmode(void); /* To access the dll _fmode */
+
 /*
  * Setup the default file handles to have the _CRT_fmode mode, as well as
  * any new files created by the user.
@@ -89,6 +93,10 @@ _mingw32_init_fmode ()
          _setmode (_fileno (stderr), _CRT_fmode);
        }
     }
+
+    /*  Now sync  the dll _fmode to the  one for this .exe.  */
+    *__p__fmode() = _fmode;    
+
 }
 
 /* This function will be called when a trap occurs. Thanks to Jacob
@@ -180,8 +188,9 @@ __mingw_CRTStartup ()
   _mingw32_init_mainargs ();
 
   /*
-   * Sets the default file mode for stdin, stdout and stderr, as well
-   * as files later opened by the user, to _CRT_fmode.
+   * Sets the default file mode.
+   * If _CRT_fmode is set, also set mode for stdin, stdout
+   * and stderr, as well
    * NOTE: DLLs don't do this because that would be rude!
    */
   _mingw32_init_fmode ();
index 33455ec..5b7a1cb 100644 (file)
@@ -1,5 +1,7 @@
 
 Main test.exe : test.c ;
 
+Main test2.exe : test2.c ;
+
 Main all.exe : all.c ;
 
diff --git a/winsup/mingw/samples/fmode/test2.c b/winsup/mingw/samples/fmode/test2.c
new file mode 100644 (file)
index 0000000..b7d6b18
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * A sample program demonstrating how to use fmode to change the default
+ * file opening mode to binary.  Compare this file, which sets _fmode
+ * at file level to test.c, which sets the dll variable directly within
+ * main.
+ * NOTE: Does not change stdin, stdout or stderr.
+ *
+ * THIS CODE IS IN THE PUBLIC DOMAIN.
+ *
+ * Colin Peters <colin@fu.is.saga-u.ac.jp>
+ * Danny Smith <dannysmith@users.sourceforge.net>
+ */
+
+#include <stdio.h>
+#include <stdlib.h>    /*  _fmode */
+#include <fcntl.h>     /*  _O_BINARY */
+
+#undef _fmode
+int _fmode = _O_BINARY;
+
+main ()
+{
+    
+       char* sz = "This is line one.\nThis is line two.\n";
+       FILE*   fp;
+
+       printf (sz);
+
+       /* Note how this fopen does NOT indicate "wb" to open the file in
+        * binary mode. */
+       fp = fopen ("test2.out", "w");
+
+       fprintf (fp, sz);
+
+       fclose (fp);
+}
+
diff --git a/winsup/mingw/txtmode.c b/winsup/mingw/txtmode.c
new file mode 100644 (file)
index 0000000..0771746
--- /dev/null
@@ -0,0 +1,7 @@
+#include <fcntl.h>
+
+/* Set default file mode to text */
+
+/* Is this correct?  Default value of  _fmode in msvcrt.dll is 0. */
+
+int _fmode = _O_TEXT;