2010-07-27 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc/alias-decl.git] / gcc / ada / sysdep.c
blobc978c036a35e20dcce5769d7385d78a5a945b208
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * S Y S D E P *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2010, 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 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. *
17 * *
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. *
21 * *
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/>. *
26 * *
27 * GNAT was originally developed by the GNAT team at New York University. *
28 * Extensive contributions were provided by Ada Core Technologies Inc. *
29 * *
30 ****************************************************************************/
32 /* This file contains system dependent symbols that are referenced in the
33 GNAT Run Time Library */
35 #ifdef __vxworks
36 #include "ioLib.h"
37 #if ! defined (VTHREADS)
38 #include "dosFsLib.h"
39 #endif
40 #if ! defined (__RTP__) && (! defined (VTHREADS) || defined (__VXWORKSMILS__))
41 # include "nfsLib.h"
42 #endif
43 #include "selectLib.h"
44 #include "vxWorks.h"
45 #endif
47 #ifdef IN_RTS
48 #define POSIX
49 #include "tconfig.h"
50 #include "tsystem.h"
51 #include <fcntl.h>
52 #include <sys/stat.h>
53 #ifdef VMS
54 #include <unixio.h>
55 #endif
56 #else
57 #include "config.h"
58 #include "system.h"
59 #endif
61 #include <time.h>
62 #include <errno.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 *);
68 #endif
70 #include "adaint.h"
73 mode_read_text
74 open text file for reading
75 rt for DOS and Windows NT, r for Unix
77 mode_write_text
78 truncate to zero length or create text file for writing
79 wt for DOS and Windows NT, w for Unix
81 mode_append_text
82 append; open or create text file for writing at end-of-file
83 at for DOS and Windows NT, a for Unix
85 mode_read_binary
86 open binary file for reading
87 rb for DOS and Windows NT, r for Unix
89 mode_write_binary
90 truncate to zero length or create binary file for writing
91 wb for DOS and Windows NT, w for Unix
93 mode_append_binary
94 append; open or create binary file for writing at end-of-file
95 ab for DOS and Windows NT, a for Unix
97 mode_read_text_plus
98 open text file for update (reading and writing)
99 r+t for DOS and Windows NT, r+ for Unix
101 mode_write_text_plus
102 truncate to zero length or create text file for update
103 w+t for DOS and Windows NT, w+ for Unix
105 mode_append_text_plus
106 append; open or create text file for update, writing at end-of-file
107 a+t for DOS and Windows NT, a+ for Unix
109 mode_read_binary_plus
110 open binary file for update (reading and writing)
111 r+b for DOS and Windows NT, r+ for Unix
113 mode_write_binary_plus
114 truncate to zero length or create binary file for update
115 w+b for DOS and Windows NT, w+ for Unix
117 mode_append_binary_plus
118 append; open or create binary file for update, writing at end-of-file
119 a+b for DOS and Windows NT, a+ for Unix
121 Notes:
123 (1) Opening a file with read mode fails if the file does not exist or
124 cannot be read.
126 (2) Opening a file with append mode causes all subsequent writes to the
127 file to be forced to the then current end-of-file, regardless of
128 intervening calls to the fseek function.
130 (3) When a file is opened with update mode, both input and output may be
131 performed on the associated stream. However, output may not be directly
132 followed by input without an intervening call to the fflush function or
133 to a file positioning function (fseek, fsetpos, or rewind), and input
134 may not be directly followed by output without an intervening call to a
135 file positioning function, unless the input operation encounters
136 end-of-file.
138 The other target dependent declarations here are for the two functions
139 __gnat_set_binary_mode and __gnat_set_text_mode:
141 void __gnat_set_binary_mode (int handle);
142 void __gnat_set_text_mode (int handle);
144 These functions have no effect in Unix (or similar systems where there is
145 no distinction between binary and text files), but in DOS (and similar
146 systems where text mode does CR/LF translation), these functions allow
147 the mode of the stream with the given handle (fileno can be used to get
148 the handle of a stream) to be changed dynamically. The returned result
149 is 0 if no error occurs and -1 if an error occurs.
151 Finally there is a boolean (character) variable
153 char __gnat_text_translation_required;
155 which is zero (false) in Unix mode, and one (true) in DOS mode, with a
156 true value indicating that text translation is required on text files
157 and that fopen supports the trailing t and b modifiers.
161 #if defined(WINNT)
162 static const char *mode_read_text = "rt";
163 static const char *mode_write_text = "wt";
164 static const char *mode_append_text = "at";
165 static const char *mode_read_binary = "rb";
166 static const char *mode_write_binary = "wb";
167 static const char *mode_append_binary = "ab";
168 static const char *mode_read_text_plus = "r+t";
169 static const char *mode_write_text_plus = "w+t";
170 static const char *mode_append_text_plus = "a+t";
171 static const char *mode_read_binary_plus = "r+b";
172 static const char *mode_write_binary_plus = "w+b";
173 static const char *mode_append_binary_plus = "a+b";
174 const char __gnat_text_translation_required = 1;
176 void
177 __gnat_set_binary_mode (int handle)
179 _setmode (handle, O_BINARY);
182 void
183 __gnat_set_text_mode (int handle)
185 _setmode (handle, O_TEXT);
188 #ifdef __MINGW32__
189 #include <windows.h>
191 /* Return the name of the tty. Under windows there is no name for
192 the tty, so this function, if connected to a tty, returns the generic name
193 "console". */
195 char *
196 __gnat_ttyname (int filedes)
198 if (isatty (filedes))
199 return "console";
200 else
201 return NULL;
204 /* This function is needed to fix a bug under Win95/98. Under these platforms
205 doing :
206 ch1 = getch();
207 ch2 = fgetc (stdin);
209 will put the same character into ch1 and ch2. It seem that the character
210 read by getch() is not correctly removed from the buffer. Even a
211 fflush(stdin) does not fix the bug. This bug does not appear under Window
212 NT. So we have two version of this routine below one for 95/98 and one for
213 NT/2000 version of Windows. There is also a special routine (winflushinit)
214 that will be called only the first time to check which version of Windows
215 we are running running on to set the right routine to use.
217 This problem occurs when using Text_IO.Get_Line after Text_IO.Get_Immediate
218 for example.
220 Calling FlushConsoleInputBuffer just after getch() fix the bug under
221 95/98. */
223 #ifdef RTX
225 static void winflush_nt (void);
227 /* winflush_function will do nothing since we only have problems with Windows
228 95/98 which are not supported by RTX. */
230 static void (*winflush_function) (void) = winflush_nt;
232 static void
233 winflush_nt (void)
235 /* Does nothing as there is no problem under NT. */
238 #else
240 static void winflush_init (void);
242 static void winflush_95 (void);
244 static void winflush_nt (void);
246 int __gnat_is_windows_xp (void);
248 /* winflusfunction is set first to the winflushinit function which will check
249 the OS version 95/98 or NT/2000 */
251 static void (*winflush_function) (void) = winflush_init;
253 /* This function does the runtime check of the OS version and then sets
254 winflush_function to the appropriate function and then call it. */
256 static void
257 winflush_init (void)
259 DWORD dwVersion = GetVersion();
261 if (dwVersion < 0x80000000) /* Windows NT/2000 */
262 winflush_function = winflush_nt;
263 else /* Windows 95/98 */
264 winflush_function = winflush_95;
266 (*winflush_function)(); /* Perform the 'flush' */
270 static void
271 winflush_95 (void)
273 FlushConsoleInputBuffer (GetStdHandle (STD_INPUT_HANDLE));
276 static void
277 winflush_nt (void)
279 /* Does nothing as there is no problem under NT. */
283 __gnat_is_windows_xp (void)
285 static int is_win_xp=0, is_win_xp_checked=0;
287 if (!is_win_xp_checked)
289 OSVERSIONINFO version;
291 is_win_xp_checked = 1;
293 memset (&version, 0, sizeof (version));
294 version.dwOSVersionInfoSize = sizeof (version);
296 is_win_xp = GetVersionEx (&version)
297 && version.dwPlatformId == VER_PLATFORM_WIN32_NT
298 && (version.dwMajorVersion > 5
299 || (version.dwMajorVersion == 5 && version.dwMinorVersion >= 1));
301 return is_win_xp;
304 #endif
306 #endif
308 #else
310 static const char *mode_read_text = "r";
311 static const char *mode_write_text = "w";
312 static const char *mode_append_text = "a";
313 static const char *mode_read_binary = "r";
314 static const char *mode_write_binary = "w";
315 static const char *mode_append_binary = "a";
316 static const char *mode_read_text_plus = "r+";
317 static const char *mode_write_text_plus = "w+";
318 static const char *mode_append_text_plus = "a+";
319 static const char *mode_read_binary_plus = "r+";
320 static const char *mode_write_binary_plus = "w+";
321 static const char *mode_append_binary_plus = "a+";
322 const char __gnat_text_translation_required = 0;
324 /* These functions do nothing in non-DOS systems. */
326 void
327 __gnat_set_binary_mode (int handle ATTRIBUTE_UNUSED)
331 void
332 __gnat_set_text_mode (int handle ATTRIBUTE_UNUSED)
335 char *
336 __gnat_ttyname (int filedes)
338 #if defined (__vxworks) || defined (__nucleus)
339 return "";
340 #else
341 extern char *ttyname (int);
343 return ttyname (filedes);
344 #endif /* defined (__vxworks) || defined (__nucleus) */
346 #endif
348 #if defined (linux) || defined (sun) || defined (sgi) \
349 || (defined (__osf__) && ! defined (__alpha_vxworks)) || defined (WINNT) \
350 || defined (__MACHTEN__) || defined (__hpux__) || defined (_AIX) \
351 || (defined (__svr4__) && defined (i386)) || defined (__Lynx__) \
352 || defined (__CYGWIN__) || defined (__FreeBSD__) || defined (__OpenBSD__) \
353 || defined (__GLIBC__) || defined (__APPLE__)
355 #ifdef __MINGW32__
356 #if OLD_MINGW
357 #include <termios.h>
358 #else
359 #include <conio.h> /* for getch(), kbhit() */
360 #endif
361 #else
362 #include <termios.h>
363 #endif
365 #else
366 #if defined (VMS)
367 extern char *decc$ga_stdscr;
368 static int initted = 0;
369 #endif
370 #endif
372 /* Implements the common processing for getc_immediate and
373 getc_immediate_nowait. */
375 extern void getc_immediate (FILE *, int *, int *);
376 extern void getc_immediate_nowait (FILE *, int *, int *, int *);
377 extern void getc_immediate_common (FILE *, int *, int *, int *, int);
379 /* Called by Get_Immediate (Foo); */
381 void
382 getc_immediate (FILE *stream, int *ch, int *end_of_file)
384 int avail;
386 getc_immediate_common (stream, ch, end_of_file, &avail, 1);
389 /* Called by Get_Immediate (Foo, Available); */
391 void
392 getc_immediate_nowait (FILE *stream, int *ch, int *end_of_file, int *avail)
394 getc_immediate_common (stream, ch, end_of_file, avail, 0);
397 /* Called by getc_immediate () and getc_immediate_nowait () */
399 void
400 getc_immediate_common (FILE *stream,
401 int *ch,
402 int *end_of_file,
403 int *avail,
404 int waiting)
406 #if defined (linux) || defined (sun) || defined (sgi) \
407 || (defined (__osf__) && ! defined (__alpha_vxworks)) \
408 || defined (__CYGWIN32__) || defined (__MACHTEN__) || defined (__hpux__) \
409 || defined (_AIX) || (defined (__svr4__) && defined (i386)) \
410 || defined (__Lynx__) || defined (__FreeBSD__) || defined (__OpenBSD__) \
411 || defined (__GLIBC__) || defined (__APPLE__)
412 char c;
413 int nread;
414 int good_one = 0;
415 int eof_ch = 4; /* Ctrl-D */
416 int fd = fileno (stream);
417 struct termios otermios_rec, termios_rec;
419 if (isatty (fd))
421 tcgetattr (fd, &termios_rec);
422 memcpy (&otermios_rec, &termios_rec, sizeof (struct termios));
424 /* Set RAW mode, with no echo */
425 termios_rec.c_lflag = termios_rec.c_lflag & ~ICANON & ~ECHO;
427 #if defined(linux) || defined (sun) || defined (sgi) \
428 || defined (__osf__) || defined (__MACHTEN__) || defined (__hpux__) \
429 || defined (_AIX) || (defined (__svr4__) && defined (i386)) \
430 || defined (__Lynx__) || defined (__FreeBSD__) || defined (__OpenBSD__) \
431 || defined (__GLIBC__) || defined (__APPLE__)
432 eof_ch = termios_rec.c_cc[VEOF];
434 /* If waiting (i.e. Get_Immediate (Char)), set MIN = 1 and wait for
435 a character forever. This doesn't seem to effect Ctrl-Z or
436 Ctrl-C processing.
437 If not waiting (i.e. Get_Immediate (Char, Available)),
438 don't wait for anything but timeout immediately. */
439 termios_rec.c_cc[VMIN] = waiting;
440 termios_rec.c_cc[VTIME] = 0;
441 #endif
442 tcsetattr (fd, TCSANOW, &termios_rec);
444 while (! good_one)
446 /* Read is used here instead of fread, because fread doesn't
447 work on Solaris5 and Sunos4 in this situation. Maybe because we
448 are mixing calls that use file descriptors and streams. */
449 nread = read (fd, &c, 1);
450 if (nread > 0)
452 /* On Unix terminals, Ctrl-D (EOT) is an End of File. */
453 if (c == eof_ch)
455 *avail = 0;
456 *end_of_file = 1;
457 good_one = 1;
460 /* Everything else is ok */
461 else if (c != eof_ch)
463 *avail = 1;
464 *end_of_file = 0;
465 good_one = 1;
469 else if (! waiting)
471 *avail = 0;
472 *end_of_file = 0;
473 good_one = 1;
475 else
476 good_one = 0;
479 tcsetattr (fd, TCSANOW, &otermios_rec);
480 *ch = c;
483 else
484 #elif defined (VMS)
485 int fd = fileno (stream);
487 if (isatty (fd))
489 if (initted == 0)
491 decc$bsd_initscr ();
492 initted = 1;
495 decc$bsd_cbreak ();
496 *ch = decc$bsd_wgetch (decc$ga_stdscr);
498 if (*ch == 4)
499 *end_of_file = 1;
500 else
501 *end_of_file = 0;
503 *avail = 1;
504 decc$bsd_nocbreak ();
506 else
507 #elif defined (__MINGW32__)
508 int fd = fileno (stream);
509 int char_waiting;
510 int eot_ch = 4; /* Ctrl-D */
512 if (isatty (fd))
514 if (waiting)
516 *ch = getch ();
517 (*winflush_function) ();
519 if (*ch == eot_ch)
520 *end_of_file = 1;
521 else
522 *end_of_file = 0;
524 *avail = 1;
526 else /* ! waiting */
528 char_waiting = kbhit();
530 if (char_waiting == 1)
532 *avail = 1;
533 *ch = getch ();
534 (*winflush_function) ();
536 if (*ch == eot_ch)
537 *end_of_file = 1;
538 else
539 *end_of_file = 0;
541 else
543 *avail = 0;
544 *end_of_file = 0;
548 else
549 #elif defined (__vxworks)
550 /* Bit masks of file descriptors to read from. */
551 struct fd_set readFds;
552 /* Timeout before select returns if nothing can be read. */
553 struct timeval timeOut;
554 char c;
555 int fd = fileno (stream);
556 int nread;
557 int option;
558 int readable;
559 int status;
560 int width;
562 if (isatty (fd))
564 /* If we do not want to wait, we have to set up fd in RAW mode. This
565 should be done outside this function as setting fd in RAW mode under
566 vxWorks flushes the buffer of fd. If the RAW mode was set here, the
567 buffer would be empty and we would always return that no character
568 is available */
569 if (! waiting)
571 /* Initialization of timeOut for its use with select. */
572 timeOut.tv_sec = 0;
573 timeOut.tv_usec = 0;
575 /* Initialization of readFds for its use with select;
576 FD is the only file descriptor to be monitored */
577 FD_ZERO (&readFds);
578 FD_SET (fd, &readFds);
579 width = 2;
581 /* We do all this processing to emulate a non blocking read. */
582 readable = select (width, &readFds, NULL, NULL, &timeOut);
583 if (readable == ERROR)
584 *avail = -1, *end_of_file = -1;
585 /* No character available in input. */
586 else if (readable == 0)
587 *avail = 0, *end_of_file = 0;
588 else
590 nread = read (fd, &c, 1);
591 if (nread > 0)
592 *avail = 1, *end_of_file = 0;
593 /* End Of File. */
594 else if (nread == 0)
595 *avail = 0, *end_of_file = 1;
596 /* Error. */
597 else
598 *avail = -1, *end_of_file = -1;
602 /* We have to wait until we get a character */
603 else
605 *avail = -1;
606 *end_of_file = -1;
608 /* Save the current mode of FD. */
609 option = ioctl (fd, FIOGETOPTIONS, 0);
611 /* Set FD in RAW mode. */
612 status = ioctl (fd, FIOSETOPTIONS, OPT_RAW);
613 if (status != -1)
615 nread = read (fd, &c, 1);
616 if (nread > 0)
617 *avail = 1, *end_of_file = 0;
618 /* End of file. */
619 else if (nread == 0)
620 *avail = 0, *end_of_file = 1;
621 /* Else there is an ERROR. */
624 /* Revert FD to its previous mode. */
625 status = ioctl (fd, FIOSETOPTIONS, option);
628 *ch = c;
630 else
631 #endif
633 /* If we're not on a terminal, then we don't need any fancy processing.
634 Also this is the only thing that's left if we're not on one of the
635 supported systems; which means that for non supported systems,
636 get_immediate may wait for a carriage return on terminals. */
637 *ch = fgetc (stream);
638 if (feof (stream))
640 *end_of_file = 1;
641 *avail = 0;
643 else
645 *end_of_file = 0;
646 *avail = 1;
651 /* The following definitions are provided in NT to support Windows based
652 Ada programs. */
654 #ifdef WINNT
655 #include <windows.h>
657 /* Provide functions to echo the values passed to WinMain (windows bindings
658 will want to import these). We use the same names as the routines used
659 by AdaMagic for compatibility. */
661 char *rts_get_hInstance (void);
662 char *rts_get_hPrevInstance (void);
663 char *rts_get_lpCommandLine (void);
664 int rts_get_nShowCmd (void);
666 char *
667 rts_get_hInstance (void)
669 return (char *)GetModuleHandleA (0);
672 char *
673 rts_get_hPrevInstance (void)
675 return 0;
678 char *
679 rts_get_lpCommandLine (void)
681 return GetCommandLineA ();
685 rts_get_nShowCmd (void)
687 return 1;
690 #endif /* WINNT */
691 #ifdef VMS
693 /* This gets around a problem with using the old threads library on VMS 7.0. */
695 extern long get_gmtoff (void);
697 long
698 get_gmtoff (void)
700 time_t t;
701 struct tm *ts;
703 t = time ((time_t) 0);
704 ts = localtime (&t);
705 return ts->tm_gmtoff;
707 #endif
709 /* This value is returned as the time zone offset when a valid value
710 cannot be determined. It is simply a bizarre value that will never
711 occur. It is 3 days plus 73 seconds (offset is in seconds). */
713 long __gnat_invalid_tzoff = 259273;
715 /* Definition of __gnat_localtime_r used by a-calend.adb */
717 #if defined (__MINGW32__)
719 #ifdef CERT
721 /* For the Cert run times on native Windows we use dummy functions
722 for locking and unlocking tasks since we do not support multiple
723 threads on this configuration (Cert run time on native Windows). */
725 void dummy (void) {}
727 void (*Lock_Task) () = &dummy;
728 void (*Unlock_Task) () = &dummy;
730 #else
732 #define Lock_Task system__soft_links__lock_task
733 extern void (*Lock_Task) (void);
735 #define Unlock_Task system__soft_links__unlock_task
736 extern void (*Unlock_Task) (void);
738 #endif
740 /* Reentrant localtime for Windows. */
742 extern void
743 __gnat_localtime_tzoff (const time_t *, long *);
745 static const unsigned long long w32_epoch_offset = 11644473600ULL;
746 void
747 __gnat_localtime_tzoff (const time_t *timer, long *off)
749 union
751 FILETIME ft_time;
752 unsigned long long ull_time;
753 } utc_time, local_time;
755 SYSTEMTIME utc_sys_time, local_sys_time;
756 TIME_ZONE_INFORMATION tzi;
758 BOOL status = 1;
759 DWORD tzi_status;
761 (*Lock_Task) ();
763 #ifdef RTX
765 tzi_status = GetTimeZoneInformation (&tzi);
766 *off = tzi.Bias;
767 if (tzi_status == TIME_ZONE_ID_STANDARD)
768 /* The system is operating in the range covered by the StandardDate
769 member. */
770 *off = *off + tzi.StandardBias;
771 else if (tzi_status == TIME_ZONE_ID_DAYLIGHT)
772 /* The system is operating in the range covered by the DaylightDate
773 member. */
774 *off = *off + tzi.DaylightBias;
775 *off = *off * -60;
777 #else
779 /* First convert unix time_t structure to windows FILETIME format. */
780 utc_time.ull_time = ((unsigned long long) *timer + w32_epoch_offset)
781 * 10000000ULL;
783 tzi_status = GetTimeZoneInformation (&tzi);
785 /* If GetTimeZoneInformation does not return a value between 0 and 2 then
786 it means that we were not able to retrieve timezone informations.
787 Note that we cannot use here FileTimeToLocalFileTime as Windows will use
788 in always in this case the current timezone setting. As suggested on
789 MSDN we use the following three system calls to get the right information.
790 Note also that starting with Windows Vista new functions are provided to
791 get timezone settings that depend on the year. We cannot use them as we
792 still support Windows XP and Windows 2003. */
793 status = (tzi_status >= 0 && tzi_status <= 2)
794 && FileTimeToSystemTime (&utc_time.ft_time, &utc_sys_time)
795 && SystemTimeToTzSpecificLocalTime (&tzi, &utc_sys_time, &local_sys_time)
796 && SystemTimeToFileTime (&local_sys_time, &local_time.ft_time);
798 if (!status)
799 /* An error occurs so return invalid_tzoff. */
800 *off = __gnat_invalid_tzoff;
801 else
802 if (local_time.ull_time > utc_time.ull_time)
803 *off = (long) ((local_time.ull_time - utc_time.ull_time) / 10000000ULL);
804 else
805 *off = - (long) ((utc_time.ull_time - local_time.ull_time) / 10000000ULL);
807 #endif
809 (*Unlock_Task) ();
812 #else
814 /* On Lynx, all time values are treated in GMT */
816 #if defined (__Lynx__)
818 /* As of LynxOS 3.1.0a patch level 040, LynuxWorks changes the
819 prototype to the C library function localtime_r from the POSIX.4
820 Draft 9 to the POSIX 1.c version. Before this change the following
821 spec is required. Only use when ___THREADS_POSIX4ad4__ is defined,
822 the Lynx convention when building against the legacy API. */
824 extern void
825 __gnat_localtime_tzoff (const time_t *, long *);
827 void
828 __gnat_localtime_tzoff (const time_t *timer, long *off)
830 *off = 0;
833 #else
835 /* VMS does not need __gnat_locatime_tzoff */
837 #if defined (VMS)
839 /* Other targets except Lynx, VMS and Windows provide a standard locatime_r */
841 #else
843 #define Lock_Task system__soft_links__lock_task
844 extern void (*Lock_Task) (void);
846 #define Unlock_Task system__soft_links__unlock_task
847 extern void (*Unlock_Task) (void);
849 extern void
850 __gnat_localtime_tzoff (const time_t *, long *);
852 void
853 __gnat_localtime_tzoff (const time_t *timer, long *off)
855 struct tm tp;
857 /* AIX, HPUX, SGI Irix, Sun Solaris */
858 #if defined (_AIX) || defined (__hpux__) || defined (sgi) || defined (sun)
860 (*Lock_Task) ();
862 localtime_r (timer, &tp);
863 *off = (long) -timezone;
865 (*Unlock_Task) ();
867 /* Correct the offset if Daylight Saving Time is in effect */
869 if (tp.tm_isdst > 0)
870 *off = *off + 3600;
873 /* VxWorks */
874 #elif defined (__vxworks)
875 #include <stdlib.h>
877 (*Lock_Task) ();
879 localtime_r (timer, &tp);
881 /* Try to read the environment variable TIMEZONE. The variable may not have
882 been initialize, in that case return an offset of zero (0) for UTC. */
884 char *tz_str = getenv ("TIMEZONE");
886 if ((tz_str == NULL) || (*tz_str == '\0'))
887 *off = 0;
888 else
890 char *tz_start, *tz_end;
892 /* The format of the data contained in TIMEZONE is N::U:S:E where N is the
893 name of the time zone, U are the minutes difference from UTC, S is the
894 start of DST in mmddhh and E is the end of DST in mmddhh. Extracting
895 the value of U involves setting two pointers, one at the beginning and
896 one at the end of the value. The end pointer is then set to null in
897 order to delimit a string slice for atol to process. */
899 tz_start = index (tz_str, ':') + 2;
900 tz_end = index (tz_start, ':');
901 tz_end = '\0';
903 /* The Ada layer expects an offset in seconds. Note that we must reverse
904 the sign of the result since west is positive and east is negative on
905 VxWorks targets. */
907 *off = -atol (tz_start) * 60;
909 /* Correct the offset if Daylight Saving Time is in effect */
911 if (tp.tm_isdst > 0)
912 *off = *off + 3600;
915 (*Unlock_Task) ();
918 /* Darwin, Free BSD, Linux, Tru64, where component tm_gmtoff is present in
919 struct tm */
921 #elif defined (__APPLE__) || defined (__FreeBSD__) || defined (linux) ||\
922 (defined (__alpha__) && defined (__osf__)) || defined (__GLIBC__)
924 localtime_r (timer, &tp);
925 *off = tp.tm_gmtoff;
928 /* Default: treat all time values in GMT */
930 #else
931 *off = 0;
933 #endif
936 #endif
937 #endif
938 #endif
940 #ifdef __vxworks
942 #include <taskLib.h>
944 /* __gnat_get_task_options is used by s-taprop.adb only for VxWorks. This
945 function returns the options to be set when creating a new task. It fetches
946 the options assigned to the current task (parent), so offering some user
947 level control over the options for a task hierarchy. It forces VX_FP_TASK
948 because it is almost always required. On processors with the SPE
949 category, VX_SPE_TASK is needed to enable the SPE. */
950 extern int __gnat_get_task_options (void);
953 __gnat_get_task_options (void)
955 int options;
957 /* Get the options for the task creator */
958 taskOptionsGet (taskIdSelf (), &options);
960 /* Force VX_FP_TASK because it is almost always required */
961 options |= VX_FP_TASK;
962 #if defined (__SPE__)
963 options |= VX_SPE_TASK;
964 #endif
966 /* Mask those bits that are not under user control */
967 #ifdef VX_USR_TASK_OPTIONS
968 return options & VX_USR_TASK_OPTIONS;
969 #else
970 return options;
971 #endif
974 #endif
977 __gnat_is_file_not_found_error (int errno_val) {
978 switch (errno_val) {
979 case ENOENT:
980 #ifdef __vxworks
981 /* In the case of VxWorks, we also have to take into account various
982 * filesystem-specific variants of this error.
984 #if ! defined (VTHREADS)
985 case S_dosFsLib_FILE_NOT_FOUND:
986 #endif
987 #if ! defined (__RTP__) && (! defined (VTHREADS) || defined (__VXWORKSMILS__))
988 case S_nfsLib_NFSERR_NOENT:
989 #endif
990 #endif
991 return 1;
993 default:
994 return 0;