1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 1992-2006, Free Software Foundation, Inc. *
11 * GNAT is free software; you can redistribute it and/or modify it under *
12 * terms of the GNU General Public License as published by the Free Soft- *
13 * ware Foundation; either version 2, or (at your option) any later ver- *
14 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
15 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
17 * for more details. You should have received a copy of the GNU General *
18 * Public License distributed with GNAT; see file COPYING. If not, write *
19 * to the Free Software Foundation, 51 Franklin Street, Fifth Floor, *
20 * Boston, MA 02110-1301, USA. *
22 * As a special exception, if you link this file with other files to *
23 * produce an executable, this file does not by itself cause the resulting *
24 * executable to be covered by the GNU General Public License. This except- *
25 * ion does not however invalidate any other reasons why the executable *
26 * file might be covered by the GNU Public License. *
28 * GNAT was originally developed by the GNAT team at New York University. *
29 * Extensive contributions were provided by Ada Core Technologies Inc. *
31 ****************************************************************************/
33 /* This file contains system dependent symbols that are referenced in the
34 GNAT Run Time Library */
38 #include "selectLib.h"
60 open text file for reading
61 rt for DOS and Windows NT, r for Unix
64 truncate to zero length or create text file for writing
65 wt for DOS and Windows NT, w for Unix
68 append; open or create text file for writing at end-of-file
69 at for DOS and Windows NT, a for Unix
72 open binary file for reading
73 rb for DOS and Windows NT, r for Unix
76 truncate to zero length or create binary file for writing
77 wb for DOS and Windows NT, w for Unix
80 append; open or create binary file for writing at end-of-file
81 ab for DOS and Windows NT, a for Unix
84 open text file for update (reading and writing)
85 r+t for DOS and Windows NT, r+ for Unix
88 truncate to zero length or create text file for update
89 w+t for DOS and Windows NT, w+ for Unix
92 append; open or create text file for update, writing at end-of-file
93 a+t for DOS and Windows NT, a+ for Unix
96 open binary file for update (reading and writing)
97 r+b for DOS and Windows NT, r+ for Unix
99 mode_write_binary_plus
100 truncate to zero length or create binary file for update
101 w+b for DOS and Windows NT, w+ for Unix
103 mode_append_binary_plus
104 append; open or create binary file for update, writing at end-of-file
105 a+b for DOS and Windows NT, a+ for Unix
109 (1) Opening a file with read mode fails if the file does not exist or
112 (2) Opening a file with append mode causes all subsequent writes to the
113 file to be forced to the then current end-of-file, regardless of
114 intervening calls to the fseek function.
116 (3) When a file is opened with update mode, both input and output may be
117 performed on the associated stream. However, output may not be directly
118 followed by input without an intervening call to the fflush function or
119 to a file positioning function (fseek, fsetpos, or rewind), and input
120 may not be directly followed by output without an intervening call to a
121 file positioning function, unless the input operation encounters
124 The other target dependent declarations here are for the two functions
125 __gnat_set_binary_mode and __gnat_set_text_mode:
127 void __gnat_set_binary_mode (int handle);
128 void __gnat_set_text_mode (int handle);
130 These functions have no effect in Unix (or similar systems where there is
131 no distinction between binary and text files), but in DOS (and similar
132 systems where text mode does CR/LF translation), these functions allow
133 the mode of the stream with the given handle (fileno can be used to get
134 the handle of a stream) to be changed dynamically. The returned result
135 is 0 if no error occurs and -1 if an error occurs.
137 Finally there is a boolean (character) variable
139 char __gnat_text_translation_required;
141 which is zero (false) in Unix mode, and one (true) in DOS mode, with a
142 true value indicating that text translation is required on text files
143 and that fopen supports the trailing t and b modifiers.
147 #if defined(WINNT) || defined (MSDOS) || defined (__EMX__)
148 static const char *mode_read_text = "rt";
149 static const char *mode_write_text = "wt";
150 static const char *mode_append_text = "at";
151 static const char *mode_read_binary = "rb";
152 static const char *mode_write_binary = "wb";
153 static const char *mode_append_binary = "ab";
154 static const char *mode_read_text_plus = "r+t";
155 static const char *mode_write_text_plus = "w+t";
156 static const char *mode_append_text_plus = "a+t";
157 static const char *mode_read_binary_plus = "r+b";
158 static const char *mode_write_binary_plus = "w+b";
159 static const char *mode_append_binary_plus = "a+b";
160 const char __gnat_text_translation_required = 1;
163 __gnat_set_binary_mode (int handle)
165 _setmode (handle, O_BINARY);
169 __gnat_set_text_mode (int handle)
171 _setmode (handle, O_TEXT);
177 /* Return the name of the tty. Under windows there is no name for
178 the tty, so this function, if connected to a tty, returns the generic name
182 __gnat_ttyname (int filedes)
184 if (isatty (filedes))
190 /* This function is needed to fix a bug under Win95/98. Under these plateforms
195 will put the same character into ch1 and ch2. It seem that the character
196 read by getch() is not correctly removed from the buffer. Even a
197 fflush(stdin) does not fix the bug. This bug does not appear under Window
198 NT. So we have two version of this routine below one for 95/98 and one for
199 NT/2000 version of Windows. There is also a special routine (winflushinit)
200 that will be called only the first time to check which version of Windows
201 we are running running on to set the right routine to use.
203 This problem occurs when using Text_IO.Get_Line after Text_IO.Get_Immediate
206 Calling FlushConsoleInputBuffer just after getch() fix the bug under
209 static void winflush_init (void);
211 static void winflush_95 (void);
213 static void winflush_nt (void);
215 int __gnat_is_windows_xp (void);
217 /* winflusfunction is set first to the winflushinit function which will check
218 the OS version 95/98 or NT/2000 */
220 static void (*winflush_function) (void) = winflush_init;
222 /* This function does the runtime check of the OS version and then sets
223 winflush_function to the appropriate function and then call it. */
228 DWORD dwVersion = GetVersion();
230 if (dwVersion < 0x80000000) /* Windows NT/2000 */
231 winflush_function = winflush_nt;
232 else /* Windows 95/98 */
233 winflush_function = winflush_95;
235 (*winflush_function)(); /* Perform the 'flush' */
242 FlushConsoleInputBuffer (GetStdHandle (STD_INPUT_HANDLE));
248 /* Does nothing as there is no problem under NT. */
252 __gnat_is_windows_xp (void)
254 static int is_win_xp=0, is_win_xp_checked=0;
256 if (!is_win_xp_checked)
258 OSVERSIONINFO version;
260 is_win_xp_checked = 1;
262 memset (&version, 0, sizeof (version));
263 version.dwOSVersionInfoSize = sizeof (version);
265 is_win_xp = GetVersionEx (&version)
266 && version.dwPlatformId == VER_PLATFORM_WIN32_NT
267 && (version.dwMajorVersion > 5
268 || (version.dwMajorVersion == 5 && version.dwMinorVersion >= 1));
277 static const char *mode_read_text = "r";
278 static const char *mode_write_text = "w";
279 static const char *mode_append_text = "a";
280 static const char *mode_read_binary = "r";
281 static const char *mode_write_binary = "w";
282 static const char *mode_append_binary = "a";
283 static const char *mode_read_text_plus = "r+";
284 static const char *mode_write_text_plus = "w+";
285 static const char *mode_append_text_plus = "a+";
286 static const char *mode_read_binary_plus = "r+";
287 static const char *mode_write_binary_plus = "w+";
288 static const char *mode_append_binary_plus = "a+";
289 const char __gnat_text_translation_required = 0;
291 /* These functions do nothing in non-DOS systems. */
294 __gnat_set_binary_mode (int handle ATTRIBUTE_UNUSED)
299 __gnat_set_text_mode (int handle ATTRIBUTE_UNUSED)
303 __gnat_ttyname (int filedes)
306 extern char *ttyname (int);
308 return ttyname (filedes);
317 #if defined (linux) || defined (sun) || defined (sgi) || defined (__EMX__) \
318 || (defined (__osf__) && ! defined (__alpha_vxworks)) || defined (WINNT) \
319 || defined (__MACHTEN__) || defined (__hpux__) || defined (_AIX) \
320 || (defined (__svr4__) && defined (i386)) || defined (__Lynx__) \
321 || defined (__CYGWIN__) || defined (__FreeBSD__)
327 #include <conio.h> /* for getch(), kbhit() */
335 extern char *decc$ga_stdscr;
336 static int initted = 0;
340 /* Implements the common processing for getc_immediate and
341 getc_immediate_nowait. */
343 extern void getc_immediate (FILE *, int *, int *);
344 extern void getc_immediate_nowait (FILE *, int *, int *, int *);
345 extern void getc_immediate_common (FILE *, int *, int *, int *, int);
347 /* Called by Get_Immediate (Foo); */
350 getc_immediate (FILE *stream, int *ch, int *end_of_file)
354 getc_immediate_common (stream, ch, end_of_file, &avail, 1);
357 /* Called by Get_Immediate (Foo, Available); */
360 getc_immediate_nowait (FILE *stream, int *ch, int *end_of_file, int *avail)
362 getc_immediate_common (stream, ch, end_of_file, avail, 0);
365 /* Called by getc_immediate () and getc_immediate_nowait () */
368 getc_immediate_common (FILE *stream,
374 #if defined (linux) || defined (sun) || defined (sgi) || defined (__EMX__) \
375 || (defined (__osf__) && ! defined (__alpha_vxworks)) \
376 || defined (__CYGWIN32__) || defined (__MACHTEN__) || defined (__hpux__) \
377 || defined (_AIX) || (defined (__svr4__) && defined (i386)) \
378 || defined (__Lynx__) || defined (__FreeBSD__)
382 int eof_ch = 4; /* Ctrl-D */
383 int fd = fileno (stream);
384 struct termios otermios_rec, termios_rec;
388 tcgetattr (fd, &termios_rec);
389 memcpy (&otermios_rec, &termios_rec, sizeof (struct termios));
391 /* Set RAW mode, with no echo */
392 termios_rec.c_lflag = termios_rec.c_lflag & ~ICANON & ~ECHO;
394 #if defined(linux) || defined (sun) || defined (sgi) || defined (__EMX__) \
395 || defined (__osf__) || defined (__MACHTEN__) || defined (__hpux__) \
396 || defined (_AIX) || (defined (__svr4__) && defined (i386)) \
397 || defined (__Lynx__) || defined (__FreeBSD__)
398 eof_ch = termios_rec.c_cc[VEOF];
400 /* If waiting (i.e. Get_Immediate (Char)), set MIN = 1 and wait for
401 a character forever. This doesn't seem to effect Ctrl-Z or
402 Ctrl-C processing except on OS/2 where Ctrl-C won't work right
403 unless we do a read loop. Luckily we can delay a bit between
404 iterations. If not waiting (i.e. Get_Immediate (Char, Available)),
405 don't wait for anything but timeout immediately. */
407 termios_rec.c_cc[VMIN] = 0;
408 termios_rec.c_cc[VTIME] = waiting;
410 termios_rec.c_cc[VMIN] = waiting;
411 termios_rec.c_cc[VTIME] = 0;
414 tcsetattr (fd, TCSANOW, &termios_rec);
418 /* Read is used here instead of fread, because fread doesn't
419 work on Solaris5 and Sunos4 in this situation. Maybe because we
420 are mixing calls that use file descriptors and streams. */
421 nread = read (fd, &c, 1);
424 /* On Unix terminals, Ctrl-D (EOT) is an End of File. */
432 /* Everything else is ok */
433 else if (c != eof_ch)
451 tcsetattr (fd, TCSANOW, &otermios_rec);
457 int fd = fileno (stream);
468 *ch = decc$bsd_wgetch (decc$ga_stdscr);
476 decc$bsd_nocbreak ();
479 #elif defined (__MINGW32__)
480 int fd = fileno (stream);
482 int eot_ch = 4; /* Ctrl-D */
489 (*winflush_function) ();
500 char_waiting = kbhit();
502 if (char_waiting == 1)
506 (*winflush_function) ();
521 #elif defined (__vxworks)
522 /* Bit masks of file descriptors to read from. */
523 struct fd_set readFds;
524 /* Timeout before select returns if nothing can be read. */
525 struct timeval timeOut;
527 int fd = fileno (stream);
536 /* If we do not want to wait, we have to set up fd in RAW mode. This
537 should be done outside this function as setting fd in RAW mode under
538 vxWorks flushes the buffer of fd. If the RAW mode was set here, the
539 buffer would be empty and we would always return that no character
543 /* Initialization of timeOut for its use with select. */
547 /* Initialization of readFds for its use with select;
548 FD is the only file descriptor to be monitored */
550 FD_SET (fd, &readFds);
553 /* We do all this processing to emulate a non blocking read. */
554 readable = select (width, &readFds, NULL, NULL, &timeOut);
555 if (readable == ERROR)
556 *avail = -1, *end_of_file = -1;
557 /* No character available in input. */
558 else if (readable == 0)
559 *avail = 0, *end_of_file = 0;
562 nread = read (fd, &c, 1);
564 *avail = 1, *end_of_file = 0;
567 *avail = 0, *end_of_file = 1;
570 *avail = -1, *end_of_file = -1;
574 /* We have to wait until we get a character */
580 /* Save the current mode of FD. */
581 option = ioctl (fd, FIOGETOPTIONS, 0);
583 /* Set FD in RAW mode. */
584 status = ioctl (fd, FIOSETOPTIONS, OPT_RAW);
587 nread = read (fd, &c, 1);
589 *avail = 1, *end_of_file = 0;
592 *avail = 0, *end_of_file = 1;
593 /* Else there is an ERROR. */
596 /* Revert FD to its previous mode. */
597 status = ioctl (fd, FIOSETOPTIONS, option);
605 /* If we're not on a terminal, then we don't need any fancy processing.
606 Also this is the only thing that's left if we're not on one of the
607 supported systems; which means that for non supported systems,
608 get_immediate may wait for a carriage return on terminals. */
609 *ch = fgetc (stream);
623 /* The following definitions are provided in NT to support Windows based
629 /* Provide functions to echo the values passed to WinMain (windows bindings
630 will want to import these). We use the same names as the routines used
631 by AdaMagic for compatibility. */
633 char *rts_get_hInstance (void);
634 char *rts_get_hPrevInstance (void);
635 char *rts_get_lpCommandLine (void);
636 int rts_get_nShowCmd (void);
639 rts_get_hInstance (void)
641 return (char *)GetModuleHandleA (0);
645 rts_get_hPrevInstance (void)
651 rts_get_lpCommandLine (void)
653 return GetCommandLineA ();
657 rts_get_nShowCmd (void)
665 /* This gets around a problem with using the old threads library on VMS 7.0. */
669 extern long get_gmtoff (void);
677 t = time ((time_t) 0);
679 return ts->tm_gmtoff;
683 /* Definition of __gnat_locatime_r used by a-calend.adb */
685 #if defined (__EMX__)
686 #define Lock_Task system__soft_links__lock_task
687 extern void (*Lock_Task) (void);
689 #define Unlock_Task system__soft_links__unlock_task
690 extern void (*Unlock_Task) (void);
692 /* Provide reentrant version of localtime on OS/2. */
694 extern struct tm *__gnat_localtime_r (const time_t *, struct tm *);
697 __gnat_localtime_r (const time_t *timer, struct tm *tp)
702 tmp = localtime (timer);
703 memcpy (tp, tmp, sizeof (struct tm));
709 #if defined (__Lynx__) && defined (___THREADS_POSIX4ad4__)
711 /* As of LynxOS 3.1.0a patch level 040, LynuxWorks changes the
712 prototype to the C library function localtime_r from the POSIX.4
713 Draft 9 to the POSIX 1.c version. Before this change the following
714 spec is required. Only use when ___THREADS_POSIX4ad4__ is defined,
715 the Lynx convention when building against the legacy API. */
717 extern struct tm *__gnat_localtime_r (const time_t *, struct tm *);
720 __gnat_localtime_r (const time_t *timer, struct tm *tp)
722 localtime_r (tp, timer);
727 #if defined (VMS) || defined (__MINGW32__)
729 /* __gnat_localtime_r is not needed on NT and VMS */
733 /* All other targets provide a standard localtime_r */
735 extern struct tm *__gnat_localtime_r (const time_t *, struct tm *);
738 __gnat_localtime_r (const time_t *timer, struct tm *tp)
740 return (struct tm *) localtime_r (timer, tp);