1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 1992-2011, 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 3, 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. *
18 * As a special exception under Section 7 of GPL version 3, you are granted *
19 * additional permissions described in the GCC Runtime Library Exception, *
20 * version 3.1, as published by the Free Software Foundation. *
22 * You should have received a copy of the GNU General Public License and *
23 * a copy of the GCC Runtime Library Exception along with this program; *
24 * see the files COPYING3 and COPYING.RUNTIME respectively. If not, see *
25 * <http://www.gnu.org/licenses/>. *
27 * GNAT was originally developed by the GNAT team at New York University. *
28 * Extensive contributions were provided by Ada Core Technologies Inc. *
30 ****************************************************************************/
32 /* This file contains system dependent symbols that are referenced in the
33 GNAT Run Time Library */
37 #if ! defined (VTHREADS)
40 #if ! defined (__RTP__) && (! defined (VTHREADS) || defined (__VXWORKSMILS__))
43 #include "selectLib.h"
64 #if defined (sun) && defined (__SVR4) && !defined (__vxworks)
65 /* The declaration is present in <time.h> but conditionalized
66 on a couple of macros we don't define. */
67 extern struct tm *localtime_r(const time_t *, struct tm *);
72 /* Don't use macros versions of this functions on VxWorks since they cause
73 imcompatible changes in some VxWorks versions */
84 open text file for reading
85 rt for DOS and Windows NT, r for Unix
88 truncate to zero length or create text file for writing
89 wt for DOS and Windows NT, w for Unix
92 append; open or create text file for writing at end-of-file
93 at for DOS and Windows NT, a for Unix
96 open binary file for reading
97 rb for DOS and Windows NT, r for Unix
100 truncate to zero length or create binary file for writing
101 wb for DOS and Windows NT, w for Unix
104 append; open or create binary file for writing at end-of-file
105 ab for DOS and Windows NT, a for Unix
108 open text file for update (reading and writing)
109 r+t for DOS and Windows NT, r+ for Unix
112 truncate to zero length or create text file for update
113 w+t for DOS and Windows NT, w+ for Unix
115 mode_append_text_plus
116 append; open or create text file for update, writing at end-of-file
117 a+t for DOS and Windows NT, a+ for Unix
119 mode_read_binary_plus
120 open binary file for update (reading and writing)
121 r+b for DOS and Windows NT, r+ for Unix
123 mode_write_binary_plus
124 truncate to zero length or create binary file for update
125 w+b for DOS and Windows NT, w+ for Unix
127 mode_append_binary_plus
128 append; open or create binary file for update, writing at end-of-file
129 a+b for DOS and Windows NT, a+ for Unix
133 (1) Opening a file with read mode fails if the file does not exist or
136 (2) Opening a file with append mode causes all subsequent writes to the
137 file to be forced to the then current end-of-file, regardless of
138 intervening calls to the fseek function.
140 (3) When a file is opened with update mode, both input and output may be
141 performed on the associated stream. However, output may not be directly
142 followed by input without an intervening call to the fflush function or
143 to a file positioning function (fseek, fsetpos, or rewind), and input
144 may not be directly followed by output without an intervening call to a
145 file positioning function, unless the input operation encounters
148 The other target dependent declarations here are for the two functions
149 __gnat_set_binary_mode and __gnat_set_text_mode:
151 void __gnat_set_binary_mode (int handle);
152 void __gnat_set_text_mode (int handle);
154 These functions have no effect in Unix (or similar systems where there is
155 no distinction between binary and text files), but in DOS (and similar
156 systems where text mode does CR/LF translation), these functions allow
157 the mode of the stream with the given handle (fileno can be used to get
158 the handle of a stream) to be changed dynamically. The returned result
159 is 0 if no error occurs and -1 if an error occurs.
161 Finally there is a boolean (character) variable
163 char __gnat_text_translation_required;
165 which is zero (false) in Unix mode, and one (true) in DOS mode, with a
166 true value indicating that text translation is required on text files
167 and that fopen supports the trailing t and b modifiers.
172 static const char *mode_read_text = "rt";
173 static const char *mode_write_text = "wt";
174 static const char *mode_append_text = "at";
175 static const char *mode_read_binary = "rb";
176 static const char *mode_write_binary = "wb";
177 static const char *mode_append_binary = "ab";
178 static const char *mode_read_text_plus = "r+t";
179 static const char *mode_write_text_plus = "w+t";
180 static const char *mode_append_text_plus = "a+t";
181 static const char *mode_read_binary_plus = "r+b";
182 static const char *mode_write_binary_plus = "w+b";
183 static const char *mode_append_binary_plus = "a+b";
184 const char __gnat_text_translation_required = 1;
187 __gnat_set_binary_mode (int handle)
189 _setmode (handle, O_BINARY);
193 __gnat_set_text_mode (int handle)
195 _setmode (handle, O_TEXT);
201 /* Return the name of the tty. Under windows there is no name for
202 the tty, so this function, if connected to a tty, returns the generic name
206 __gnat_ttyname (int filedes)
208 if (isatty (filedes))
214 /* This function is needed to fix a bug under Win95/98. Under these platforms
219 will put the same character into ch1 and ch2. It seem that the character
220 read by getch() is not correctly removed from the buffer. Even a
221 fflush(stdin) does not fix the bug. This bug does not appear under Window
222 NT. So we have two version of this routine below one for 95/98 and one for
223 NT/2000 version of Windows. There is also a special routine (winflushinit)
224 that will be called only the first time to check which version of Windows
225 we are running running on to set the right routine to use.
227 This problem occurs when using Text_IO.Get_Line after Text_IO.Get_Immediate
230 Calling FlushConsoleInputBuffer just after getch() fix the bug under
235 static void winflush_nt (void);
237 /* winflush_function will do nothing since we only have problems with Windows
238 95/98 which are not supported by RTX. */
240 static void (*winflush_function) (void) = winflush_nt;
245 /* Does nothing as there is no problem under NT. */
250 static void winflush_init (void);
252 static void winflush_95 (void);
254 static void winflush_nt (void);
256 int __gnat_is_windows_xp (void);
258 /* winflusfunction is set first to the winflushinit function which will check
259 the OS version 95/98 or NT/2000 */
261 static void (*winflush_function) (void) = winflush_init;
263 /* This function does the runtime check of the OS version and then sets
264 winflush_function to the appropriate function and then call it. */
269 DWORD dwVersion = GetVersion();
271 if (dwVersion < 0x80000000) /* Windows NT/2000 */
272 winflush_function = winflush_nt;
273 else /* Windows 95/98 */
274 winflush_function = winflush_95;
276 (*winflush_function)(); /* Perform the 'flush' */
283 FlushConsoleInputBuffer (GetStdHandle (STD_INPUT_HANDLE));
289 /* Does nothing as there is no problem under NT. */
293 __gnat_is_windows_xp (void)
295 static int is_win_xp=0, is_win_xp_checked=0;
297 if (!is_win_xp_checked)
299 OSVERSIONINFO version;
301 is_win_xp_checked = 1;
303 memset (&version, 0, sizeof (version));
304 version.dwOSVersionInfoSize = sizeof (version);
306 is_win_xp = GetVersionEx (&version)
307 && version.dwPlatformId == VER_PLATFORM_WIN32_NT
308 && (version.dwMajorVersion > 5
309 || (version.dwMajorVersion == 5 && version.dwMinorVersion >= 1));
316 /* Get the bounds of the stack. The stack pointer is supposed to be
317 initialized to BASE when a thread is created and the stack can be extended
318 to LIMIT before reaching a guard page.
319 Note: for the main thread, the system automatically extend the stack, so
320 LIMIT is only the current limit. */
323 __gnat_get_stack_bounds (void **base, void **limit)
327 /* We know that the first field of the TEB is the TIB. */
328 tib = (NT_TIB *)NtCurrentTeb ();
330 *base = tib->StackBase;
331 *limit = tib->StackLimit;
334 #endif /* !__MINGW32__ */
338 static const char *mode_read_text = "r";
339 static const char *mode_write_text = "w";
340 static const char *mode_append_text = "a";
341 static const char *mode_read_binary = "r";
342 static const char *mode_write_binary = "w";
343 static const char *mode_append_binary = "a";
344 static const char *mode_read_text_plus = "r+";
345 static const char *mode_write_text_plus = "w+";
346 static const char *mode_append_text_plus = "a+";
347 static const char *mode_read_binary_plus = "r+";
348 static const char *mode_write_binary_plus = "w+";
349 static const char *mode_append_binary_plus = "a+";
350 const char __gnat_text_translation_required = 0;
352 /* These functions do nothing in non-DOS systems. */
355 __gnat_set_binary_mode (int handle ATTRIBUTE_UNUSED)
360 __gnat_set_text_mode (int handle ATTRIBUTE_UNUSED)
364 __gnat_ttyname (int filedes)
366 #if defined (__vxworks) || defined (__nucleus)
369 extern char *ttyname (int);
371 return ttyname (filedes);
372 #endif /* defined (__vxworks) || defined (__nucleus) */
376 #if defined (linux) || defined (sun) || defined (sgi) \
377 || (defined (__osf__) && ! defined (__alpha_vxworks)) || defined (WINNT) \
378 || defined (__MACHTEN__) || defined (__hpux__) || defined (_AIX) \
379 || (defined (__svr4__) && defined (i386)) || defined (__Lynx__) \
380 || defined (__CYGWIN__) || defined (__FreeBSD__) || defined (__OpenBSD__) \
381 || defined (__GLIBC__) || defined (__APPLE__)
387 #include <conio.h> /* for getch(), kbhit() */
395 extern char *decc$ga_stdscr;
396 static int initted = 0;
400 /* Implements the common processing for getc_immediate and
401 getc_immediate_nowait. */
403 extern void getc_immediate (FILE *, int *, int *);
404 extern void getc_immediate_nowait (FILE *, int *, int *, int *);
405 extern void getc_immediate_common (FILE *, int *, int *, int *, int);
407 /* Called by Get_Immediate (Foo); */
410 getc_immediate (FILE *stream, int *ch, int *end_of_file)
414 getc_immediate_common (stream, ch, end_of_file, &avail, 1);
417 /* Called by Get_Immediate (Foo, Available); */
420 getc_immediate_nowait (FILE *stream, int *ch, int *end_of_file, int *avail)
422 getc_immediate_common (stream, ch, end_of_file, avail, 0);
425 /* Called by getc_immediate () and getc_immediate_nowait () */
428 getc_immediate_common (FILE *stream,
434 #if defined (linux) || defined (sun) || defined (sgi) \
435 || (defined (__osf__) && ! defined (__alpha_vxworks)) \
436 || defined (__CYGWIN32__) || defined (__MACHTEN__) || defined (__hpux__) \
437 || defined (_AIX) || (defined (__svr4__) && defined (i386)) \
438 || defined (__Lynx__) || defined (__FreeBSD__) || defined (__OpenBSD__) \
439 || defined (__GLIBC__) || defined (__APPLE__)
443 int eof_ch = 4; /* Ctrl-D */
444 int fd = fileno (stream);
445 struct termios otermios_rec, termios_rec;
449 tcgetattr (fd, &termios_rec);
450 memcpy (&otermios_rec, &termios_rec, sizeof (struct termios));
452 /* Set RAW mode, with no echo */
453 termios_rec.c_lflag = termios_rec.c_lflag & ~ICANON & ~ECHO;
455 #if defined(linux) || defined (sun) || defined (sgi) \
456 || defined (__osf__) || defined (__MACHTEN__) || defined (__hpux__) \
457 || defined (_AIX) || (defined (__svr4__) && defined (i386)) \
458 || defined (__Lynx__) || defined (__FreeBSD__) || defined (__OpenBSD__) \
459 || defined (__GLIBC__) || defined (__APPLE__)
460 eof_ch = termios_rec.c_cc[VEOF];
462 /* If waiting (i.e. Get_Immediate (Char)), set MIN = 1 and wait for
463 a character forever. This doesn't seem to effect Ctrl-Z or
465 If not waiting (i.e. Get_Immediate (Char, Available)),
466 don't wait for anything but timeout immediately. */
467 termios_rec.c_cc[VMIN] = waiting;
468 termios_rec.c_cc[VTIME] = 0;
470 tcsetattr (fd, TCSANOW, &termios_rec);
474 /* Read is used here instead of fread, because fread doesn't
475 work on Solaris5 and Sunos4 in this situation. Maybe because we
476 are mixing calls that use file descriptors and streams. */
477 nread = read (fd, &c, 1);
480 /* On Unix terminals, Ctrl-D (EOT) is an End of File. */
488 /* Everything else is ok */
489 else if (c != eof_ch)
507 tcsetattr (fd, TCSANOW, &otermios_rec);
513 int fd = fileno (stream);
524 *ch = decc$bsd_wgetch (decc$ga_stdscr);
532 decc$bsd_nocbreak ();
535 #elif defined (__MINGW32__)
536 int fd = fileno (stream);
538 int eot_ch = 4; /* Ctrl-D */
545 (*winflush_function) ();
556 char_waiting = kbhit();
558 if (char_waiting == 1)
562 (*winflush_function) ();
577 #elif defined (__vxworks)
578 /* Bit masks of file descriptors to read from. */
579 struct fd_set readFds;
580 /* Timeout before select returns if nothing can be read. */
581 struct timeval timeOut;
583 int fd = fileno (stream);
592 /* If we do not want to wait, we have to set up fd in RAW mode. This
593 should be done outside this function as setting fd in RAW mode under
594 vxWorks flushes the buffer of fd. If the RAW mode was set here, the
595 buffer would be empty and we would always return that no character
599 /* Initialization of timeOut for its use with select. */
603 /* Initialization of readFds for its use with select;
604 FD is the only file descriptor to be monitored */
606 FD_SET (fd, &readFds);
609 /* We do all this processing to emulate a non blocking read. */
610 readable = select (width, &readFds, NULL, NULL, &timeOut);
611 if (readable == ERROR)
612 *avail = -1, *end_of_file = -1;
613 /* No character available in input. */
614 else if (readable == 0)
615 *avail = 0, *end_of_file = 0;
618 nread = read (fd, &c, 1);
620 *avail = 1, *end_of_file = 0;
623 *avail = 0, *end_of_file = 1;
626 *avail = -1, *end_of_file = -1;
630 /* We have to wait until we get a character */
636 /* Save the current mode of FD. */
637 option = ioctl (fd, FIOGETOPTIONS, 0);
639 /* Set FD in RAW mode. */
640 status = ioctl (fd, FIOSETOPTIONS, OPT_RAW);
643 nread = read (fd, &c, 1);
645 *avail = 1, *end_of_file = 0;
648 *avail = 0, *end_of_file = 1;
649 /* Else there is an ERROR. */
652 /* Revert FD to its previous mode. */
653 status = ioctl (fd, FIOSETOPTIONS, option);
661 /* If we're not on a terminal, then we don't need any fancy processing.
662 Also this is the only thing that's left if we're not on one of the
663 supported systems; which means that for non supported systems,
664 get_immediate may wait for a carriage return on terminals. */
665 *ch = fgetc (stream);
679 /* The following definitions are provided in NT to support Windows based
685 /* Provide functions to echo the values passed to WinMain (windows bindings
686 will want to import these). We use the same names as the routines used
687 by AdaMagic for compatibility. */
689 char *rts_get_hInstance (void);
690 char *rts_get_hPrevInstance (void);
691 char *rts_get_lpCommandLine (void);
692 int rts_get_nShowCmd (void);
695 rts_get_hInstance (void)
697 return (char *)GetModuleHandleA (0);
701 rts_get_hPrevInstance (void)
707 rts_get_lpCommandLine (void)
709 return GetCommandLineA ();
713 rts_get_nShowCmd (void)
721 /* This gets around a problem with using the old threads library on VMS 7.0. */
723 extern long get_gmtoff (void);
731 t = time ((time_t) 0);
733 return ts->tm_gmtoff;
737 /* This value is returned as the time zone offset when a valid value
738 cannot be determined. It is simply a bizarre value that will never
739 occur. It is 3 days plus 73 seconds (offset is in seconds). */
741 long __gnat_invalid_tzoff = 259273;
743 /* Definition of __gnat_localtime_r used by a-calend.adb */
745 #if defined (__MINGW32__)
749 /* For the Cert run times on native Windows we use dummy functions
750 for locking and unlocking tasks since we do not support multiple
751 threads on this configuration (Cert run time on native Windows). */
755 void (*Lock_Task) () = &dummy;
756 void (*Unlock_Task) () = &dummy;
760 #define Lock_Task system__soft_links__lock_task
761 extern void (*Lock_Task) (void);
763 #define Unlock_Task system__soft_links__unlock_task
764 extern void (*Unlock_Task) (void);
768 /* Reentrant localtime for Windows. */
771 __gnat_localtime_tzoff (const time_t *, long *);
773 static const unsigned long long w32_epoch_offset = 11644473600ULL;
775 __gnat_localtime_tzoff (const time_t *timer, long *off)
780 unsigned long long ull_time;
781 } utc_time, local_time;
783 SYSTEMTIME utc_sys_time, local_sys_time;
784 TIME_ZONE_INFORMATION tzi;
793 tzi_status = GetTimeZoneInformation (&tzi);
795 if (tzi_status == TIME_ZONE_ID_STANDARD)
796 /* The system is operating in the range covered by the StandardDate
798 *off = *off + tzi.StandardBias;
799 else if (tzi_status == TIME_ZONE_ID_DAYLIGHT)
800 /* The system is operating in the range covered by the DaylightDate
802 *off = *off + tzi.DaylightBias;
807 /* First convert unix time_t structure to windows FILETIME format. */
808 utc_time.ull_time = ((unsigned long long) *timer + w32_epoch_offset)
811 tzi_status = GetTimeZoneInformation (&tzi);
813 /* If GetTimeZoneInformation does not return a value between 0 and 2 then
814 it means that we were not able to retrieve timezone informations.
815 Note that we cannot use here FileTimeToLocalFileTime as Windows will use
816 in always in this case the current timezone setting. As suggested on
817 MSDN we use the following three system calls to get the right information.
818 Note also that starting with Windows Vista new functions are provided to
819 get timezone settings that depend on the year. We cannot use them as we
820 still support Windows XP and Windows 2003. */
821 status = (tzi_status >= 0 && tzi_status <= 2)
822 && FileTimeToSystemTime (&utc_time.ft_time, &utc_sys_time)
823 && SystemTimeToTzSpecificLocalTime (&tzi, &utc_sys_time, &local_sys_time)
824 && SystemTimeToFileTime (&local_sys_time, &local_time.ft_time);
827 /* An error occurs so return invalid_tzoff. */
828 *off = __gnat_invalid_tzoff;
830 if (local_time.ull_time > utc_time.ull_time)
831 *off = (long) ((local_time.ull_time - utc_time.ull_time) / 10000000ULL);
833 *off = - (long) ((utc_time.ull_time - local_time.ull_time) / 10000000ULL);
842 /* On Lynx, all time values are treated in GMT */
844 #if defined (__Lynx__)
846 /* As of LynxOS 3.1.0a patch level 040, LynuxWorks changes the
847 prototype to the C library function localtime_r from the POSIX.4
848 Draft 9 to the POSIX 1.c version. Before this change the following
849 spec is required. Only use when ___THREADS_POSIX4ad4__ is defined,
850 the Lynx convention when building against the legacy API. */
853 __gnat_localtime_tzoff (const time_t *, long *);
856 __gnat_localtime_tzoff (const time_t *timer, long *off)
863 /* VMS does not need __gnat_localtime_tzoff */
867 /* Other targets except Lynx, VMS and Windows provide a standard localtime_r */
871 #define Lock_Task system__soft_links__lock_task
872 extern void (*Lock_Task) (void);
874 #define Unlock_Task system__soft_links__unlock_task
875 extern void (*Unlock_Task) (void);
878 __gnat_localtime_tzoff (const time_t *, long *);
881 __gnat_localtime_tzoff (const time_t *timer, long *off)
885 /* AIX, HPUX, SGI Irix, Sun Solaris */
886 #if defined (_AIX) || defined (__hpux__) || defined (sgi) || defined (sun)
890 localtime_r (timer, &tp);
891 *off = (long) -timezone;
895 /* Correct the offset if Daylight Saving Time is in effect */
902 #elif defined (__vxworks)
907 localtime_r (timer, &tp);
909 /* Try to read the environment variable TIMEZONE. The variable may not have
910 been initialize, in that case return an offset of zero (0) for UTC. */
912 char *tz_str = getenv ("TIMEZONE");
914 if ((tz_str == NULL) || (*tz_str == '\0'))
918 char *tz_start, *tz_end;
920 /* The format of the data contained in TIMEZONE is N::U:S:E where N is the
921 name of the time zone, U are the minutes difference from UTC, S is the
922 start of DST in mmddhh and E is the end of DST in mmddhh. Extracting
923 the value of U involves setting two pointers, one at the beginning and
924 one at the end of the value. The end pointer is then set to null in
925 order to delimit a string slice for atol to process. */
927 tz_start = index (tz_str, ':') + 2;
928 tz_end = index (tz_start, ':');
931 /* The Ada layer expects an offset in seconds. Note that we must reverse
932 the sign of the result since west is positive and east is negative on
935 *off = -atol (tz_start) * 60;
937 /* Correct the offset if Daylight Saving Time is in effect */
946 /* Darwin, Free BSD, Linux, Tru64, where component tm_gmtoff is present in
949 #elif defined (__APPLE__) || defined (__FreeBSD__) || defined (linux) ||\
950 (defined (__alpha__) && defined (__osf__)) || defined (__GLIBC__)
952 localtime_r (timer, &tp);
956 /* Default: treat all time values in GMT */
972 /* __gnat_get_task_options is used by s-taprop.adb only for VxWorks. This
973 function returns the options to be set when creating a new task. It fetches
974 the options assigned to the current task (parent), so offering some user
975 level control over the options for a task hierarchy. It forces VX_FP_TASK
976 because it is almost always required. On processors with the SPE
977 category, VX_SPE_TASK is needed to enable the SPE. */
978 extern int __gnat_get_task_options (void);
981 __gnat_get_task_options (void)
985 /* Get the options for the task creator */
986 taskOptionsGet (taskIdSelf (), &options);
988 /* Force VX_FP_TASK because it is almost always required */
989 options |= VX_FP_TASK;
990 #if defined (__SPE__) && (! defined (__VXWORKSMILS__))
991 options |= VX_SPE_TASK;
994 /* Mask those bits that are not under user control */
995 #ifdef VX_USR_TASK_OPTIONS
996 return options & VX_USR_TASK_OPTIONS;
1005 __gnat_is_file_not_found_error (int errno_val) {
1006 switch (errno_val) {
1009 /* In the case of VxWorks, we also have to take into account various
1010 * filesystem-specific variants of this error.
1012 #if ! defined (VTHREADS)
1013 case S_dosFsLib_FILE_NOT_FOUND:
1015 #if ! defined (__RTP__) && (! defined (VTHREADS) || defined (__VXWORKSMILS__))
1016 case S_nfsLib_NFSERR_NOENT: