From bea966c0d9a65fd0c7b909fc5f8b2b2ff7635537 Mon Sep 17 00:00:00 2001 From: Danny Smith Date: Sat, 19 Oct 2002 20:26:26 +0000 Subject: * 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. --- winsup/mingw/ChangeLog | 15 +++++++++++++++ winsup/mingw/Makefile.in | 6 +++--- winsup/mingw/binmode.c | 5 +++++ winsup/mingw/crt1.c | 17 +++++++++++++---- winsup/mingw/samples/fmode/jamfile | 2 ++ winsup/mingw/samples/fmode/test2.c | 37 +++++++++++++++++++++++++++++++++++++ winsup/mingw/txtmode.c | 7 +++++++ 7 files changed, 82 insertions(+), 7 deletions(-) create mode 100644 winsup/mingw/binmode.c create mode 100644 winsup/mingw/samples/fmode/test2.c create mode 100644 winsup/mingw/txtmode.c diff --git a/winsup/mingw/ChangeLog b/winsup/mingw/ChangeLog index 9808a1f56..33f053bee 100644 --- a/winsup/mingw/ChangeLog +++ b/winsup/mingw/ChangeLog @@ -1,3 +1,18 @@ +2002-10-19 Danny Smith + + * 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 * include/stdint.h (INT64_C, UINT64_C ): Append suffix to let diff --git a/winsup/mingw/Makefile.in b/winsup/mingw/Makefile.in index 332fb1784..f2e5d253d 100644 --- a/winsup/mingw/Makefile.in +++ b/winsup/mingw/Makefile.in @@ -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 index 000000000..1cc3dedbf --- /dev/null +++ b/winsup/mingw/binmode.c @@ -0,0 +1,5 @@ +#include + +/* Set default file mode to binary */ + +int _fmode = _O_BINARY; diff --git a/winsup/mingw/crt1.c b/winsup/mingw/crt1.c index 07aa9c5d4..40c8d466a 100644 --- a/winsup/mingw/crt1.c +++ b/winsup/mingw/crt1.c @@ -26,10 +26,8 @@ * */ -#include #include #include -#include #include #include #include @@ -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 (); diff --git a/winsup/mingw/samples/fmode/jamfile b/winsup/mingw/samples/fmode/jamfile index 33455ec59..5b7a1cb06 100644 --- a/winsup/mingw/samples/fmode/jamfile +++ b/winsup/mingw/samples/fmode/jamfile @@ -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 index 000000000..b7d6b1891 --- /dev/null +++ b/winsup/mingw/samples/fmode/test2.c @@ -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 + * Danny Smith + */ + +#include +#include /* _fmode */ +#include /* _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 index 000000000..077174697 --- /dev/null +++ b/winsup/mingw/txtmode.c @@ -0,0 +1,7 @@ +#include + +/* Set default file mode to text */ + +/* Is this correct? Default value of _fmode in msvcrt.dll is 0. */ + +int _fmode = _O_TEXT; -- cgit v1.2.3