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"
68 #if defined (sun) && defined (__SVR4) && !defined (__vxworks)
69 /* The declaration is present in <time.h> but conditionalized
70 on a couple of macros we don't define. */
71 extern struct tm
*localtime_r(const time_t *, struct tm
*);
76 /* Don't use macros versions of this functions on VxWorks since they cause
77 imcompatible changes in some VxWorks versions */
89 (1) Opening a file with read mode fails if the file does not exist or
92 (2) Opening a file with append mode causes all subsequent writes to the
93 file to be forced to the then current end-of-file, regardless of
94 intervening calls to the fseek function.
96 (3) When a file is opened with update mode, both input and output may be
97 performed on the associated stream. However, output may not be directly
98 followed by input without an intervening call to the fflush function or
99 to a file positioning function (fseek, fsetpos, or rewind), and input
100 may not be directly followed by output without an intervening call to a
101 file positioning function, unless the input operation encounters
104 The other target dependent declarations here are for the two functions
105 __gnat_set_binary_mode and __gnat_set_text_mode:
107 void __gnat_set_binary_mode (int handle);
108 void __gnat_set_text_mode (int handle);
110 These functions have no effect in Unix (or similar systems where there is
111 no distinction between binary and text files), but in DOS (and similar
112 systems where text mode does CR/LF translation), these functions allow
113 the mode of the stream with the given handle (fileno can be used to get
114 the handle of a stream) to be changed dynamically. The returned result
115 is 0 if no error occurs and -1 if an error occurs.
117 Finally there is a boolean (character) variable
119 char __gnat_text_translation_required;
121 which is zero (false) in Unix mode, and one (true) in DOS mode, with a
122 true value indicating that text translation is required on text files
123 and that fopen supports the trailing t and b modifiers.
127 #if defined (WINNT) || defined (__CYGWIN__)
129 const char __gnat_text_translation_required
= 1;
132 #define WIN_SETMODE setmode
135 #define WIN_SETMODE _setmode
139 __gnat_set_binary_mode (int handle
)
141 WIN_SETMODE (handle
, O_BINARY
);
145 __gnat_set_text_mode (int handle
)
147 WIN_SETMODE (handle
, O_TEXT
);
153 __gnat_ttyname (int filedes
)
155 extern char *ttyname (int);
157 return ttyname (filedes
);
160 #endif /* __CYGWIN__ */
162 #if defined (__CYGWIN__) || defined (__MINGW32__)
167 int __gnat_is_windows_xp (void);
170 __gnat_is_windows_xp (void)
172 static int is_win_xp
=0, is_win_xp_checked
=0;
174 if (!is_win_xp_checked
)
176 OSVERSIONINFO version
;
178 is_win_xp_checked
= 1;
180 memset (&version
, 0, sizeof (version
));
181 version
.dwOSVersionInfoSize
= sizeof (version
);
183 is_win_xp
= GetVersionEx (&version
)
184 && version
.dwPlatformId
== VER_PLATFORM_WIN32_NT
185 && (version
.dwMajorVersion
> 5
186 || (version
.dwMajorVersion
== 5 && version
.dwMinorVersion
>= 1));
193 /* Get the bounds of the stack. The stack pointer is supposed to be
194 initialized to BASE when a thread is created and the stack can be extended
195 to LIMIT before reaching a guard page.
196 Note: for the main thread, the system automatically extend the stack, so
197 LIMIT is only the current limit. */
200 __gnat_get_stack_bounds (void **base
, void **limit
)
204 /* We know that the first field of the TEB is the TIB. */
205 tib
= (NT_TIB
*)NtCurrentTeb ();
207 *base
= tib
->StackBase
;
208 *limit
= tib
->StackLimit
;
211 #endif /* __CYGWIN__ || __MINGW32__ */
215 /* Return the name of the tty. Under windows there is no name for
216 the tty, so this function, if connected to a tty, returns the generic name
220 __gnat_ttyname (int filedes
)
222 if (isatty (filedes
))
228 #endif /* __MINGW32__ */
232 const char __gnat_text_translation_required
= 0;
234 /* These functions do nothing in non-DOS systems. */
237 __gnat_set_binary_mode (int handle ATTRIBUTE_UNUSED
)
242 __gnat_set_text_mode (int handle ATTRIBUTE_UNUSED
)
246 __gnat_ttyname (int filedes
)
248 #if defined (__vxworks) || defined (__nucleus)
251 extern char *ttyname (int);
253 return ttyname (filedes
);
254 #endif /* defined (__vxworks) || defined (__nucleus) */
258 #if defined (linux) || defined (sun) \
260 || defined (__MACHTEN__) || defined (__hpux__) || defined (_AIX) \
261 || (defined (__svr4__) && defined (i386)) || defined (__Lynx__) \
262 || defined (__CYGWIN__) || defined (__FreeBSD__) || defined (__OpenBSD__) \
263 || defined (__GLIBC__) || defined (__APPLE__)
267 # include <termios.h>
269 # include <conio.h> /* for getch(), kbhit() */
272 # include <termios.h>
277 extern char *decc$ga_stdscr
;
278 static int initted
= 0;
282 /* Implements the common processing for getc_immediate and
283 getc_immediate_nowait. */
285 extern void getc_immediate (FILE *, int *, int *);
286 extern void getc_immediate_nowait (FILE *, int *, int *, int *);
287 extern void getc_immediate_common (FILE *, int *, int *, int *, int);
289 /* Called by Get_Immediate (Foo); */
292 getc_immediate (FILE *stream
, int *ch
, int *end_of_file
)
296 getc_immediate_common (stream
, ch
, end_of_file
, &avail
, 1);
299 /* Called by Get_Immediate (Foo, Available); */
302 getc_immediate_nowait (FILE *stream
, int *ch
, int *end_of_file
, int *avail
)
304 getc_immediate_common (stream
, ch
, end_of_file
, avail
, 0);
307 /* Called by getc_immediate () and getc_immediate_nowait () */
310 getc_immediate_common (FILE *stream
,
316 #if defined (linux) || defined (sun) \
317 || defined (__CYGWIN32__) || defined (__MACHTEN__) || defined (__hpux__) \
318 || defined (_AIX) || (defined (__svr4__) && defined (i386)) \
319 || defined (__Lynx__) || defined (__FreeBSD__) || defined (__OpenBSD__) \
320 || defined (__GLIBC__) || defined (__APPLE__)
324 int eof_ch
= 4; /* Ctrl-D */
325 int fd
= fileno (stream
);
326 struct termios otermios_rec
, termios_rec
;
330 tcgetattr (fd
, &termios_rec
);
331 memcpy (&otermios_rec
, &termios_rec
, sizeof (struct termios
));
333 /* Set RAW mode, with no echo */
334 termios_rec
.c_lflag
= termios_rec
.c_lflag
& ~ICANON
& ~ECHO
;
336 #if defined(linux) || defined (sun) \
337 || defined (__MACHTEN__) || defined (__hpux__) \
338 || defined (_AIX) || (defined (__svr4__) && defined (i386)) \
339 || defined (__Lynx__) || defined (__FreeBSD__) || defined (__OpenBSD__) \
340 || defined (__GLIBC__) || defined (__APPLE__)
341 eof_ch
= termios_rec
.c_cc
[VEOF
];
343 /* If waiting (i.e. Get_Immediate (Char)), set MIN = 1 and wait for
344 a character forever. This doesn't seem to effect Ctrl-Z or
346 If not waiting (i.e. Get_Immediate (Char, Available)),
347 don't wait for anything but timeout immediately. */
348 termios_rec
.c_cc
[VMIN
] = waiting
;
349 termios_rec
.c_cc
[VTIME
] = 0;
351 tcsetattr (fd
, TCSANOW
, &termios_rec
);
355 /* Read is used here instead of fread, because fread doesn't
356 work on Solaris5 and Sunos4 in this situation. Maybe because we
357 are mixing calls that use file descriptors and streams. */
358 nread
= read (fd
, &c
, 1);
361 /* On Unix terminals, Ctrl-D (EOT) is an End of File. */
369 /* Everything else is ok */
370 else if (c
!= eof_ch
)
388 tcsetattr (fd
, TCSANOW
, &otermios_rec
);
394 int fd
= fileno (stream
);
405 *ch
= decc$
bsd_wgetch (decc$ga_stdscr
);
413 decc$
bsd_nocbreak ();
416 #elif defined (__MINGW32__)
417 int fd
= fileno (stream
);
419 int eot_ch
= 4; /* Ctrl-D */
436 char_waiting
= kbhit();
438 if (char_waiting
== 1)
456 #elif defined (__vxworks)
457 /* Bit masks of file descriptors to read from. */
458 struct fd_set readFds
;
459 /* Timeout before select returns if nothing can be read. */
460 struct timeval timeOut
;
462 int fd
= fileno (stream
);
471 /* If we do not want to wait, we have to set up fd in RAW mode. This
472 should be done outside this function as setting fd in RAW mode under
473 vxWorks flushes the buffer of fd. If the RAW mode was set here, the
474 buffer would be empty and we would always return that no character
478 /* Initialization of timeOut for its use with select. */
482 /* Initialization of readFds for its use with select;
483 FD is the only file descriptor to be monitored */
485 FD_SET (fd
, &readFds
);
488 /* We do all this processing to emulate a non blocking read. */
489 readable
= select (width
, &readFds
, NULL
, NULL
, &timeOut
);
490 if (readable
== ERROR
)
491 *avail
= -1, *end_of_file
= -1;
492 /* No character available in input. */
493 else if (readable
== 0)
494 *avail
= 0, *end_of_file
= 0;
497 nread
= read (fd
, &c
, 1);
499 *avail
= 1, *end_of_file
= 0;
502 *avail
= 0, *end_of_file
= 1;
505 *avail
= -1, *end_of_file
= -1;
509 /* We have to wait until we get a character */
515 /* Save the current mode of FD. */
516 option
= ioctl (fd
, FIOGETOPTIONS
, 0);
518 /* Set FD in RAW mode. */
519 status
= ioctl (fd
, FIOSETOPTIONS
, OPT_RAW
);
522 nread
= read (fd
, &c
, 1);
524 *avail
= 1, *end_of_file
= 0;
527 *avail
= 0, *end_of_file
= 1;
528 /* Else there is an ERROR. */
531 /* Revert FD to its previous mode. */
532 status
= ioctl (fd
, FIOSETOPTIONS
, option
);
540 /* If we're not on a terminal, then we don't need any fancy processing.
541 Also this is the only thing that's left if we're not on one of the
542 supported systems; which means that for non supported systems,
543 get_immediate may wait for a carriage return on terminals. */
544 *ch
= fgetc (stream
);
558 /* The following definitions are provided in NT to support Windows based
564 /* Provide functions to echo the values passed to WinMain (windows bindings
565 will want to import these). We use the same names as the routines used
566 by AdaMagic for compatibility. */
568 char *rts_get_hInstance (void);
569 char *rts_get_hPrevInstance (void);
570 char *rts_get_lpCommandLine (void);
571 int rts_get_nShowCmd (void);
574 rts_get_hInstance (void)
576 return (char *)GetModuleHandleA (0);
580 rts_get_hPrevInstance (void)
586 rts_get_lpCommandLine (void)
588 return GetCommandLineA ();
592 rts_get_nShowCmd (void)
600 /* This gets around a problem with using the old threads library on VMS 7.0. */
602 extern long get_gmtoff (void);
610 t
= time ((time_t) 0);
612 return ts
->tm_gmtoff
;
616 /* This value is returned as the time zone offset when a valid value
617 cannot be determined. It is simply a bizarre value that will never
618 occur. It is 3 days plus 73 seconds (offset is in seconds). */
620 long __gnat_invalid_tzoff
= 259273;
622 /* Definition of __gnat_localtime_r used by a-calend.adb */
624 #if defined (__MINGW32__)
628 /* For the Cert run times on native Windows we use dummy functions
629 for locking and unlocking tasks since we do not support multiple
630 threads on this configuration (Cert run time on native Windows). */
634 void (*Lock_Task
) () = &dummy
;
635 void (*Unlock_Task
) () = &dummy
;
639 #define Lock_Task system__soft_links__lock_task
640 extern void (*Lock_Task
) (void);
642 #define Unlock_Task system__soft_links__unlock_task
643 extern void (*Unlock_Task
) (void);
647 /* Reentrant localtime for Windows. */
650 __gnat_localtime_tzoff (const time_t *, const int *, long *);
652 static const unsigned long long w32_epoch_offset
= 11644473600ULL;
654 __gnat_localtime_tzoff (const time_t *timer
, const int *is_historic
, long *off
)
656 TIME_ZONE_INFORMATION tzi
;
669 tzi_status
= GetTimeZoneInformation (&tzi
);
671 /* Processing for RTX targets or cases where we simply want to extract the
672 offset of the current time zone, regardless of the date. A value of "0"
673 for flag "is_historic" signifies that the date is NOT historic, see the
674 body of Ada.Calendar.UTC_Time_Offset. */
676 if (rtx_active
|| *is_historic
== 0) {
679 /* The system is operating in the range covered by the StandardDate
681 if (tzi_status
== TIME_ZONE_ID_STANDARD
) {
682 *off
= *off
+ tzi
.StandardBias
;
685 /* The system is operating in the range covered by the DaylightDate
687 else if (tzi_status
== TIME_ZONE_ID_DAYLIGHT
) {
688 *off
= *off
+ tzi
.DaylightBias
;
694 /* Time zone offset calculations for a historic or future date */
700 unsigned long long ull_time
;
701 } utc_time
, local_time
;
703 SYSTEMTIME utc_sys_time
, local_sys_time
;
706 /* First convert unix time_t structure to windows FILETIME format. */
707 utc_time
.ull_time
= ((unsigned long long) *timer
+ w32_epoch_offset
)
710 /* If GetTimeZoneInformation does not return a value between 0 and 2 then
711 it means that we were not able to retrieve timezone informations. Note
712 that we cannot use here FileTimeToLocalFileTime as Windows will use in
713 always in this case the current timezone setting. As suggested on MSDN
714 we use the following three system calls to get the right information.
715 Note also that starting with Windows Vista new functions are provided
716 to get timezone settings that depend on the year. We cannot use them as
717 we still support Windows XP and Windows 2003. */
719 status
= (tzi_status
>= 0 && tzi_status
<= 2)
720 && FileTimeToSystemTime (&utc_time
.ft_time
, &utc_sys_time
)
721 && SystemTimeToTzSpecificLocalTime (&tzi
, &utc_sys_time
, &local_sys_time
)
722 && SystemTimeToFileTime (&local_sys_time
, &local_time
.ft_time
);
724 /* An error has occurred, return invalid_tzoff */
727 *off
= __gnat_invalid_tzoff
;
730 if (local_time
.ull_time
> utc_time
.ull_time
) {
731 *off
= (long) ((local_time
.ull_time
- utc_time
.ull_time
)
735 *off
= - (long) ((utc_time
.ull_time
- local_time
.ull_time
)
746 /* On Lynx, all time values are treated in GMT */
748 #if defined (__Lynx__)
750 /* As of LynxOS 3.1.0a patch level 040, LynuxWorks changes the
751 prototype to the C library function localtime_r from the POSIX.4
752 Draft 9 to the POSIX 1.c version. Before this change the following
753 spec is required. Only use when ___THREADS_POSIX4ad4__ is defined,
754 the Lynx convention when building against the legacy API. */
757 __gnat_localtime_tzoff (const time_t *, const int *, long *);
760 __gnat_localtime_tzoff (const time_t *timer
, const int *is_historic
, long *off
)
767 /* VMS does not need __gnat_localtime_tzoff */
771 /* Other targets except Lynx, VMS and Windows provide a standard localtime_r */
775 #define Lock_Task system__soft_links__lock_task
776 extern void (*Lock_Task
) (void);
778 #define Unlock_Task system__soft_links__unlock_task
779 extern void (*Unlock_Task
) (void);
782 __gnat_localtime_tzoff (const time_t *, const int *, long *);
785 __gnat_localtime_tzoff (const time_t *timer
, const int *is_historic
, long *off
)
789 /* AIX, HPUX, Sun Solaris */
790 #if defined (_AIX) || defined (__hpux__) || defined (sun)
794 localtime_r (timer
, &tp
);
795 *off
= (long) -timezone
;
799 /* Correct the offset if Daylight Saving Time is in effect */
806 #elif defined (__vxworks)
811 localtime_r (timer
, &tp
);
813 /* Try to read the environment variable TIMEZONE. The variable may not have
814 been initialize, in that case return an offset of zero (0) for UTC. */
816 char *tz_str
= getenv ("TIMEZONE");
818 if ((tz_str
== NULL
) || (*tz_str
== '\0'))
822 char *tz_start
, *tz_end
;
824 /* The format of the data contained in TIMEZONE is N::U:S:E where N is the
825 name of the time zone, U are the minutes difference from UTC, S is the
826 start of DST in mmddhh and E is the end of DST in mmddhh. Extracting
827 the value of U involves setting two pointers, one at the beginning and
828 one at the end of the value. The end pointer is then set to null in
829 order to delimit a string slice for atol to process. */
831 tz_start
= index (tz_str
, ':') + 2;
832 tz_end
= index (tz_start
, ':');
835 /* The Ada layer expects an offset in seconds. Note that we must reverse
836 the sign of the result since west is positive and east is negative on
839 *off
= -atol (tz_start
) * 60;
841 /* Correct the offset if Daylight Saving Time is in effect */
850 /* Darwin, Free BSD, Linux, where component tm_gmtoff is present in
853 #elif defined (__APPLE__) || defined (__FreeBSD__) || defined (linux) ||\
856 localtime_r (timer
, &tp
);
860 /* Default: treat all time values in GMT */
876 /* __gnat_get_task_options is used by s-taprop.adb only for VxWorks. This
877 function returns the options to be set when creating a new task. It fetches
878 the options assigned to the current task (parent), so offering some user
879 level control over the options for a task hierarchy. It forces VX_FP_TASK
880 because it is almost always required. On processors with the SPE
881 category, VX_SPE_TASK should be used instead to enable the SPE. */
882 extern int __gnat_get_task_options (void);
885 __gnat_get_task_options (void)
889 /* Get the options for the task creator */
890 taskOptionsGet (taskIdSelf (), &options
);
892 /* Force VX_FP_TASK or VX_SPE_TASK as needed */
893 #if defined (__SPE__)
894 options
|= VX_SPE_TASK
;
896 options
|= VX_FP_TASK
;
899 /* Mask those bits that are not under user control */
900 #ifdef VX_USR_TASK_OPTIONS
901 return options
& VX_USR_TASK_OPTIONS
;
910 __gnat_is_file_not_found_error (int errno_val
) {
914 /* In the case of VxWorks, we also have to take into account various
915 * filesystem-specific variants of this error.
917 #if ! defined (VTHREADS)
918 case S_dosFsLib_FILE_NOT_FOUND
:
920 #if ! defined (__RTP__) && (! defined (VTHREADS) || defined (__VXWORKSMILS__))
921 case S_nfsLib_NFSERR_NOENT
:
933 /* Provide extern symbols for sig* as needed by the tasking run-time, instead
934 of static inline functions. */
939 _sigismember (sigset_t
*set
, int signum
)
941 return sigismember (set
, signum
);
945 _sigaddset (sigset_t
*set
, int signum
)
947 return sigaddset (set
, signum
);
951 _sigdelset (sigset_t
*set
, int signum
)
953 return sigdelset (set
, signum
);
957 _sigemptyset (sigset_t
*set
)
959 return sigemptyset (set
);
963 _sigfillset (sigset_t
*set
)
965 return sigfillset (set
);
972 return getpagesize ();