1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 1992-2015, 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"
47 # include "vwModNum.h"
69 #if defined (__sun__) && !defined (__vxworks)
70 /* The declaration is present in <time.h> but conditionalized
71 on a couple of macros we don't define. */
72 extern struct tm
*localtime_r(const time_t *, struct tm
*);
77 /* Don't use macros versions of this functions on VxWorks since they cause
78 imcompatible changes in some VxWorks versions */
90 (1) Opening a file with read mode fails if the file does not exist or
93 (2) Opening a file with append mode causes all subsequent writes to the
94 file to be forced to the then current end-of-file, regardless of
95 intervening calls to the fseek function.
97 (3) When a file is opened with update mode, both input and output may be
98 performed on the associated stream. However, output may not be directly
99 followed by input without an intervening call to the fflush function or
100 to a file positioning function (fseek, fsetpos, or rewind), and input
101 may not be directly followed by output without an intervening call to a
102 file positioning function, unless the input operation encounters
105 The other target dependent declarations here are for the three functions
106 __gnat_set_binary_mode, __gnat_set_text_mode and __gnat_set_mode:
108 void __gnat_set_binary_mode (int handle);
109 void __gnat_set_text_mode (int handle);
110 void __gnat_set_mode (int handle, int mode);
112 These functions have no effect in Unix (or similar systems where there is
113 no distinction between binary and text files), but in DOS (and similar
114 systems where text mode does CR/LF translation), these functions allow
115 the mode of the stream with the given handle (fileno can be used to get
116 the handle of a stream) to be changed dynamically. The returned result
117 is 0 if no error occurs and -1 if an error occurs.
119 Finally there is a boolean (character) variable
121 char __gnat_text_translation_required;
123 which is zero (false) in Unix mode, and one (true) in DOS mode, with a
124 true value indicating that text translation is required on text files
125 and that fopen supports the trailing t and b modifiers.
129 #if defined (WINNT) || defined (__CYGWIN__)
131 const char __gnat_text_translation_required
= 1;
134 #define WIN_SETMODE setmode
137 #define WIN_SETMODE _setmode
141 __gnat_set_binary_mode (int handle
)
143 WIN_SETMODE (handle
, O_BINARY
);
147 __gnat_set_text_mode (int handle
)
149 WIN_SETMODE (handle
, O_TEXT
);
153 __gnat_set_mode (int handle
, int mode
)
155 /* the values here must be synchronized with
156 System.File_Control_Block.Content_Encodding:
166 case 0 : WIN_SETMODE (handle
, _O_BINARY
); break;
167 case 1 : WIN_SETMODE (handle
, CurrentCCSEncoding
); break;
168 case 2 : WIN_SETMODE (handle
, _O_TEXT
); break;
169 case 3 : WIN_SETMODE (handle
, _O_U8TEXT
); break;
170 case 4 : WIN_SETMODE (handle
, _O_WTEXT
); break;
171 case 5 : WIN_SETMODE (handle
, _O_U16TEXT
); break;
178 __gnat_ttyname (int filedes
)
180 extern char *ttyname (int);
182 return ttyname (filedes
);
185 #endif /* __CYGWIN__ */
187 #if defined (__CYGWIN__) || defined (__MINGW32__)
190 int __gnat_is_windows_xp (void);
193 __gnat_is_windows_xp (void)
195 static int is_win_xp
=0, is_win_xp_checked
=0;
197 if (!is_win_xp_checked
)
199 OSVERSIONINFO version
;
201 is_win_xp_checked
= 1;
203 memset (&version
, 0, sizeof (version
));
204 version
.dwOSVersionInfoSize
= sizeof (version
);
206 is_win_xp
= GetVersionEx (&version
)
207 && version
.dwPlatformId
== VER_PLATFORM_WIN32_NT
208 && (version
.dwMajorVersion
> 5
209 || (version
.dwMajorVersion
== 5 && version
.dwMinorVersion
>= 1));
214 /* Get the bounds of the stack. The stack pointer is supposed to be
215 initialized to BASE when a thread is created and the stack can be extended
216 to LIMIT before reaching a guard page.
217 Note: for the main thread, the system automatically extend the stack, so
218 LIMIT is only the current limit. */
221 __gnat_get_stack_bounds (void **base
, void **limit
)
225 /* We know that the first field of the TEB is the TIB. */
226 tib
= (NT_TIB
*)NtCurrentTeb ();
228 *base
= tib
->StackBase
;
229 *limit
= tib
->StackLimit
;
232 #endif /* __CYGWIN__ || __MINGW32__ */
236 /* Return the name of the tty. Under windows there is no name for
237 the tty, so this function, if connected to a tty, returns the generic name
241 __gnat_ttyname (int filedes
)
243 if (isatty (filedes
))
249 #endif /* __MINGW32__ */
253 const char __gnat_text_translation_required
= 0;
255 /* These functions do nothing in non-DOS systems. */
258 __gnat_set_binary_mode (int handle ATTRIBUTE_UNUSED
)
263 __gnat_set_text_mode (int handle ATTRIBUTE_UNUSED
)
268 __gnat_set_mode (int handle ATTRIBUTE_UNUSED
, int mode ATTRIBUTE_UNUSED
)
273 __gnat_ttyname (int filedes
)
275 #if defined (__vxworks)
278 extern char *ttyname (int);
280 return ttyname (filedes
);
281 #endif /* defined (__vxworks) */
285 #if defined (__linux__) || defined (__sun__) \
287 || defined (__MACHTEN__) || defined (__hpux__) || defined (_AIX) \
288 || (defined (__svr4__) && defined (__i386__)) || defined (__Lynx__) \
289 || defined (__CYGWIN__) || defined (__FreeBSD__) || defined (__OpenBSD__) \
290 || defined (__GLIBC__) || defined (__APPLE__)
294 # include <termios.h>
296 # include <conio.h> /* for getch(), kbhit() */
299 # include <termios.h>
304 /* Implements the common processing for getc_immediate and
305 getc_immediate_nowait. */
307 extern void getc_immediate (FILE *, int *, int *);
308 extern void getc_immediate_nowait (FILE *, int *, int *, int *);
309 extern void getc_immediate_common (FILE *, int *, int *, int *, int);
311 /* Called by Get_Immediate (Foo); */
314 getc_immediate (FILE *stream
, int *ch
, int *end_of_file
)
318 getc_immediate_common (stream
, ch
, end_of_file
, &avail
, 1);
321 /* Called by Get_Immediate (Foo, Available); */
324 getc_immediate_nowait (FILE *stream
, int *ch
, int *end_of_file
, int *avail
)
326 getc_immediate_common (stream
, ch
, end_of_file
, avail
, 0);
329 /* Called by getc_immediate () and getc_immediate_nowait () */
332 getc_immediate_common (FILE *stream
,
336 int waiting ATTRIBUTE_UNUSED
)
338 #if defined (__linux__) || defined (__sun__) \
339 || defined (__CYGWIN32__) || defined (__MACHTEN__) || defined (__hpux__) \
340 || defined (_AIX) || (defined (__svr4__) && defined (__i386__)) \
341 || defined (__Lynx__) || defined (__FreeBSD__) || defined (__OpenBSD__) \
342 || defined (__GLIBC__) || defined (__APPLE__)
346 int eof_ch
= 4; /* Ctrl-D */
347 int fd
= fileno (stream
);
348 struct termios otermios_rec
, termios_rec
;
352 tcgetattr (fd
, &termios_rec
);
353 memcpy (&otermios_rec
, &termios_rec
, sizeof (struct termios
));
355 /* Set RAW mode, with no echo */
356 termios_rec
.c_lflag
= termios_rec
.c_lflag
& ~ICANON
& ~ECHO
;
358 #if defined (__linux__) || defined (__sun__) \
359 || defined (__MACHTEN__) || defined (__hpux__) \
360 || defined (_AIX) || (defined (__svr4__) && defined (__i386__)) \
361 || defined (__Lynx__) || defined (__FreeBSD__) || defined (__OpenBSD__) \
362 || defined (__GLIBC__) || defined (__APPLE__)
363 eof_ch
= termios_rec
.c_cc
[VEOF
];
365 /* If waiting (i.e. Get_Immediate (Char)), set MIN = 1 and wait for
366 a character forever. This doesn't seem to effect Ctrl-Z or
368 If not waiting (i.e. Get_Immediate (Char, Available)),
369 don't wait for anything but timeout immediately. */
370 termios_rec
.c_cc
[VMIN
] = waiting
;
371 termios_rec
.c_cc
[VTIME
] = 0;
373 tcsetattr (fd
, TCSANOW
, &termios_rec
);
377 /* Read is used here instead of fread, because fread doesn't
378 work on Solaris5 and Sunos4 in this situation. Maybe because we
379 are mixing calls that use file descriptors and streams. */
380 nread
= read (fd
, &c
, 1);
383 /* On Unix terminals, Ctrl-D (EOT) is an End of File. */
391 /* Everything else is ok */
392 else if (c
!= eof_ch
)
410 tcsetattr (fd
, TCSANOW
, &otermios_rec
);
415 #elif defined (__MINGW32__)
416 int fd
= fileno (stream
);
418 int eot_ch
= 4; /* Ctrl-D */
435 char_waiting
= kbhit();
437 if (char_waiting
== 1)
455 #elif defined (__vxworks)
456 /* Bit masks of file descriptors to read from. */
457 struct fd_set readFds
;
458 /* Timeout before select returns if nothing can be read. */
459 struct timeval timeOut
;
461 int fd
= fileno (stream
);
470 /* If we do not want to wait, we have to set up fd in RAW mode. This
471 should be done outside this function as setting fd in RAW mode under
472 vxWorks flushes the buffer of fd. If the RAW mode was set here, the
473 buffer would be empty and we would always return that no character
477 /* Initialization of timeOut for its use with select. */
481 /* Initialization of readFds for its use with select;
482 FD is the only file descriptor to be monitored */
484 FD_SET (fd
, &readFds
);
487 /* We do all this processing to emulate a non blocking read. */
488 readable
= select (width
, &readFds
, NULL
, NULL
, &timeOut
);
489 if (readable
== ERROR
)
490 *avail
= -1, *end_of_file
= -1;
491 /* No character available in input. */
492 else if (readable
== 0)
493 *avail
= 0, *end_of_file
= 0;
496 nread
= read (fd
, &c
, 1);
498 *avail
= 1, *end_of_file
= 0;
501 *avail
= 0, *end_of_file
= 1;
504 *avail
= -1, *end_of_file
= -1;
508 /* We have to wait until we get a character */
514 /* Save the current mode of FD. */
515 option
= ioctl (fd
, FIOGETOPTIONS
, 0);
517 /* Set FD in RAW mode. */
518 status
= ioctl (fd
, FIOSETOPTIONS
, OPT_RAW
);
521 nread
= read (fd
, &c
, 1);
523 *avail
= 1, *end_of_file
= 0;
526 *avail
= 0, *end_of_file
= 1;
527 /* Else there is an ERROR. */
530 /* Revert FD to its previous mode. */
531 status
= ioctl (fd
, FIOSETOPTIONS
, option
);
539 /* If we're not on a terminal, then we don't need any fancy processing.
540 Also this is the only thing that's left if we're not on one of the
541 supported systems; which means that for non supported systems,
542 get_immediate may wait for a carriage return on terminals. */
543 *ch
= fgetc (stream
);
557 /* The following definitions are provided in NT to support Windows based
563 /* Provide functions to echo the values passed to WinMain (windows bindings
564 will want to import these). We use the same names as the routines used
565 by AdaMagic for compatibility. */
567 char *rts_get_hInstance (void);
568 char *rts_get_hPrevInstance (void);
569 char *rts_get_lpCommandLine (void);
570 int rts_get_nShowCmd (void);
573 rts_get_hInstance (void)
575 return (char *)GetModuleHandleA (0);
579 rts_get_hPrevInstance (void)
585 rts_get_lpCommandLine (void)
587 return GetCommandLineA ();
591 rts_get_nShowCmd (void)
598 /* This value is returned as the time zone offset when a valid value
599 cannot be determined. It is simply a bizarre value that will never
600 occur. It is 3 days plus 73 seconds (offset is in seconds). */
602 long __gnat_invalid_tzoff
= 259273;
604 /* Definition of __gnat_localtime_r used by a-calend.adb */
606 #if defined (__MINGW32__)
610 /* For the Cert run times on native Windows we use dummy functions
611 for locking and unlocking tasks since we do not support multiple
612 threads on this configuration (Cert run time on native Windows). */
616 void (*Lock_Task
) () = &dummy
;
617 void (*Unlock_Task
) () = &dummy
;
621 #define Lock_Task system__soft_links__lock_task
622 extern void (*Lock_Task
) (void);
624 #define Unlock_Task system__soft_links__unlock_task
625 extern void (*Unlock_Task
) (void);
629 /* Reentrant localtime for Windows. */
632 __gnat_localtime_tzoff (const time_t *, const int *, long *);
634 static const unsigned long long w32_epoch_offset
= 11644473600ULL;
636 __gnat_localtime_tzoff (const time_t *timer
, const int *is_historic
, long *off
)
638 TIME_ZONE_INFORMATION tzi
;
644 tzi_status
= GetTimeZoneInformation (&tzi
);
646 /* Cases where we simply want to extract the offset of the current time
647 zone, regardless of the date. A value of "0" for flag "is_historic"
648 signifies that the date is NOT historic, see the
649 body of Ada.Calendar.UTC_Time_Offset. */
651 if (*is_historic
== 0) {
654 /* The system is operating in the range covered by the StandardDate
656 if (tzi_status
== TIME_ZONE_ID_STANDARD
) {
657 *off
= *off
+ tzi
.StandardBias
;
660 /* The system is operating in the range covered by the DaylightDate
662 else if (tzi_status
== TIME_ZONE_ID_DAYLIGHT
) {
663 *off
= *off
+ tzi
.DaylightBias
;
669 /* Time zone offset calculations for a historic or future date */
675 unsigned long long ull_time
;
676 } utc_time
, local_time
;
678 SYSTEMTIME utc_sys_time
, local_sys_time
;
681 /* First convert unix time_t structure to windows FILETIME format. */
682 utc_time
.ull_time
= ((unsigned long long) *timer
+ w32_epoch_offset
)
685 /* If GetTimeZoneInformation does not return a value between 0 and 2 then
686 it means that we were not able to retrieve timezone information. Note
687 that we cannot use here FileTimeToLocalFileTime as Windows will use in
688 always in this case the current timezone setting. As suggested on MSDN
689 we use the following three system calls to get the right information.
690 Note also that starting with Windows Vista new functions are provided
691 to get timezone settings that depend on the year. We cannot use them as
692 we still support Windows XP and Windows 2003. */
694 status
= (tzi_status
>= 0 && tzi_status
<= 2)
695 && FileTimeToSystemTime (&utc_time
.ft_time
, &utc_sys_time
)
696 && SystemTimeToTzSpecificLocalTime (&tzi
, &utc_sys_time
, &local_sys_time
)
697 && SystemTimeToFileTime (&local_sys_time
, &local_time
.ft_time
);
699 /* An error has occurred, return invalid_tzoff */
702 *off
= __gnat_invalid_tzoff
;
705 if (local_time
.ull_time
> utc_time
.ull_time
) {
706 *off
= (long) ((local_time
.ull_time
- utc_time
.ull_time
)
710 *off
= - (long) ((utc_time
.ull_time
- local_time
.ull_time
)
719 #elif defined (__Lynx__)
721 /* On Lynx, all time values are treated in GMT */
723 /* As of LynxOS 3.1.0a patch level 040, LynuxWorks changes the
724 prototype to the C library function localtime_r from the POSIX.4
725 Draft 9 to the POSIX 1.c version. Before this change the following
726 spec is required. Only use when ___THREADS_POSIX4ad4__ is defined,
727 the Lynx convention when building against the legacy API. */
730 __gnat_localtime_tzoff (const time_t *, const int *, long *);
733 __gnat_localtime_tzoff (const time_t *timer
, const int *is_historic
, long *off
)
740 /* Other targets except Lynx and Windows provide a standard localtime_r */
742 #define Lock_Task system__soft_links__lock_task
743 extern void (*Lock_Task
) (void);
745 #define Unlock_Task system__soft_links__unlock_task
746 extern void (*Unlock_Task
) (void);
749 __gnat_localtime_tzoff (const time_t *, const int *, long *);
752 __gnat_localtime_tzoff (const time_t *timer ATTRIBUTE_UNUSED
,
753 const int *is_historic ATTRIBUTE_UNUSED
,
754 long *off ATTRIBUTE_UNUSED
)
756 struct tm tp ATTRIBUTE_UNUSED
;
758 /* AIX, HPUX, Sun Solaris */
759 #if defined (_AIX) || defined (__hpux__) || defined (__sun__)
763 localtime_r (timer
, &tp
);
764 *off
= (long) -timezone
;
768 /* Correct the offset if Daylight Saving Time is in effect */
775 #elif defined (__vxworks)
780 localtime_r (timer
, &tp
);
782 /* Try to read the environment variable TIMEZONE. The variable may not have
783 been initialize, in that case return an offset of zero (0) for UTC. */
785 char *tz_str
= getenv ("TIMEZONE");
787 if ((tz_str
== NULL
) || (*tz_str
== '\0'))
791 char *tz_start
, *tz_end
;
793 /* The format of the data contained in TIMEZONE is N::U:S:E where N is the
794 name of the time zone, U are the minutes difference from UTC, S is the
795 start of DST in mmddhh and E is the end of DST in mmddhh. Extracting
796 the value of U involves setting two pointers, one at the beginning and
797 one at the end of the value. The end pointer is then set to null in
798 order to delimit a string slice for atol to process. */
800 tz_start
= index (tz_str
, ':') + 2;
801 tz_end
= index (tz_start
, ':');
804 /* The Ada layer expects an offset in seconds. Note that we must reverse
805 the sign of the result since west is positive and east is negative on
808 *off
= -atol (tz_start
) * 60;
810 /* Correct the offset if Daylight Saving Time is in effect */
819 /* Darwin, Free BSD, Linux, where component tm_gmtoff is present in
822 #elif defined (__APPLE__) || defined (__FreeBSD__) || defined (__linux__) \
823 || defined (__GLIBC__)
825 localtime_r (timer
, &tp
);
829 /* Default: treat all time values in GMT */
834 #endif /* defined(_AIX) ... */
843 /* __gnat_get_task_options is used by s-taprop.adb only for VxWorks. This
844 function returns the options to be set when creating a new task. It fetches
845 the options assigned to the current task (parent), so offering some user
846 level control over the options for a task hierarchy. It forces VX_FP_TASK
847 because it is almost always required. On processors with the SPE
848 category, VX_SPE_TASK should be used instead to enable the SPE. */
849 extern int __gnat_get_task_options (void);
852 __gnat_get_task_options (void)
856 /* Get the options for the task creator */
857 taskOptionsGet (taskIdSelf (), &options
);
859 /* Force VX_FP_TASK or VX_SPE_TASK as needed */
860 #if defined (__SPE__)
861 options
|= VX_SPE_TASK
;
863 options
|= VX_FP_TASK
;
866 /* Mask those bits that are not under user control */
867 #ifdef VX_USR_TASK_OPTIONS
868 return options
& VX_USR_TASK_OPTIONS
;
877 __gnat_is_file_not_found_error (int errno_val
) {
881 /* In the case of VxWorks, we also have to take into account various
882 * filesystem-specific variants of this error.
884 #if ! defined (VTHREADS) && (_WRS_VXWORKS_MAJOR < 7)
885 case S_dosFsLib_FILE_NOT_FOUND
:
887 #if ! defined (__RTP__) && (! defined (VTHREADS) || defined (__VXWORKSMILS__))
888 case S_nfsLib_NFSERR_NOENT
:
890 #if defined (__RTP__)
891 /* An RTP can return an NFS file not found, and the NFS bits must
892 first be masked on to check the errno. */
893 case M_nfsStat
| ENOENT
:
905 /* Provide extern symbols for sig* as needed by the tasking run-time, instead
906 of static inline functions. */
911 _sigismember (sigset_t
*set
, int signum
)
913 return sigismember (set
, signum
);
917 _sigaddset (sigset_t
*set
, int signum
)
919 return sigaddset (set
, signum
);
923 _sigdelset (sigset_t
*set
, int signum
)
925 return sigdelset (set
, signum
);
929 _sigemptyset (sigset_t
*set
)
931 return sigemptyset (set
);
935 _sigfillset (sigset_t
*set
)
937 return sigfillset (set
);
944 return getpagesize ();