1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 1992-2004 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, 59 Temple Place - Suite 330, Boston, *
20 * MA 02111-1307, 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 /* winflusfunction is set first to the winflushinit function which will check
216 the OS version 95/98 or NT/2000 */
218 static void (*winflush_function
) (void) = winflush_init
;
220 /* This function does the runtime check of the OS version and then sets
221 winflush_function to the appropriate function and then call it. */
226 DWORD dwVersion
= GetVersion();
228 if (dwVersion
< 0x80000000) /* Windows NT/2000 */
229 winflush_function
= winflush_nt
;
230 else /* Windows 95/98 */
231 winflush_function
= winflush_95
;
233 (*winflush_function
)(); /* Perform the 'flush' */
237 static void winflush_95 (void)
239 FlushConsoleInputBuffer (GetStdHandle (STD_INPUT_HANDLE
));
242 static void winflush_nt (void)
244 /* Does nothing as there is no problem under NT. */
250 static const char *mode_read_text
= "r";
251 static const char *mode_write_text
= "w";
252 static const char *mode_append_text
= "a";
253 static const char *mode_read_binary
= "r";
254 static const char *mode_write_binary
= "w";
255 static const char *mode_append_binary
= "a";
256 static const char *mode_read_text_plus
= "r+";
257 static const char *mode_write_text_plus
= "w+";
258 static const char *mode_append_text_plus
= "a+";
259 static const char *mode_read_binary_plus
= "r+";
260 static const char *mode_write_binary_plus
= "w+";
261 static const char *mode_append_binary_plus
= "a+";
262 const char __gnat_text_translation_required
= 0;
264 /* These functions do nothing in non-DOS systems. */
267 __gnat_set_binary_mode (int handle ATTRIBUTE_UNUSED
)
272 __gnat_set_text_mode (int handle ATTRIBUTE_UNUSED
)
276 __gnat_ttyname (int filedes
)
279 extern char *ttyname (int);
281 return ttyname (filedes
);
290 #if defined (linux) || defined (sun) || defined (sgi) || defined (__EMX__) \
291 || (defined (__osf__) && ! defined (__alpha_vxworks)) || defined (WINNT) \
292 || defined (__MACHTEN__) || defined (__hpux__) || defined (_AIX) \
293 || (defined (__svr4__) && defined (i386)) || defined (__Lynx__) \
294 || defined (__CYGWIN__) || defined (__FreeBSD__)
300 #include <conio.h> /* for getch(), kbhit() */
308 extern char *decc$ga_stdscr
;
309 static int initted
= 0;
313 /* Implements the common processing for getc_immediate and
314 getc_immediate_nowait. */
316 extern void getc_immediate (FILE *, int *, int *);
317 extern void getc_immediate_nowait (FILE *, int *, int *, int *);
318 extern void getc_immediate_common (FILE *, int *, int *, int *, int);
320 /* Called by Get_Immediate (Foo); */
323 getc_immediate (FILE *stream
, int *ch
, int *end_of_file
)
327 getc_immediate_common (stream
, ch
, end_of_file
, &avail
, 1);
330 /* Called by Get_Immediate (Foo, Available); */
333 getc_immediate_nowait (FILE *stream
, int *ch
, int *end_of_file
, int *avail
)
335 getc_immediate_common (stream
, ch
, end_of_file
, avail
, 0);
338 /* Called by getc_immediate () and getc_immediate_nowait () */
341 getc_immediate_common (FILE *stream
,
347 #if defined (linux) || defined (sun) || defined (sgi) || defined (__EMX__) \
348 || (defined (__osf__) && ! defined (__alpha_vxworks)) \
349 || defined (__CYGWIN32__) || defined (__MACHTEN__) || defined (__hpux__) \
350 || defined (_AIX) || (defined (__svr4__) && defined (i386)) \
351 || defined (__Lynx__) || defined (__FreeBSD__)
355 int eof_ch
= 4; /* Ctrl-D */
356 int fd
= fileno (stream
);
357 struct termios otermios_rec
, termios_rec
;
361 tcgetattr (fd
, &termios_rec
);
362 memcpy (&otermios_rec
, &termios_rec
, sizeof (struct termios
));
364 /* Set RAW mode, with no echo */
365 termios_rec
.c_lflag
= termios_rec
.c_lflag
& ~ICANON
& ~ECHO
;
367 #if defined(linux) || defined (sun) || defined (sgi) || defined (__EMX__) \
368 || defined (__osf__) || defined (__MACHTEN__) || defined (__hpux__) \
369 || defined (_AIX) || (defined (__svr4__) && defined (i386)) \
370 || defined (__Lynx__) || defined (__FreeBSD__)
371 eof_ch
= termios_rec
.c_cc
[VEOF
];
373 /* If waiting (i.e. Get_Immediate (Char)), set MIN = 1 and wait for
374 a character forever. This doesn't seem to effect Ctrl-Z or
375 Ctrl-C processing except on OS/2 where Ctrl-C won't work right
376 unless we do a read loop. Luckily we can delay a bit between
377 iterations. If not waiting (i.e. Get_Immediate (Char, Available)),
378 don't wait for anything but timeout immediately. */
380 termios_rec
.c_cc
[VMIN
] = 0;
381 termios_rec
.c_cc
[VTIME
] = waiting
;
383 termios_rec
.c_cc
[VMIN
] = waiting
;
384 termios_rec
.c_cc
[VTIME
] = 0;
387 tcsetattr (fd
, TCSANOW
, &termios_rec
);
391 /* Read is used here instead of fread, because fread doesn't
392 work on Solaris5 and Sunos4 in this situation. Maybe because we
393 are mixing calls that use file descriptors and streams. */
394 nread
= read (fd
, &c
, 1);
397 /* On Unix terminals, Ctrl-D (EOT) is an End of File. */
405 /* Everything else is ok */
406 else if (c
!= eof_ch
)
424 tcsetattr (fd
, TCSANOW
, &otermios_rec
);
430 int fd
= fileno (stream
);
441 *ch
= decc$
bsd_wgetch (decc$ga_stdscr
);
449 decc$
bsd_nocbreak ();
452 #elif defined (__MINGW32__)
453 int fd
= fileno (stream
);
455 int eot_ch
= 4; /* Ctrl-D */
462 (*winflush_function
) ();
473 char_waiting
= kbhit();
475 if (char_waiting
== 1)
479 (*winflush_function
) ();
494 #elif defined (__vxworks)
495 /* Bit masks of file descriptors to read from. */
496 struct fd_set readFds
;
497 /* Timeout before select returns if nothing can be read. */
498 struct timeval timeOut
;
500 int fd
= fileno (stream
);
509 /* If we do not want to wait, we have to set up fd in RAW mode. This
510 should be done outside this function as setting fd in RAW mode under
511 vxWorks flushes the buffer of fd. If the RAW mode was set here, the
512 buffer would be empty and we would always return that no character
516 /* Initialization of timeOut for its use with select. */
520 /* Initialization of readFds for its use with select;
521 FD is the only file descriptor to be monitored */
523 FD_SET (fd
, &readFds
);
526 /* We do all this processing to emulate a non blocking read. */
527 readable
= select (width
, &readFds
, NULL
, NULL
, &timeOut
);
528 if (readable
== ERROR
)
529 *avail
= -1, *end_of_file
= -1;
530 /* No character available in input. */
531 else if (readable
== 0)
532 *avail
= 0, *end_of_file
= 0;
535 nread
= read (fd
, &c
, 1);
537 *avail
= 1, *end_of_file
= 0;
540 *avail
= 0, *end_of_file
= 1;
543 *avail
= -1, *end_of_file
= -1;
547 /* We have to wait until we get a character */
553 /* Save the current mode of FD. */
554 option
= ioctl (fd
, FIOGETOPTIONS
, 0);
556 /* Set FD in RAW mode. */
557 status
= ioctl (fd
, FIOSETOPTIONS
, OPT_RAW
);
560 nread
= read (fd
, &c
, 1);
562 *avail
= 1, *end_of_file
= 0;
565 *avail
= 0, *end_of_file
= 1;
566 /* Else there is an ERROR. */
569 /* Revert FD to its previous mode. */
570 status
= ioctl (fd
, FIOSETOPTIONS
, option
);
578 /* If we're not on a terminal, then we don't need any fancy processing.
579 Also this is the only thing that's left if we're not on one of the
580 supported systems; which means that for non supported systems,
581 get_immediate may wait for a carriage return on terminals. */
582 *ch
= fgetc (stream
);
596 /* The following definitions are provided in NT to support Windows based
602 /* Provide functions to echo the values passed to WinMain (windows bindings
603 will want to import these). We use the same names as the routines used
604 by AdaMagic for compatibility. */
606 char *rts_get_hInstance (void);
607 char *rts_get_hPrevInstance (void);
608 char *rts_get_lpCommandLine (void);
609 int rts_get_nShowCmd (void);
612 rts_get_hInstance (void)
614 return (char *)GetModuleHandleA (0);
618 rts_get_hPrevInstance (void)
624 rts_get_lpCommandLine (void)
626 return GetCommandLineA ();
630 rts_get_nShowCmd (void)
638 /* This gets around a problem with using the old threads library on VMS 7.0. */
642 extern long get_gmtoff (void);
650 t
= time ((time_t) 0);
652 return ts
->tm_gmtoff
;
656 /* Definition of __gnat_locatime_r used by a-calend.adb */
658 #if defined (_AIX) || defined (__EMX__)
659 #define Lock_Task system__soft_links__lock_task
660 extern void (*Lock_Task
) (void);
662 #define Unlock_Task system__soft_links__unlock_task
663 extern void (*Unlock_Task
) (void);
665 /* Provide reentrant version of localtime on Aix and OS/2. Note that AiX does
666 provide localtime_r, but in the library libc_r which doesn't get included
667 systematically, so we can't use it. */
669 extern struct tm
*__gnat_localtime_r (const time_t *, struct tm
*);
672 __gnat_localtime_r (const time_t *timer
, struct tm
*tp
)
677 tmp
= localtime (timer
);
678 memcpy (tp
, tmp
, sizeof (struct tm
));
684 #if defined (__Lynx__) && defined (___THREADS_POSIX4ad4__)
686 /* As of LynxOS 3.1.0a patch level 040, LynuxWorks changes the
687 prototype to the C library function localtime_r from the POSIX.4
688 Draft 9 to the POSIX 1.c version. Before this change the following
689 spec is required. Only use when ___THREADS_POSIX4ad4__ is defined,
690 the Lynx convention when building against the legacy API. */
692 extern struct tm
*__gnat_localtime_r (const time_t *, struct tm
*);
695 __gnat_localtime_r (const time_t *timer
, struct tm
*tp
)
697 localtime_r (tp
, timer
);
702 #if defined (VMS) || defined (__MINGW32__)
704 /* __gnat_localtime_r is not needed on NT and VMS */
708 /* All other targets provide a standard localtime_r */
710 extern struct tm
*__gnat_localtime_r (const time_t *, struct tm
*);
713 __gnat_localtime_r (const time_t *timer
, struct tm
*tp
)
715 return (struct tm
*) localtime_r (timer
, tp
);