* c-common.c (get_priority): Add check for
[official-gcc.git] / gcc / ada / sysdep.c
blob0562766a9e5becefd26acb12706e97fa2d55d46a
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * S Y S D E P *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2006, Free Software Foundation, Inc. *
10 * *
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. *
21 * *
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. *
27 * *
28 * GNAT was originally developed by the GNAT team at New York University. *
29 * Extensive contributions were provided by Ada Core Technologies Inc. *
30 * *
31 ****************************************************************************/
33 /* This file contains system dependent symbols that are referenced in the
34 GNAT Run Time Library */
36 #ifdef __vxworks
37 #include "ioLib.h"
38 #include "selectLib.h"
39 #include "vxWorks.h"
40 #endif
41 #ifdef IN_RTS
42 #define POSIX
43 #include "tconfig.h"
44 #include "tsystem.h"
45 #include <fcntl.h>
46 #include <sys/stat.h>
47 #ifdef VMS
48 #include <unixio.h>
49 #endif
50 #else
51 #include "config.h"
52 #include "system.h"
53 #endif
55 #include <time.h>
57 #if defined (sun) && defined (__SVR4) && !defined (__vxworks)
58 /* The declaration is present in <time.h> but conditionalized
59 on a couple of macros we don't define. */
60 extern struct tm *localtime_r(const time_t *, struct tm *);
61 #endif
63 #include "adaint.h"
66 mode_read_text
67 open text file for reading
68 rt for DOS and Windows NT, r for Unix
70 mode_write_text
71 truncate to zero length or create text file for writing
72 wt for DOS and Windows NT, w for Unix
74 mode_append_text
75 append; open or create text file for writing at end-of-file
76 at for DOS and Windows NT, a for Unix
78 mode_read_binary
79 open binary file for reading
80 rb for DOS and Windows NT, r for Unix
82 mode_write_binary
83 truncate to zero length or create binary file for writing
84 wb for DOS and Windows NT, w for Unix
86 mode_append_binary
87 append; open or create binary file for writing at end-of-file
88 ab for DOS and Windows NT, a for Unix
90 mode_read_text_plus
91 open text file for update (reading and writing)
92 r+t for DOS and Windows NT, r+ for Unix
94 mode_write_text_plus
95 truncate to zero length or create text file for update
96 w+t for DOS and Windows NT, w+ for Unix
98 mode_append_text_plus
99 append; open or create text file for update, writing at end-of-file
100 a+t for DOS and Windows NT, a+ for Unix
102 mode_read_binary_plus
103 open binary file for update (reading and writing)
104 r+b for DOS and Windows NT, r+ for Unix
106 mode_write_binary_plus
107 truncate to zero length or create binary file for update
108 w+b for DOS and Windows NT, w+ for Unix
110 mode_append_binary_plus
111 append; open or create binary file for update, writing at end-of-file
112 a+b for DOS and Windows NT, a+ for Unix
114 Notes:
116 (1) Opening a file with read mode fails if the file does not exist or
117 cannot be read.
119 (2) Opening a file with append mode causes all subsequent writes to the
120 file to be forced to the then current end-of-file, regardless of
121 intervening calls to the fseek function.
123 (3) When a file is opened with update mode, both input and output may be
124 performed on the associated stream. However, output may not be directly
125 followed by input without an intervening call to the fflush function or
126 to a file positioning function (fseek, fsetpos, or rewind), and input
127 may not be directly followed by output without an intervening call to a
128 file positioning function, unless the input operation encounters
129 end-of-file.
131 The other target dependent declarations here are for the two functions
132 __gnat_set_binary_mode and __gnat_set_text_mode:
134 void __gnat_set_binary_mode (int handle);
135 void __gnat_set_text_mode (int handle);
137 These functions have no effect in Unix (or similar systems where there is
138 no distinction between binary and text files), but in DOS (and similar
139 systems where text mode does CR/LF translation), these functions allow
140 the mode of the stream with the given handle (fileno can be used to get
141 the handle of a stream) to be changed dynamically. The returned result
142 is 0 if no error occurs and -1 if an error occurs.
144 Finally there is a boolean (character) variable
146 char __gnat_text_translation_required;
148 which is zero (false) in Unix mode, and one (true) in DOS mode, with a
149 true value indicating that text translation is required on text files
150 and that fopen supports the trailing t and b modifiers.
154 #if defined(WINNT) || defined (MSDOS) || defined (__EMX__)
155 static const char *mode_read_text = "rt";
156 static const char *mode_write_text = "wt";
157 static const char *mode_append_text = "at";
158 static const char *mode_read_binary = "rb";
159 static const char *mode_write_binary = "wb";
160 static const char *mode_append_binary = "ab";
161 static const char *mode_read_text_plus = "r+t";
162 static const char *mode_write_text_plus = "w+t";
163 static const char *mode_append_text_plus = "a+t";
164 static const char *mode_read_binary_plus = "r+b";
165 static const char *mode_write_binary_plus = "w+b";
166 static const char *mode_append_binary_plus = "a+b";
167 const char __gnat_text_translation_required = 1;
169 void
170 __gnat_set_binary_mode (int handle)
172 _setmode (handle, O_BINARY);
175 void
176 __gnat_set_text_mode (int handle)
178 _setmode (handle, O_TEXT);
181 #ifdef __MINGW32__
182 #include <windows.h>
184 /* Return the name of the tty. Under windows there is no name for
185 the tty, so this function, if connected to a tty, returns the generic name
186 "console". */
188 char *
189 __gnat_ttyname (int filedes)
191 if (isatty (filedes))
192 return "console";
193 else
194 return NULL;
197 /* This function is needed to fix a bug under Win95/98. Under these platforms
198 doing :
199 ch1 = getch();
200 ch2 = fgetc (stdin);
202 will put the same character into ch1 and ch2. It seem that the character
203 read by getch() is not correctly removed from the buffer. Even a
204 fflush(stdin) does not fix the bug. This bug does not appear under Window
205 NT. So we have two version of this routine below one for 95/98 and one for
206 NT/2000 version of Windows. There is also a special routine (winflushinit)
207 that will be called only the first time to check which version of Windows
208 we are running running on to set the right routine to use.
210 This problem occurs when using Text_IO.Get_Line after Text_IO.Get_Immediate
211 for example.
213 Calling FlushConsoleInputBuffer just after getch() fix the bug under
214 95/98. */
216 static void winflush_init (void);
218 static void winflush_95 (void);
220 static void winflush_nt (void);
222 int __gnat_is_windows_xp (void);
224 /* winflusfunction is set first to the winflushinit function which will check
225 the OS version 95/98 or NT/2000 */
227 static void (*winflush_function) (void) = winflush_init;
229 /* This function does the runtime check of the OS version and then sets
230 winflush_function to the appropriate function and then call it. */
232 static void
233 winflush_init (void)
235 DWORD dwVersion = GetVersion();
237 if (dwVersion < 0x80000000) /* Windows NT/2000 */
238 winflush_function = winflush_nt;
239 else /* Windows 95/98 */
240 winflush_function = winflush_95;
242 (*winflush_function)(); /* Perform the 'flush' */
246 static void
247 winflush_95 (void)
249 FlushConsoleInputBuffer (GetStdHandle (STD_INPUT_HANDLE));
252 static void
253 winflush_nt (void)
255 /* Does nothing as there is no problem under NT. */
259 __gnat_is_windows_xp (void)
261 static int is_win_xp=0, is_win_xp_checked=0;
263 if (!is_win_xp_checked)
265 OSVERSIONINFO version;
267 is_win_xp_checked = 1;
269 memset (&version, 0, sizeof (version));
270 version.dwOSVersionInfoSize = sizeof (version);
272 is_win_xp = GetVersionEx (&version)
273 && version.dwPlatformId == VER_PLATFORM_WIN32_NT
274 && (version.dwMajorVersion > 5
275 || (version.dwMajorVersion == 5 && version.dwMinorVersion >= 1));
277 return is_win_xp;
280 #endif
282 #else
284 static const char *mode_read_text = "r";
285 static const char *mode_write_text = "w";
286 static const char *mode_append_text = "a";
287 static const char *mode_read_binary = "r";
288 static const char *mode_write_binary = "w";
289 static const char *mode_append_binary = "a";
290 static const char *mode_read_text_plus = "r+";
291 static const char *mode_write_text_plus = "w+";
292 static const char *mode_append_text_plus = "a+";
293 static const char *mode_read_binary_plus = "r+";
294 static const char *mode_write_binary_plus = "w+";
295 static const char *mode_append_binary_plus = "a+";
296 const char __gnat_text_translation_required = 0;
298 /* These functions do nothing in non-DOS systems. */
300 void
301 __gnat_set_binary_mode (int handle ATTRIBUTE_UNUSED)
305 void
306 __gnat_set_text_mode (int handle ATTRIBUTE_UNUSED)
309 char *
310 __gnat_ttyname (int filedes)
312 #ifndef __vxworks
313 extern char *ttyname (int);
315 return ttyname (filedes);
317 #else
318 return "";
320 #endif
322 #endif
324 #if defined (linux) || defined (sun) || defined (sgi) || defined (__EMX__) \
325 || (defined (__osf__) && ! defined (__alpha_vxworks)) || defined (WINNT) \
326 || defined (__MACHTEN__) || defined (__hpux__) || defined (_AIX) \
327 || (defined (__svr4__) && defined (i386)) || defined (__Lynx__) \
328 || defined (__CYGWIN__) || defined (__FreeBSD__)
330 #ifdef __MINGW32__
331 #if OLD_MINGW
332 #include <termios.h>
333 #else
334 #include <conio.h> /* for getch(), kbhit() */
335 #endif
336 #else
337 #include <termios.h>
338 #endif
340 #else
341 #if defined (VMS)
342 extern char *decc$ga_stdscr;
343 static int initted = 0;
344 #endif
345 #endif
347 /* Implements the common processing for getc_immediate and
348 getc_immediate_nowait. */
350 extern void getc_immediate (FILE *, int *, int *);
351 extern void getc_immediate_nowait (FILE *, int *, int *, int *);
352 extern void getc_immediate_common (FILE *, int *, int *, int *, int);
354 /* Called by Get_Immediate (Foo); */
356 void
357 getc_immediate (FILE *stream, int *ch, int *end_of_file)
359 int avail;
361 getc_immediate_common (stream, ch, end_of_file, &avail, 1);
364 /* Called by Get_Immediate (Foo, Available); */
366 void
367 getc_immediate_nowait (FILE *stream, int *ch, int *end_of_file, int *avail)
369 getc_immediate_common (stream, ch, end_of_file, avail, 0);
372 /* Called by getc_immediate () and getc_immediate_nowait () */
374 void
375 getc_immediate_common (FILE *stream,
376 int *ch,
377 int *end_of_file,
378 int *avail,
379 int waiting)
381 #if defined (linux) || defined (sun) || defined (sgi) || defined (__EMX__) \
382 || (defined (__osf__) && ! defined (__alpha_vxworks)) \
383 || defined (__CYGWIN32__) || defined (__MACHTEN__) || defined (__hpux__) \
384 || defined (_AIX) || (defined (__svr4__) && defined (i386)) \
385 || defined (__Lynx__) || defined (__FreeBSD__)
386 char c;
387 int nread;
388 int good_one = 0;
389 int eof_ch = 4; /* Ctrl-D */
390 int fd = fileno (stream);
391 struct termios otermios_rec, termios_rec;
393 if (isatty (fd))
395 tcgetattr (fd, &termios_rec);
396 memcpy (&otermios_rec, &termios_rec, sizeof (struct termios));
398 /* Set RAW mode, with no echo */
399 termios_rec.c_lflag = termios_rec.c_lflag & ~ICANON & ~ECHO;
401 #if defined(linux) || defined (sun) || defined (sgi) || defined (__EMX__) \
402 || defined (__osf__) || defined (__MACHTEN__) || defined (__hpux__) \
403 || defined (_AIX) || (defined (__svr4__) && defined (i386)) \
404 || defined (__Lynx__) || defined (__FreeBSD__)
405 eof_ch = termios_rec.c_cc[VEOF];
407 /* If waiting (i.e. Get_Immediate (Char)), set MIN = 1 and wait for
408 a character forever. This doesn't seem to effect Ctrl-Z or
409 Ctrl-C processing except on OS/2 where Ctrl-C won't work right
410 unless we do a read loop. Luckily we can delay a bit between
411 iterations. If not waiting (i.e. Get_Immediate (Char, Available)),
412 don't wait for anything but timeout immediately. */
413 #ifdef __EMX__
414 termios_rec.c_cc[VMIN] = 0;
415 termios_rec.c_cc[VTIME] = waiting;
416 #else
417 termios_rec.c_cc[VMIN] = waiting;
418 termios_rec.c_cc[VTIME] = 0;
419 #endif
420 #endif
421 tcsetattr (fd, TCSANOW, &termios_rec);
423 while (! good_one)
425 /* Read is used here instead of fread, because fread doesn't
426 work on Solaris5 and Sunos4 in this situation. Maybe because we
427 are mixing calls that use file descriptors and streams. */
428 nread = read (fd, &c, 1);
429 if (nread > 0)
431 /* On Unix terminals, Ctrl-D (EOT) is an End of File. */
432 if (c == eof_ch)
434 *avail = 0;
435 *end_of_file = 1;
436 good_one = 1;
439 /* Everything else is ok */
440 else if (c != eof_ch)
442 *avail = 1;
443 *end_of_file = 0;
444 good_one = 1;
448 else if (! waiting)
450 *avail = 0;
451 *end_of_file = 0;
452 good_one = 1;
454 else
455 good_one = 0;
458 tcsetattr (fd, TCSANOW, &otermios_rec);
459 *ch = c;
462 else
463 #elif defined (VMS)
464 int fd = fileno (stream);
466 if (isatty (fd))
468 if (initted == 0)
470 decc$bsd_initscr ();
471 initted = 1;
474 decc$bsd_cbreak ();
475 *ch = decc$bsd_wgetch (decc$ga_stdscr);
477 if (*ch == 4)
478 *end_of_file = 1;
479 else
480 *end_of_file = 0;
482 *avail = 1;
483 decc$bsd_nocbreak ();
485 else
486 #elif defined (__MINGW32__)
487 int fd = fileno (stream);
488 int char_waiting;
489 int eot_ch = 4; /* Ctrl-D */
491 if (isatty (fd))
493 if (waiting)
495 *ch = getch ();
496 (*winflush_function) ();
498 if (*ch == eot_ch)
499 *end_of_file = 1;
500 else
501 *end_of_file = 0;
503 *avail = 1;
505 else /* ! waiting */
507 char_waiting = kbhit();
509 if (char_waiting == 1)
511 *avail = 1;
512 *ch = getch ();
513 (*winflush_function) ();
515 if (*ch == eot_ch)
516 *end_of_file = 1;
517 else
518 *end_of_file = 0;
520 else
522 *avail = 0;
523 *end_of_file = 0;
527 else
528 #elif defined (__vxworks)
529 /* Bit masks of file descriptors to read from. */
530 struct fd_set readFds;
531 /* Timeout before select returns if nothing can be read. */
532 struct timeval timeOut;
533 char c;
534 int fd = fileno (stream);
535 int nread;
536 int option;
537 int readable;
538 int status;
539 int width;
541 if (isatty (fd))
543 /* If we do not want to wait, we have to set up fd in RAW mode. This
544 should be done outside this function as setting fd in RAW mode under
545 vxWorks flushes the buffer of fd. If the RAW mode was set here, the
546 buffer would be empty and we would always return that no character
547 is available */
548 if (! waiting)
550 /* Initialization of timeOut for its use with select. */
551 timeOut.tv_sec = 0;
552 timeOut.tv_usec = 0;
554 /* Initialization of readFds for its use with select;
555 FD is the only file descriptor to be monitored */
556 FD_ZERO (&readFds);
557 FD_SET (fd, &readFds);
558 width = 2;
560 /* We do all this processing to emulate a non blocking read. */
561 readable = select (width, &readFds, NULL, NULL, &timeOut);
562 if (readable == ERROR)
563 *avail = -1, *end_of_file = -1;
564 /* No character available in input. */
565 else if (readable == 0)
566 *avail = 0, *end_of_file = 0;
567 else
569 nread = read (fd, &c, 1);
570 if (nread > 0)
571 *avail = 1, *end_of_file = 0;
572 /* End Of File. */
573 else if (nread == 0)
574 *avail = 0, *end_of_file = 1;
575 /* Error. */
576 else
577 *avail = -1, *end_of_file = -1;
581 /* We have to wait until we get a character */
582 else
584 *avail = -1;
585 *end_of_file = -1;
587 /* Save the current mode of FD. */
588 option = ioctl (fd, FIOGETOPTIONS, 0);
590 /* Set FD in RAW mode. */
591 status = ioctl (fd, FIOSETOPTIONS, OPT_RAW);
592 if (status != -1)
594 nread = read (fd, &c, 1);
595 if (nread > 0)
596 *avail = 1, *end_of_file = 0;
597 /* End of file. */
598 else if (nread == 0)
599 *avail = 0, *end_of_file = 1;
600 /* Else there is an ERROR. */
603 /* Revert FD to its previous mode. */
604 status = ioctl (fd, FIOSETOPTIONS, option);
607 *ch = c;
609 else
610 #endif
612 /* If we're not on a terminal, then we don't need any fancy processing.
613 Also this is the only thing that's left if we're not on one of the
614 supported systems; which means that for non supported systems,
615 get_immediate may wait for a carriage return on terminals. */
616 *ch = fgetc (stream);
617 if (feof (stream))
619 *end_of_file = 1;
620 *avail = 0;
622 else
624 *end_of_file = 0;
625 *avail = 1;
630 /* The following definitions are provided in NT to support Windows based
631 Ada programs. */
633 #ifdef WINNT
634 #include <windows.h>
636 /* Provide functions to echo the values passed to WinMain (windows bindings
637 will want to import these). We use the same names as the routines used
638 by AdaMagic for compatibility. */
640 char *rts_get_hInstance (void);
641 char *rts_get_hPrevInstance (void);
642 char *rts_get_lpCommandLine (void);
643 int rts_get_nShowCmd (void);
645 char *
646 rts_get_hInstance (void)
648 return (char *)GetModuleHandleA (0);
651 char *
652 rts_get_hPrevInstance (void)
654 return 0;
657 char *
658 rts_get_lpCommandLine (void)
660 return GetCommandLineA ();
664 rts_get_nShowCmd (void)
666 return 1;
669 #endif /* WINNT */
670 #ifdef VMS
672 /* This gets around a problem with using the old threads library on VMS 7.0. */
674 extern long get_gmtoff (void);
676 long
677 get_gmtoff (void)
679 time_t t;
680 struct tm *ts;
682 t = time ((time_t) 0);
683 ts = localtime (&t);
684 return ts->tm_gmtoff;
686 #endif
688 /* This value is returned as the time zone offset when a valid value
689 cannot be determined. It is simply a bizarre value that will never
690 occur. It is 3 days plus 73 seconds (offset is in seconds. */
692 long __gnat_invalid_tzoff = 259273;
694 /* Definition of __gnat_locatime_r used by a-calend.adb */
696 #if defined (__EMX__) || defined (__MINGW32__)
698 #ifdef CERT
700 /* For the Cert run times on native Windows we use dummy functions
701 for locking and unlocking tasks since we do not support multiple
702 threads on this configuration (Cert run time on native Windows). */
704 void dummy (void) {}
706 void (*Lock_Task) () = &dummy;
707 void (*Unlock_Task) () = &dummy;
709 #else
711 #define Lock_Task system__soft_links__lock_task
712 extern void (*Lock_Task) (void);
714 #define Unlock_Task system__soft_links__unlock_task
715 extern void (*Unlock_Task) (void);
717 #endif
719 /* Reentrant localtime for Windows and OS/2. */
721 extern struct tm *
722 __gnat_localtime_tzoff (const time_t *, struct tm *, long *);
724 struct tm *
725 __gnat_localtime_tzoff (const time_t *timer, struct tm *tp, long *off)
727 DWORD dwRet;
728 struct tm *tmp;
729 TIME_ZONE_INFORMATION tzi;
731 (*Lock_Task) ();
732 tmp = localtime (timer);
733 memcpy (tp, tmp, sizeof (struct tm));
734 dwRet = GetTimeZoneInformation (&tzi);
735 *off = tzi.Bias;
736 if (tp->tm_isdst > 0)
737 *off = *off + tzi.DaylightBias;
738 *off = *off * -60;
739 (*Unlock_Task) ();
740 return tp;
743 #else
744 #if defined (__Lynx__) && defined (___THREADS_POSIX4ad4__)
746 /* As of LynxOS 3.1.0a patch level 040, LynuxWorks changes the
747 prototype to the C library function localtime_r from the POSIX.4
748 Draft 9 to the POSIX 1.c version. Before this change the following
749 spec is required. Only use when ___THREADS_POSIX4ad4__ is defined,
750 the Lynx convention when building against the legacy API. */
752 extern struct tm *
753 __gnat_localtime_tzoff (const time_t *, struct tm *, long *);
755 struct tm *
756 __gnat_localtime_tzoff (const time_t *timer, struct tm *tp, long *off)
758 localtime_r (tp, timer);
759 *off = __gnat_invalid_tzoff;
760 return NULL;
763 #else
764 #if defined (VMS)
766 /* __gnat_localtime_tzoff is not needed on VMS */
768 #else
770 /* All other targets provide a standard localtime_r */
772 extern struct tm *
773 __gnat_localtime_tzoff (const time_t *, struct tm *, long *);
775 struct tm *
776 __gnat_localtime_tzoff (const time_t *timer, struct tm *tp, long *off)
778 localtime_r (timer, tp);
780 /* AIX, HPUX, SGI Irix, Sun Solaris */
781 #if defined (_AIX) || defined (__hpux__) || defined (sgi) || defined (sun)
782 *off = (long) -timezone;
783 if (tp->tm_isdst > 0)
784 *off = *off + 3600;
786 /* Lynx, VXWorks */
787 #elif defined (__Lynx__) || defined (__vxworks)
788 *off = __gnat_invalid_tzoff;
790 /* Darwin, Free BSD, Linux, Tru64 */
791 #else
792 *off = tp->tm_gmtoff;
793 #endif
794 return NULL;
797 #endif
798 #endif
799 #endif