checkin of 0.3.0
[pspp-builds.git] / sysdeps / borlandc5.0 / bc5-con32s.c
1 /* con32s - emulates console under Windows.
2    Copyright (C) 1997, 1998 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18    02111-1307, USA. */
19
20 /* This replaces a few of the Borland C++ library functions.  It does
21    not use any of the runtime library header files, so you do not need
22    the runtime library source in order to compile it. */
23
24 #include <io.h>
25 #include <windef.h>
26 #include <wincon.h>
27
28 /* 1=It is necessary to emulate the console window. */
29 int _emu_console;
30
31 /* Exported by con32s.c. */
32 extern int _blp_console_read (void *buf, unsigned len);
33
34 /* Exported by Borland runtime library. */
35 extern long _handles[];
36 extern int __IOerror (int);
37 extern int __NTerror (void);
38
39 /* Replaces Borland library function. */
40 int
41 _rtl_read (int fd, void *buf, unsigned len)
42 {
43   DWORD nread;
44
45   if ((unsigned) fd >= _nfile)
46     return __IOerror (ERROR_INVALID_HANDLE);
47
48   /* Redirect stdin to the faked console window. */
49   if (_emu_console && fd < 3)
50     return _blp_console_read (buf, len);
51
52   if (ReadFile ((HANDLE) _handles[fd], buf, (DWORD) len, &nread, NULL) != 1)
53     return __NTerror ();
54   else
55     return (int) nread;
56 }
57
58 /* Replaces Borland library function. */
59 int
60 _rtl_write (int fd, const void *buf, unsigned int len)
61 {
62   DWORD written;
63
64   if ((unsigned) fd >= _nfile)
65     return __IOerror (ERROR_INVALID_HANDLE);
66
67   /* Redirect stdout, stderr to the faked console window. */
68   if (_emu_console && fd < 3)
69     return _blp_console_write (buf, len);
70
71   if (WriteFile ((HANDLE) _handles[fd], (PVOID) buf, (DWORD) len, &written,
72                  NULL) != 1)
73     return __NTerror ();
74   else
75     return (int) written;
76 }
77
78 void
79 determine_os (void)
80 {
81 #pragma startup determine_os 64
82   DWORD nButtons;
83
84   /* Try out a random console function.  If it fails then we must not
85      have a console.
86
87      Believe it or not, this seems to be the only way to determine
88      reliably whether we're running under 3.1.  If you know a better
89      way, let me know. */
90   if (GetNumberOfConsoleMouseButtons (&nButtons))
91     _emu_console = 0;
92   else
93     _emu_console = 1;
94 }
95