summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDanny Smith <dannysmith@users.sourceforge.net>2002-10-03 23:42:00 +0000
committerDanny Smith <dannysmith@users.sourceforge.net>2002-10-03 23:42:00 +0000
commit62c4e5f143c12b4cc5be00df3142f9879495ce58 (patch)
tree9e9e4e33ba5bb8b6f9654f8bf8cf6ee9ab9020ca
parenta6eda2bb662dae98b6b61236da6501a53a1f3794 (diff)
downloadgdb-62c4e5f143c12b4cc5be00df3142f9879495ce58.tar.gz
Merge from mingw mainline.
-rw-r--r--winsup/mingw/CRTfmode.c20
-rw-r--r--winsup/mingw/ChangeLog29
-rw-r--r--winsup/mingw/crt1.c251
-rw-r--r--winsup/mingw/include/ctype.h4
-rw-r--r--winsup/mingw/include/fcntl.h91
-rw-r--r--winsup/mingw/include/io.h2
-rw-r--r--winsup/mingw/include/stdlib.h33
-rw-r--r--winsup/mingw/samples/fmode/all.c49
-rw-r--r--winsup/mingw/samples/fmode/test.c32
9 files changed, 505 insertions, 6 deletions
diff --git a/winsup/mingw/CRTfmode.c b/winsup/mingw/CRTfmode.c
new file mode 100644
index 00000000000..c3f2a444c44
--- /dev/null
+++ b/winsup/mingw/CRTfmode.c
@@ -0,0 +1,20 @@
+/*
+ * CRTfmode.c
+ *
+ * Sets _CRT_fmode to be zero, which will cause _mingw32_init_fmode to leave
+ * all file modes in their default state (basically text mode).
+ *
+ * This file is part of the Mingw32 package.
+ *
+ * THIS FILE IS IN THE PUBLIC DOMAIN.
+ *
+ * Contributers:
+ * Created by Colin Peters <colin@fu.is.saga-u.ac.jp>
+ *
+ * $Revision$
+ * $Author$
+ * $Date$
+ *
+ */
+
+int _CRT_fmode = 0;
diff --git a/winsup/mingw/ChangeLog b/winsup/mingw/ChangeLog
index f3840fc29dc..04495c64034 100644
--- a/winsup/mingw/ChangeLog
+++ b/winsup/mingw/ChangeLog
@@ -1,3 +1,32 @@
+2002-10-04 Danny Smith <dannysmith@users.sourceforge.net>
+
+ Merge from mingw mainline.
+
+ 2002-10-03 Danny Smith <dannysmith@users.sourceforge.net>
+
+ * include/cytpe.h (_imp____mbcur_max): Add missing ';'.
+ (_imp____mbcur_max_dll): Likewise.
+
+ 2002-10-03 Danny Smith <dannysmith@users.sourceforge.net>
+
+ * include/fcntl.h (_fmode): Remove declarations and
+ compatibility defines.
+ (_setmode, setmode): Remove prototypes.
+ * include/stdlib (_fmode): Add declarations and
+ compatibility defines. Change type to int.
+ * include/io.h (_setmode, setmode): Add prototypes.
+ * samples/fmode/all.c: Adjust includes.
+ * samples/fmode/test.c: Likewise.
+ * crt1.c (_CRT_fmode): Declare as int.
+ * CRTfmode.c (_CRT_fmode): Likewise.
+
+ * include/stdlib: Remove comment about MB_CUR_MAX.
+
+ 2002-10-02 Danny Smith <dannysmith@users.sourceforge.net>
+
+ * include/stdlib.h (_imp____mbcur_max): Add missing ';'.
+ (_imp____mbcur_max_dll): Likewise.
+
2002-10-03 Danny Smith <dannysmith@users.sourceforge.net>
* include/math.h (__signbit); Remove errant '__' added in last
diff --git a/winsup/mingw/crt1.c b/winsup/mingw/crt1.c
new file mode 100644
index 00000000000..07aa9c5d41c
--- /dev/null
+++ b/winsup/mingw/crt1.c
@@ -0,0 +1,251 @@
+/*
+ * crt1.c
+ *
+ * Source code for the startup proceedures used by all programs. This code
+ * is compiled to make crt1.o, which should be located in the library path.
+ *
+ * This code is part of the Mingw32 package.
+ *
+ * Contributors:
+ * Created by Colin Peters <colin@bird.fu.is.saga-u.ac.jp>
+ * Maintained by Mumit Khan <khan@xraylith.wisc.EDU>
+ *
+ * THIS SOFTWARE IS NOT COPYRIGHTED
+ *
+ * This source code is offered for use in the public domain. You may
+ * use, modify or distribute it freely.
+ *
+ * This code is distributed in the hope that it will be useful but
+ * WITHOUT ANY WARRANTY. ALL WARRENTIES, EXPRESS OR IMPLIED ARE HEREBY
+ * DISCLAMED. This includes but is not limited to warrenties of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * $Revision$
+ * $Author$
+ * $Date$
+ *
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <io.h>
+#include <fcntl.h>
+#include <process.h>
+#include <float.h>
+#include <windows.h>
+#include <signal.h>
+
+/* NOTE: The code for initializing the _argv, _argc, and environ variables
+ * has been moved to a separate .c file which is included in both
+ * crt1.c and dllcrt1.c. This means changes in the code don't have to
+ * be manually synchronized, but it does lead to this not-generally-
+ * a-good-idea use of include. */
+#include "init.c"
+
+extern int main (int, char **, char **);
+
+/*
+ * Must have the correct app type for MSVCRT.
+ */
+
+#ifdef __MSVCRT__
+#define __UNKNOWN_APP 0
+#define __CONSOLE_APP 1
+#define __GUI_APP 2
+__MINGW_IMPORT void __set_app_type(int);
+#endif /* __MSVCRT__ */
+
+/*
+ * Setup the default file handles to have the _CRT_fmode mode, as well as
+ * any new files created by the user.
+ */
+extern int _CRT_fmode;
+
+static void
+_mingw32_init_fmode ()
+{
+ /* Don't set the file mode if the user hasn't set any value for it. */
+ if (_CRT_fmode)
+ {
+ _fmode = _CRT_fmode;
+
+ /*
+ * This overrides the default file mode settings for stdin,
+ * stdout and stderr. At first I thought you would have to
+ * test with isatty, but it seems that the DOS console at
+ * least is smart enough to handle _O_BINARY stdout and
+ * still display correctly.
+ */
+ if (stdin)
+ {
+ _setmode (_fileno (stdin), _CRT_fmode);
+ }
+ if (stdout)
+ {
+ _setmode (_fileno (stdout), _CRT_fmode);
+ }
+ if (stderr)
+ {
+ _setmode (_fileno (stderr), _CRT_fmode);
+ }
+ }
+}
+
+/* This function will be called when a trap occurs. Thanks to Jacob
+ Navia for his contribution. */
+static CALLBACK long
+_gnu_exception_handler (EXCEPTION_POINTERS * exception_data)
+{
+ void (*old_handler) (int);
+ long action = EXCEPTION_CONTINUE_SEARCH;
+ int reset_fpu = 0;
+
+ switch (exception_data->ExceptionRecord->ExceptionCode)
+ {
+ case EXCEPTION_ACCESS_VIOLATION:
+ /* test if the user has set SIGSEGV */
+ old_handler = signal (SIGSEGV, SIG_DFL);
+ if (old_handler == SIG_IGN)
+ {
+ /* this is undefined if the signal was raised by anything other
+ than raise (). */
+ signal (SIGSEGV, SIG_IGN);
+ action = EXCEPTION_CONTINUE_EXECUTION;
+ }
+ else if (old_handler != SIG_DFL)
+ {
+ /* This means 'old' is a user defined function. Call it */
+ (*old_handler) (SIGSEGV);
+ action = EXCEPTION_CONTINUE_EXECUTION;
+ }
+ break;
+
+ case EXCEPTION_FLT_INVALID_OPERATION:
+ case EXCEPTION_FLT_DIVIDE_BY_ZERO:
+ case EXCEPTION_FLT_DENORMAL_OPERAND:
+ case EXCEPTION_FLT_OVERFLOW:
+ case EXCEPTION_FLT_UNDERFLOW:
+ case EXCEPTION_FLT_INEXACT_RESULT:
+ reset_fpu = 1;
+ /* fall through. */
+
+ case EXCEPTION_INT_DIVIDE_BY_ZERO:
+ /* test if the user has set SIGFPE */
+ old_handler = signal (SIGFPE, SIG_DFL);
+ if (old_handler == SIG_IGN)
+ {
+ signal (SIGFPE, SIG_IGN);
+ if (reset_fpu)
+ _fpreset ();
+ action = EXCEPTION_CONTINUE_EXECUTION;
+ }
+ else if (old_handler != SIG_DFL)
+ {
+ /* This means 'old' is a user defined function. Call it */
+ (*old_handler) (SIGFPE);
+ action = EXCEPTION_CONTINUE_EXECUTION;
+ }
+ break;
+
+ default:
+ break;
+ }
+ return action;
+}
+
+/*
+ * The function mainCRTStartup is the entry point for all console programs.
+ */
+static int
+__mingw_CRTStartup ()
+{
+ int nRet;
+
+ /*
+ * Set up the top-level exception handler so that signal handling
+ * works as expected. The mapping between ANSI/POSIX signals and
+ * Win32 SE is not 1-to-1, so caveat emptore.
+ *
+ */
+ SetUnhandledExceptionFilter (_gnu_exception_handler);
+
+ /*
+ * Initialize floating point unit.
+ */
+ _fpreset (); /* Supplied by the runtime library. */
+
+ /*
+ * Set up __argc, __argv and _environ.
+ */
+ _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.
+ * NOTE: DLLs don't do this because that would be rude!
+ */
+ _mingw32_init_fmode ();
+
+ /*
+ * Call the main function. If the user does not supply one
+ * the one in the 'libmingw32.a' library will be linked in, and
+ * that one calls WinMain. See main.c in the 'lib' dir
+ * for more details.
+ */
+ nRet = main (_argc, _argv, environ);
+
+ /*
+ * Perform exit processing for the C library. This means
+ * flushing output and calling 'atexit' registered functions.
+ */
+ _cexit ();
+
+ ExitProcess (nRet);
+
+ return 0;
+}
+
+/*
+ * The function mainCRTStartup is the entry point for all console programs.
+ */
+int
+mainCRTStartup ()
+{
+#ifdef __MSVCRT__
+ __set_app_type (__CONSOLE_APP);
+#endif
+ __mingw_CRTStartup ();
+ return 0;
+}
+
+/*
+ * For now the GUI startup function is the same as the console one.
+ * This simply gets rid of the annoying warning about not being able
+ * to find WinMainCRTStartup when linking GUI applications.
+ */
+int
+WinMainCRTStartup ()
+{
+#ifdef __MSVCRT__
+ __set_app_type (__GUI_APP);
+#endif
+ __mingw_CRTStartup ();
+}
+
+/*
+ * We force use of library version of atexit, which is only
+ * visible in import lib as _imp__atexit
+ */
+extern int (*_imp__atexit)(void (*)(void));
+int atexit (void (* pfn )(void) )
+{
+ return ( (*_imp__atexit)(pfn));
+}
+
+/* Likewise for non-ANSI _onexit */
+extern _onexit_t (*_imp___onexit)(_onexit_t);
+_onexit_t
+_onexit (_onexit_t pfn )
+{
+ return (*_imp___onexit)(pfn);
+}
diff --git a/winsup/mingw/include/ctype.h b/winsup/mingw/include/ctype.h
index 50e7ed2414e..17301eff690 100644
--- a/winsup/mingw/include/ctype.h
+++ b/winsup/mingw/include/ctype.h
@@ -117,10 +117,10 @@ __BEGIN_CSTD_NAMESPACE
#else /* ! __DECLSPEC_SUPPORTED */
# ifdef __MSVCRT__
- extern int* _imp____mbcur_max
+ extern int* _imp____mbcur_max;
# define MB_CUR_MAX (*_imp____mb_cur_max)
# else /* not __MSVCRT */
- extern int* _imp____mbcur_max_dll
+ extern int* _imp____mbcur_max_dll;
# define MB_CUR_MAX (*_imp____mb_cur_max_dll)
# endif /* not __MSVCRT */
#endif /* __DECLSPEC_SUPPORTED */
diff --git a/winsup/mingw/include/fcntl.h b/winsup/mingw/include/fcntl.h
new file mode 100644
index 00000000000..add43ff0e1b
--- /dev/null
+++ b/winsup/mingw/include/fcntl.h
@@ -0,0 +1,91 @@
+/*
+ * fcntl.h
+ *
+ * Access constants for _open. Note that the permissions constants are
+ * in sys/stat.h (ick).
+ *
+ * This code is part of the Mingw32 package.
+ *
+ * Contributors:
+ * Created by Colin Peters <colin@bird.fu.is.saga-u.ac.jp>
+ *
+ * THIS SOFTWARE IS NOT COPYRIGHTED
+ *
+ * This source code is offered for use in the public domain. You may
+ * use, modify or distribute it freely.
+ *
+ * This code is distributed in the hope that it will be useful but
+ * WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY
+ * DISCLAIMED. This includes but is not limited to warranties of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * $Revision$
+ * $Author$
+ * $Date$
+ *
+ */
+
+#ifndef __STRICT_ANSI__
+
+#ifndef _FCNTL_H_
+#define _FCNTL_H_
+
+/* All the headers include this file. */
+#include <_mingw.h>
+
+/*
+ * It appears that fcntl.h should include io.h for compatibility...
+ */
+#include <io.h>
+
+/* Specifiy one of these flags to define the access mode. */
+#define _O_RDONLY 0
+#define _O_WRONLY 1
+#define _O_RDWR 2
+
+/* Mask for access mode bits in the _open flags. */
+#define _O_ACCMODE (_O_RDONLY|_O_WRONLY|_O_RDWR)
+
+#define _O_APPEND 0x0008 /* Writes will add to the end of the file. */
+
+#define _O_RANDOM 0x0010
+#define _O_SEQUENTIAL 0x0020
+#define _O_TEMPORARY 0x0040 /* Make the file dissappear after closing.
+ * WARNING: Even if not created by _open! */
+#define _O_NOINHERIT 0x0080
+
+#define _O_CREAT 0x0100 /* Create the file if it does not exist. */
+#define _O_TRUNC 0x0200 /* Truncate the file if it does exist. */
+#define _O_EXCL 0x0400 /* Open only if the file does not exist. */
+
+#define _O_SHORT_LIVED 0x1000
+
+/* NOTE: Text is the default even if the given _O_TEXT bit is not on. */
+#define _O_TEXT 0x4000 /* CR-LF in file becomes LF in memory. */
+#define _O_BINARY 0x8000 /* Input and output is not translated. */
+#define _O_RAW _O_BINARY
+
+#ifndef _NO_OLDNAMES
+
+/* POSIX/Non-ANSI names for increased portability */
+#define O_RDONLY _O_RDONLY
+#define O_WRONLY _O_WRONLY
+#define O_RDWR _O_RDWR
+#define O_ACCMODE _O_ACCMODE
+#define O_APPEND _O_APPEND
+#define O_CREAT _O_CREAT
+#define O_TRUNC _O_TRUNC
+#define O_EXCL _O_EXCL
+#define O_TEXT _O_TEXT
+#define O_BINARY _O_BINARY
+#define O_TEMPORARY _O_TEMPORARY
+#define O_NOINHERIT _O_NOINHERIT
+#define O_SEQENTIAL _O_SEQUENTIAL
+#define O_RANDOM _O_RANDOM
+
+#endif /* Not _NO_OLDNAMES */
+
+#endif /* Not _FCNTL_H_ */
+
+#endif /* Not __STRICT_ANSI__ */
+
diff --git a/winsup/mingw/include/io.h b/winsup/mingw/include/io.h
index 5c6ac46f8a6..ab12c7c1e5a 100644
--- a/winsup/mingw/include/io.h
+++ b/winsup/mingw/include/io.h
@@ -193,6 +193,7 @@ int _open (const char*, int, ...);
int _open_osfhandle (long, int);
int _pipe (int *, unsigned int, int);
int _read (int, void*, unsigned int);
+int _setmode (int, int);
/* SH_... flags for nShFlags defined in share.h
* Optional fourth argument is unsigned unPermissions */
@@ -223,6 +224,7 @@ int isatty (int);
long lseek (int, long, int);
int open (const char*, int, ...);
int read (int, void*, unsigned int);
+int setmode (int, int);
int sopen (const char*, int, int, ...);
long tell (int);
int umask (int);
diff --git a/winsup/mingw/include/stdlib.h b/winsup/mingw/include/stdlib.h
index eb06c7138cb..e54b4e63d29 100644
--- a/winsup/mingw/include/stdlib.h
+++ b/winsup/mingw/include/stdlib.h
@@ -118,8 +118,6 @@ __MINGW_IMPORT char** __argv_dll;
/*
* Also defined in ctype.h.
*/
-
-/* Also defined in stdlib.h */
#ifndef MB_CUR_MAX
#ifdef __DECLSPEC_SUPPORTED
# ifdef __MSVCRT__
@@ -132,14 +130,15 @@ __MINGW_IMPORT char** __argv_dll;
#else /* ! __DECLSPEC_SUPPORTED */
# ifdef __MSVCRT__
- extern int* _imp____mbcur_max
+ extern int* _imp____mbcur_max;
# define MB_CUR_MAX (*_imp____mb_cur_max)
# else /* not __MSVCRT */
- extern int* _imp____mbcur_max_dll
+ extern int* _imp____mbcur_max_dll;
# define MB_CUR_MAX (*_imp____mb_cur_max_dll)
# endif /* not __MSVCRT */
#endif /* __DECLSPEC_SUPPORTED */
#endif /* MB_CUR_MAX */
+
/*
* MS likes to declare errno in stdlib.h as well.
*/
@@ -279,6 +278,32 @@ wchar_t** __p__wpgmptr(void);
/* no wide version in CRTDLL */
#endif /* __MSVCRT__ */
+/*
+ * This variable determines the default file mode.
+ * TODO: Which flags work?
+ */
+#ifndef __DECLSPEC_SUPPORTED
+
+#ifdef __MSVCRT__
+extern int* _imp___fmode;
+#define _fmode (*_imp___fmode)
+#else
+/* CRTDLL */
+extern int* _imp___fmode_dll;
+#define _fmode (*_imp___fmode_dll)
+#endif
+
+#else /* __DECLSPEC_SUPPORTED */
+
+#ifdef __MSVCRT__
+__MINGW_IMPORT int _fmode;
+#else /* ! __MSVCRT__ */
+__MINGW_IMPORT int _fmode_dll;
+#define _fmode _fmode_dll
+#endif /* ! __MSVCRT__ */
+
+#endif /* __DECLSPEC_SUPPORTED */
+
#endif /* Not __STRICT_ANSI__ */
#ifdef __GNUC__
diff --git a/winsup/mingw/samples/fmode/all.c b/winsup/mingw/samples/fmode/all.c
new file mode 100644
index 00000000000..f052b930638
--- /dev/null
+++ b/winsup/mingw/samples/fmode/all.c
@@ -0,0 +1,49 @@
+/*
+ * A sample program demonstrating how to use _CRT_fmode to change the default
+ * file opening mode to binary AND change stdin, stdout and stderr. Redirect
+ * stdout to a file from the command line to see the difference.
+ *
+ * Also try directing a file into stdin. If you type into stdin you will get
+ * \r\n at the end of every line... unlike UNIX. But at least if you
+ * redirect a file in you will get exactly the characters in the file as input.
+ *
+ * THIS CODE IS IN THE PUBLIC DOMAIN.
+ *
+ * Colin Peters <colin@fu.is.saga-u.ac.jp>
+ */
+
+#include <stdio.h>
+#include <stdlib.h> /* _fmode */
+#include <fcntl.h> /* _O_BINARY */
+#include <io.h> /* _setmode */
+
+
+unsigned int _CRT_fmode = _O_BINARY;
+
+main ()
+{
+ char* sz = "This is line one.\nThis is line two.\n";
+ FILE* fp;
+ int c;
+
+ printf (sz);
+
+ /* Note how this fopen does NOT indicate "wb" to open the file in
+ * binary mode. */
+ fp = fopen ("all.out", "w");
+
+ fprintf (fp, sz);
+
+ fclose (fp);
+
+ if (_isatty (_fileno(stdin)))
+ {
+ fprintf (stderr, "Waiting for input, press Ctrl-Z to finish.\n");
+ }
+
+ while ((c = fgetc(stdin)) != EOF)
+ {
+ printf ("\'%c\' %02X\n", (char) c, c);
+ }
+}
+
diff --git a/winsup/mingw/samples/fmode/test.c b/winsup/mingw/samples/fmode/test.c
new file mode 100644
index 00000000000..0ec364534db
--- /dev/null
+++ b/winsup/mingw/samples/fmode/test.c
@@ -0,0 +1,32 @@
+/*
+ * A sample program demonstrating how to use fmode to change the default
+ * file opening mode to binary. NOTE: Does not change stdin, stdout or
+ * stderr.
+ *
+ * THIS CODE IS IN THE PUBLIC DOMAIN.
+ *
+ * Colin Peters <colin@fu.is.saga-u.ac.jp>
+ */
+
+#include <stdio.h>
+#include <stdlib.h> /* _fmode */
+#include <fcntl.h> /* _O_BINARY */
+
+main ()
+{
+ char* sz = "This is line one.\nThis is line two.\n";
+ FILE* fp;
+
+ _fmode = _O_BINARY;
+
+ printf (sz);
+
+ /* Note how this fopen does NOT indicate "wb" to open the file in
+ * binary mode. */
+ fp = fopen ("test.out", "w");
+
+ fprintf (fp, sz);
+
+ fclose (fp);
+}
+