1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 1992-2012, 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 */
85 (1) Opening a file with read mode fails if the file does not exist or
88 (2) Opening a file with append mode causes all subsequent writes to the
89 file to be forced to the then current end-of-file, regardless of
90 intervening calls to the fseek function.
92 (3) When a file is opened with update mode, both input and output may be
93 performed on the associated stream. However, output may not be directly
94 followed by input without an intervening call to the fflush function or
95 to a file positioning function (fseek, fsetpos, or rewind), and input
96 may not be directly followed by output without an intervening call to a
97 file positioning function, unless the input operation encounters
100 The other target dependent declarations here are for the two functions
101 __gnat_set_binary_mode and __gnat_set_text_mode:
103 void __gnat_set_binary_mode (int handle);
104 void __gnat_set_text_mode (int handle);
106 These functions have no effect in Unix (or similar systems where there is
107 no distinction between binary and text files), but in DOS (and similar
108 systems where text mode does CR/LF translation), these functions allow
109 the mode of the stream with the given handle (fileno can be used to get
110 the handle of a stream) to be changed dynamically. The returned result
111 is 0 if no error occurs and -1 if an error occurs.
113 Finally there is a boolean (character) variable
115 char __gnat_text_translation_required;
117 which is zero (false) in Unix mode, and one (true) in DOS mode, with a
118 true value indicating that text translation is required on text files
119 and that fopen supports the trailing t and b modifiers.
123 #if defined (WINNT) || defined (__CYGWIN__)
125 const char __gnat_text_translation_required
= 1;
128 #define WIN_SETMODE setmode
131 #define WIN_SETMODE _setmode
135 __gnat_set_binary_mode (int handle
)
137 WIN_SETMODE (handle
, O_BINARY
);
141 __gnat_set_text_mode (int handle
)
143 WIN_SETMODE (handle
, O_TEXT
);
149 __gnat_ttyname (int filedes
)
151 extern char *ttyname (int);
153 return ttyname (filedes
);
156 #endif /* __CYGWIN__ */
158 #if defined (__CYGWIN__) || defined (__MINGW32__)
163 int __gnat_is_windows_xp (void);
166 __gnat_is_windows_xp (void)
168 static int is_win_xp
=0, is_win_xp_checked
=0;
170 if (!is_win_xp_checked
)
172 OSVERSIONINFO version
;
174 is_win_xp_checked
= 1;
176 memset (&version
, 0, sizeof (version
));
177 version
.dwOSVersionInfoSize
= sizeof (version
);
179 is_win_xp
= GetVersionEx (&version
)
180 && version
.dwPlatformId
== VER_PLATFORM_WIN32_NT
181 && (version
.dwMajorVersion
> 5
182 || (version
.dwMajorVersion
== 5 && version
.dwMinorVersion
>= 1));
189 /* Get the bounds of the stack. The stack pointer is supposed to be
190 initialized to BASE when a thread is created and the stack can be extended
191 to LIMIT before reaching a guard page.
192 Note: for the main thread, the system automatically extend the stack, so
193 LIMIT is only the current limit. */
196 __gnat_get_stack_bounds (void **base
, void **limit
)
200 /* We know that the first field of the TEB is the TIB. */
201 tib
= (NT_TIB
*)NtCurrentTeb ();
203 *base
= tib
->StackBase
;
204 *limit
= tib
->StackLimit
;
207 #endif /* __CYGWIN__ || __MINGW32__ */
211 /* Return the name of the tty. Under windows there is no name for
212 the tty, so this function, if connected to a tty, returns the generic name
216 __gnat_ttyname (int filedes
)
218 if (isatty (filedes
))
224 #endif /* __MINGW32__ */
228 const char __gnat_text_translation_required
= 0;
230 /* These functions do nothing in non-DOS systems. */
233 __gnat_set_binary_mode (int handle ATTRIBUTE_UNUSED
)
238 __gnat_set_text_mode (int handle ATTRIBUTE_UNUSED
)
242 __gnat_ttyname (int filedes
)
244 #if defined (__vxworks) || defined (__nucleus)
247 extern char *ttyname (int);
249 return ttyname (filedes
);
250 #endif /* defined (__vxworks) || defined (__nucleus) */
254 #if defined (linux) || defined (sun) \
256 || defined (__MACHTEN__) || defined (__hpux__) || defined (_AIX) \
257 || (defined (__svr4__) && defined (i386)) || defined (__Lynx__) \
258 || defined (__CYGWIN__) || defined (__FreeBSD__) || defined (__OpenBSD__) \
259 || defined (__GLIBC__) || defined (__APPLE__)
263 # include <termios.h>
265 # include <conio.h> /* for getch(), kbhit() */
268 # include <termios.h>
273 extern char *decc$ga_stdscr
;
274 static int initted
= 0;
278 /* Implements the common processing for getc_immediate and
279 getc_immediate_nowait. */
281 extern void getc_immediate (FILE *, int *, int *);
282 extern void getc_immediate_nowait (FILE *, int *, int *, int *);
283 extern void getc_immediate_common (FILE *, int *, int *, int *, int);
285 /* Called by Get_Immediate (Foo); */
288 getc_immediate (FILE *stream
, int *ch
, int *end_of_file
)
292 getc_immediate_common (stream
, ch
, end_of_file
, &avail
, 1);
295 /* Called by Get_Immediate (Foo, Available); */
298 getc_immediate_nowait (FILE *stream
, int *ch
, int *end_of_file
, int *avail
)
300 getc_immediate_common (stream
, ch
, end_of_file
, avail
, 0);
303 /* Called by getc_immediate () and getc_immediate_nowait () */
306 getc_immediate_common (FILE *stream
,
312 #if defined (linux) || defined (sun) \
313 || defined (__CYGWIN32__) || defined (__MACHTEN__) || defined (__hpux__) \
314 || defined (_AIX) || (defined (__svr4__) && defined (i386)) \
315 || defined (__Lynx__) || defined (__FreeBSD__) || defined (__OpenBSD__) \
316 || defined (__GLIBC__) || defined (__APPLE__)
320 int eof_ch
= 4; /* Ctrl-D */
321 int fd
= fileno (stream
);
322 struct termios otermios_rec
, termios_rec
;
326 tcgetattr (fd
, &termios_rec
);
327 memcpy (&otermios_rec
, &termios_rec
, sizeof (struct termios
));
329 /* Set RAW mode, with no echo */
330 termios_rec
.c_lflag
= termios_rec
.c_lflag
& ~ICANON
& ~ECHO
;
332 #if defined(linux) || defined (sun) \
333 || defined (__MACHTEN__) || defined (__hpux__) \
334 || defined (_AIX) || (defined (__svr4__) && defined (i386)) \
335 || defined (__Lynx__) || defined (__FreeBSD__) || defined (__OpenBSD__) \
336 || defined (__GLIBC__) || defined (__APPLE__)
337 eof_ch
= termios_rec
.c_cc
[VEOF
];
339 /* If waiting (i.e. Get_Immediate (Char)), set MIN = 1 and wait for
340 a character forever. This doesn't seem to effect Ctrl-Z or
342 If not waiting (i.e. Get_Immediate (Char, Available)),
343 don't wait for anything but timeout immediately. */
344 termios_rec
.c_cc
[VMIN
] = waiting
;
345 termios_rec
.c_cc
[VTIME
] = 0;
347 tcsetattr (fd
, TCSANOW
, &termios_rec
);
351 /* Read is used here instead of fread, because fread doesn't
352 work on Solaris5 and Sunos4 in this situation. Maybe because we
353 are mixing calls that use file descriptors and streams. */
354 nread
= read (fd
, &c
, 1);
357 /* On Unix terminals, Ctrl-D (EOT) is an End of File. */
365 /* Everything else is ok */
366 else if (c
!= eof_ch
)
384 tcsetattr (fd
, TCSANOW
, &otermios_rec
);
390 int fd
= fileno (stream
);
401 *ch
= decc$
bsd_wgetch (decc$ga_stdscr
);
409 decc$
bsd_nocbreak ();
412 #elif defined (__MINGW32__)
413 int fd
= fileno (stream
);
415 int eot_ch
= 4; /* Ctrl-D */
432 char_waiting
= kbhit();
434 if (char_waiting
== 1)
452 #elif defined (__vxworks)
453 /* Bit masks of file descriptors to read from. */
454 struct fd_set readFds
;
455 /* Timeout before select returns if nothing can be read. */
456 struct timeval timeOut
;
458 int fd
= fileno (stream
);
467 /* If we do not want to wait, we have to set up fd in RAW mode. This
468 should be done outside this function as setting fd in RAW mode under
469 vxWorks flushes the buffer of fd. If the RAW mode was set here, the
470 buffer would be empty and we would always return that no character
474 /* Initialization of timeOut for its use with select. */
478 /* Initialization of readFds for its use with select;
479 FD is the only file descriptor to be monitored */
481 FD_SET (fd
, &readFds
);
484 /* We do all this processing to emulate a non blocking read. */
485 readable
= select (width
, &readFds
, NULL
, NULL
, &timeOut
);
486 if (readable
== ERROR
)
487 *avail
= -1, *end_of_file
= -1;
488 /* No character available in input. */
489 else if (readable
== 0)
490 *avail
= 0, *end_of_file
= 0;
493 nread
= read (fd
, &c
, 1);
495 *avail
= 1, *end_of_file
= 0;
498 *avail
= 0, *end_of_file
= 1;
501 *avail
= -1, *end_of_file
= -1;
505 /* We have to wait until we get a character */
511 /* Save the current mode of FD. */
512 option
= ioctl (fd
, FIOGETOPTIONS
, 0);
514 /* Set FD in RAW mode. */
515 status
= ioctl (fd
, FIOSETOPTIONS
, OPT_RAW
);
518 nread
= read (fd
, &c
, 1);
520 *avail
= 1, *end_of_file
= 0;
523 *avail
= 0, *end_of_file
= 1;
524 /* Else there is an ERROR. */
527 /* Revert FD to its previous mode. */
528 status
= ioctl (fd
, FIOSETOPTIONS
, option
);
536 /* If we're not on a terminal, then we don't need any fancy processing.
537 Also this is the only thing that's left if we're not on one of the
538 supported systems; which means that for non supported systems,
539 get_immediate may wait for a carriage return on terminals. */
540 *ch
= fgetc (stream
);
554 /* The following definitions are provided in NT to support Windows based
560 /* Provide functions to echo the values passed to WinMain (windows bindings
561 will want to import these). We use the same names as the routines used
562 by AdaMagic for compatibility. */
564 char *rts_get_hInstance (void);
565 char *rts_get_hPrevInstance (void);
566 char *rts_get_lpCommandLine (void);
567 int rts_get_nShowCmd (void);
570 rts_get_hInstance (void)
572 return (char *)GetModuleHandleA (0);
576 rts_get_hPrevInstance (void)
582 rts_get_lpCommandLine (void)
584 return GetCommandLineA ();
588 rts_get_nShowCmd (void)
596 /* This gets around a problem with using the old threads library on VMS 7.0. */
598 extern long get_gmtoff (void);
606 t
= time ((time_t) 0);
608 return ts
->tm_gmtoff
;
612 /* This value is returned as the time zone offset when a valid value
613 cannot be determined. It is simply a bizarre value that will never
614 occur. It is 3 days plus 73 seconds (offset is in seconds). */
616 long __gnat_invalid_tzoff
= 259273;
618 /* Definition of __gnat_localtime_r used by a-calend.adb */
620 #if defined (__MINGW32__)
624 /* For the Cert run times on native Windows we use dummy functions
625 for locking and unlocking tasks since we do not support multiple
626 threads on this configuration (Cert run time on native Windows). */
630 void (*Lock_Task
) () = &dummy
;
631 void (*Unlock_Task
) () = &dummy
;
635 #define Lock_Task system__soft_links__lock_task
636 extern void (*Lock_Task
) (void);
638 #define Unlock_Task system__soft_links__unlock_task
639 extern void (*Unlock_Task
) (void);
643 /* Reentrant localtime for Windows. */
646 __gnat_localtime_tzoff (const time_t *, const int *, long *);
648 static const unsigned long long w32_epoch_offset
= 11644473600ULL;
650 __gnat_localtime_tzoff (const time_t *timer
, const int *is_historic
, long *off
)
652 TIME_ZONE_INFORMATION tzi
;
665 tzi_status
= GetTimeZoneInformation (&tzi
);
667 /* Processing for RTX targets or cases where we simply want to extract the
668 offset of the current time zone, regardless of the date. A value of "0"
669 for flag "is_historic" signifies that the date is NOT historic, see the
670 body of Ada.Calendar.UTC_Time_Offset. */
672 if (rtx_active
|| *is_historic
== 0) {
675 /* The system is operating in the range covered by the StandardDate
677 if (tzi_status
== TIME_ZONE_ID_STANDARD
) {
678 *off
= *off
+ tzi
.StandardBias
;
681 /* The system is operating in the range covered by the DaylightDate
683 else if (tzi_status
== TIME_ZONE_ID_DAYLIGHT
) {
684 *off
= *off
+ tzi
.DaylightBias
;
690 /* Time zone offset calculations for a historic or future date */
696 unsigned long long ull_time
;
697 } utc_time
, local_time
;
699 SYSTEMTIME utc_sys_time
, local_sys_time
;
702 /* First convert unix time_t structure to windows FILETIME format. */
703 utc_time
.ull_time
= ((unsigned long long) *timer
+ w32_epoch_offset
)
706 /* If GetTimeZoneInformation does not return a value between 0 and 2 then
707 it means that we were not able to retrieve timezone informations. Note
708 that we cannot use here FileTimeToLocalFileTime as Windows will use in
709 always in this case the current timezone setting. As suggested on MSDN
710 we use the following three system calls to get the right information.
711 Note also that starting with Windows Vista new functions are provided
712 to get timezone settings that depend on the year. We cannot use them as
713 we still support Windows XP and Windows 2003. */
715 status
= (tzi_status
>= 0 && tzi_status
<= 2)
716 && FileTimeToSystemTime (&utc_time
.ft_time
, &utc_sys_time
)
717 && SystemTimeToTzSpecificLocalTime (&tzi
, &utc_sys_time
, &local_sys_time
)
718 && SystemTimeToFileTime (&local_sys_time
, &local_time
.ft_time
);
720 /* An error has occured, return invalid_tzoff */
723 *off
= __gnat_invalid_tzoff
;
726 if (local_time
.ull_time
> utc_time
.ull_time
) {
727 *off
= (long) ((local_time
.ull_time
- utc_time
.ull_time
)
731 *off
= - (long) ((utc_time
.ull_time
- local_time
.ull_time
)
742 /* On Lynx, all time values are treated in GMT */
744 #if defined (__Lynx__)
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. */
753 __gnat_localtime_tzoff (const time_t *, const int *, long *);
756 __gnat_localtime_tzoff (const time_t *timer
, const int *is_historic
, long *off
)
763 /* VMS does not need __gnat_localtime_tzoff */
767 /* Other targets except Lynx, VMS and Windows provide a standard localtime_r */
771 #define Lock_Task system__soft_links__lock_task
772 extern void (*Lock_Task
) (void);
774 #define Unlock_Task system__soft_links__unlock_task
775 extern void (*Unlock_Task
) (void);
778 __gnat_localtime_tzoff (const time_t *, const int *, long *);
781 __gnat_localtime_tzoff (const time_t *timer
, const int *is_historic
, long *off
)
785 /* AIX, HPUX, Sun Solaris */
786 #if defined (_AIX) || defined (__hpux__) || defined (sun)
790 localtime_r (timer
, &tp
);
791 *off
= (long) -timezone
;
795 /* Correct the offset if Daylight Saving Time is in effect */
802 #elif defined (__vxworks)
807 localtime_r (timer
, &tp
);
809 /* Try to read the environment variable TIMEZONE. The variable may not have
810 been initialize, in that case return an offset of zero (0) for UTC. */
812 char *tz_str
= getenv ("TIMEZONE");
814 if ((tz_str
== NULL
) || (*tz_str
== '\0'))
818 char *tz_start
, *tz_end
;
820 /* The format of the data contained in TIMEZONE is N::U:S:E where N is the
821 name of the time zone, U are the minutes difference from UTC, S is the
822 start of DST in mmddhh and E is the end of DST in mmddhh. Extracting
823 the value of U involves setting two pointers, one at the beginning and
824 one at the end of the value. The end pointer is then set to null in
825 order to delimit a string slice for atol to process. */
827 tz_start
= index (tz_str
, ':') + 2;
828 tz_end
= index (tz_start
, ':');
831 /* The Ada layer expects an offset in seconds. Note that we must reverse
832 the sign of the result since west is positive and east is negative on
835 *off
= -atol (tz_start
) * 60;
837 /* Correct the offset if Daylight Saving Time is in effect */
846 /* Darwin, Free BSD, Linux, where component tm_gmtoff is present in
849 #elif defined (__APPLE__) || defined (__FreeBSD__) || defined (linux) ||\
852 localtime_r (timer
, &tp
);
856 /* Default: treat all time values in GMT */
872 /* __gnat_get_task_options is used by s-taprop.adb only for VxWorks. This
873 function returns the options to be set when creating a new task. It fetches
874 the options assigned to the current task (parent), so offering some user
875 level control over the options for a task hierarchy. It forces VX_FP_TASK
876 because it is almost always required. On processors with the SPE
877 category, VX_SPE_TASK should be used instead to enable the SPE. */
878 extern int __gnat_get_task_options (void);
881 __gnat_get_task_options (void)
885 /* Get the options for the task creator */
886 taskOptionsGet (taskIdSelf (), &options
);
888 /* Force VX_FP_TASK or VX_SPE_TASK as needed */
889 #if defined (__SPE__)
890 options
|= VX_SPE_TASK
;
892 options
|= VX_FP_TASK
;
895 /* Mask those bits that are not under user control */
896 #ifdef VX_USR_TASK_OPTIONS
897 return options
& VX_USR_TASK_OPTIONS
;
906 __gnat_is_file_not_found_error (int errno_val
) {
910 /* In the case of VxWorks, we also have to take into account various
911 * filesystem-specific variants of this error.
913 #if ! defined (VTHREADS)
914 case S_dosFsLib_FILE_NOT_FOUND
:
916 #if ! defined (__RTP__) && (! defined (VTHREADS) || defined (__VXWORKSMILS__))
917 case S_nfsLib_NFSERR_NOENT
: