OSDN Git Service

Fix typos in gcc/ada.
[pf3gnuchains/gcc-fork.git] / gcc / ada / sysdep.c
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  ****************************************************************************/
31
32 /* This file contains system dependent symbols that are referenced in the
33    GNAT Run Time Library */
34
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
46
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
60
61 #include <time.h>
62 #include <errno.h>
63
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
69
70 #include "adaint.h"
71
72 /*
73    mode_read_text
74    open text file for reading
75    rt for DOS and Windows NT, r for Unix
76
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
80
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
84
85    mode_read_binary
86    open binary file for reading
87    rb for DOS and Windows NT, r for Unix
88
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
92
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
96
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
100
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
104
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
108
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
112
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
116
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
120
121    Notes:
122
123    (1) Opening a file with read mode fails if the file does not exist or
124    cannot be read.
125
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.
129
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.
137
138    The other target dependent declarations here are for the two functions
139    __gnat_set_binary_mode and __gnat_set_text_mode:
140
141       void __gnat_set_binary_mode (int handle);
142       void __gnat_set_text_mode   (int handle);
143
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.
150
151    Finally there is a boolean (character) variable
152
153       char __gnat_text_translation_required;
154
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.
158
159 */
160
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;
175
176 void
177 __gnat_set_binary_mode (int handle)
178 {
179   _setmode (handle, O_BINARY);
180 }
181
182 void
183 __gnat_set_text_mode (int handle)
184 {
185   _setmode (handle, O_TEXT);
186 }
187
188 #ifdef __MINGW32__
189 #include <windows.h>
190
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".  */
194
195 char *
196 __gnat_ttyname (int filedes)
197 {
198   if (isatty (filedes))
199     return "console";
200   else
201     return NULL;
202 }
203
204 /* This function is needed to fix a bug under Win95/98. Under these platforms
205    doing :
206                 ch1 = getch();
207                 ch2 = fgetc (stdin);
208
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.
216
217    This problem occurs when using Text_IO.Get_Line after Text_IO.Get_Immediate
218    for example.
219
220    Calling FlushConsoleInputBuffer just after getch() fix the bug under
221    95/98. */
222
223 #ifdef RTX
224
225 static void winflush_nt (void);
226
227 /* winflush_function will do nothing since we only have problems with Windows
228    95/98 which are not supported by RTX. */
229
230 static void (*winflush_function) (void) = winflush_nt;
231
232 static void
233 winflush_nt (void)
234 {
235   /* Does nothing as there is no problem under NT.  */
236 }
237
238 #else /* !RTX */
239
240 static void winflush_init (void);
241
242 static void winflush_95 (void);
243
244 static void winflush_nt (void);
245
246 int __gnat_is_windows_xp (void);
247
248 /* winflusfunction is set first to the winflushinit function which will check
249    the OS version 95/98 or NT/2000 */
250
251 static void (*winflush_function) (void) = winflush_init;
252
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. */
255
256 static void
257 winflush_init (void)
258 {
259   DWORD dwVersion = GetVersion();
260
261   if (dwVersion < 0x80000000)                /* Windows NT/2000 */
262     winflush_function = winflush_nt;
263   else                                       /* Windows 95/98   */
264     winflush_function = winflush_95;
265
266   (*winflush_function)();      /* Perform the 'flush' */
267
268 }
269
270 static void
271 winflush_95 (void)
272 {
273   FlushConsoleInputBuffer (GetStdHandle (STD_INPUT_HANDLE));
274 }
275
276 static void
277 winflush_nt (void)
278 {
279   /* Does nothing as there is no problem under NT.  */
280 }
281
282 int
283 __gnat_is_windows_xp (void)
284 {
285   static int is_win_xp=0, is_win_xp_checked=0;
286
287   if (!is_win_xp_checked)
288     {
289       OSVERSIONINFO version;
290
291       is_win_xp_checked = 1;
292
293       memset (&version, 0, sizeof (version));
294       version.dwOSVersionInfoSize = sizeof (version);
295
296       is_win_xp = GetVersionEx (&version)
297         && version.dwPlatformId == VER_PLATFORM_WIN32_NT
298         && (version.dwMajorVersion > 5
299             || (version.dwMajorVersion == 5 && version.dwMinorVersion >= 1));
300     }
301   return is_win_xp;
302 }
303
304 #endif /* !RTX */
305
306 /* Get the bounds of the stack.  The stack pointer is supposed to be
307    initialized to BASE when a thread is created and the stack can be extended
308    to LIMIT before reaching a guard page.
309    Note: for the main thread, the system automatically extend the stack, so
310    LIMIT is only the current limit.  */
311
312 void
313 __gnat_get_stack_bounds (void **base, void **limit)
314 {
315   NT_TIB *tib;
316
317   /* We know that the first field of the TEB is the TIB.  */
318   tib = (NT_TIB *)NtCurrentTeb ();
319
320   *base = tib->StackBase;
321   *limit = tib->StackLimit;
322 }
323
324 #endif /* !__MINGW32__ */
325
326 #else
327
328 static const char *mode_read_text = "r";
329 static const char *mode_write_text = "w";
330 static const char *mode_append_text = "a";
331 static const char *mode_read_binary = "r";
332 static const char *mode_write_binary = "w";
333 static const char *mode_append_binary = "a";
334 static const char *mode_read_text_plus = "r+";
335 static const char *mode_write_text_plus = "w+";
336 static const char *mode_append_text_plus = "a+";
337 static const char *mode_read_binary_plus = "r+";
338 static const char *mode_write_binary_plus = "w+";
339 static const char *mode_append_binary_plus = "a+";
340 const char __gnat_text_translation_required = 0;
341
342 /* These functions do nothing in non-DOS systems. */
343
344 void
345 __gnat_set_binary_mode (int handle ATTRIBUTE_UNUSED)
346 {
347 }
348
349 void
350 __gnat_set_text_mode (int handle ATTRIBUTE_UNUSED)
351 {
352 }
353 char *
354 __gnat_ttyname (int filedes)
355 {
356 #if defined (__vxworks) || defined (__nucleus)
357   return "";
358 #else
359   extern char *ttyname (int);
360
361   return ttyname (filedes);
362 #endif /* defined (__vxworks) || defined (__nucleus) */
363 }
364 #endif
365 \f
366 #if defined (linux) || defined (sun) || defined (sgi) \
367   || (defined (__osf__) && ! defined (__alpha_vxworks)) || defined (WINNT) \
368   || defined (__MACHTEN__) || defined (__hpux__) || defined (_AIX) \
369   || (defined (__svr4__) && defined (i386)) || defined (__Lynx__) \
370   || defined (__CYGWIN__) || defined (__FreeBSD__) || defined (__OpenBSD__) \
371   || defined (__GLIBC__) || defined (__APPLE__)
372
373 #ifdef __MINGW32__
374 #if OLD_MINGW
375 #include <termios.h>
376 #else
377 #include <conio.h>  /* for getch(), kbhit() */
378 #endif
379 #else
380 #include <termios.h>
381 #endif
382
383 #else
384 #if defined (VMS)
385 extern char *decc$ga_stdscr;
386 static int initted = 0;
387 #endif
388 #endif
389
390 /* Implements the common processing for getc_immediate and
391    getc_immediate_nowait. */
392
393 extern void getc_immediate (FILE *, int *, int *);
394 extern void getc_immediate_nowait (FILE *, int *, int *, int *);
395 extern void getc_immediate_common (FILE *, int *, int *, int *, int);
396
397 /* Called by Get_Immediate (Foo); */
398
399 void
400 getc_immediate (FILE *stream, int *ch, int *end_of_file)
401 {
402   int avail;
403
404   getc_immediate_common (stream, ch, end_of_file, &avail, 1);
405 }
406
407 /* Called by Get_Immediate (Foo, Available); */
408
409 void
410 getc_immediate_nowait (FILE *stream, int *ch, int *end_of_file, int *avail)
411 {
412   getc_immediate_common (stream, ch, end_of_file, avail, 0);
413 }
414
415 /* Called by getc_immediate () and getc_immediate_nowait () */
416
417 void
418 getc_immediate_common (FILE *stream,
419                        int *ch,
420                        int *end_of_file,
421                        int *avail,
422                        int waiting)
423 {
424 #if defined (linux) || defined (sun) || defined (sgi) \
425     || (defined (__osf__) && ! defined (__alpha_vxworks)) \
426     || defined (__CYGWIN32__) || defined (__MACHTEN__) || defined (__hpux__) \
427     || defined (_AIX) || (defined (__svr4__) && defined (i386)) \
428     || defined (__Lynx__) || defined (__FreeBSD__) || defined (__OpenBSD__) \
429     || defined (__GLIBC__) || defined (__APPLE__)
430   char c;
431   int nread;
432   int good_one = 0;
433   int eof_ch = 4; /* Ctrl-D */
434   int fd = fileno (stream);
435   struct termios otermios_rec, termios_rec;
436
437   if (isatty (fd))
438     {
439       tcgetattr (fd, &termios_rec);
440       memcpy (&otermios_rec, &termios_rec, sizeof (struct termios));
441
442       /* Set RAW mode, with no echo */
443       termios_rec.c_lflag = termios_rec.c_lflag & ~ICANON & ~ECHO;
444
445 #if defined(linux) || defined (sun) || defined (sgi) \
446     || defined (__osf__) || defined (__MACHTEN__) || defined (__hpux__) \
447     || defined (_AIX) || (defined (__svr4__) && defined (i386)) \
448     || defined (__Lynx__) || defined (__FreeBSD__) || defined (__OpenBSD__) \
449     || defined (__GLIBC__) || defined (__APPLE__)
450       eof_ch = termios_rec.c_cc[VEOF];
451
452       /* If waiting (i.e. Get_Immediate (Char)), set MIN = 1 and wait for
453          a character forever. This doesn't seem to effect Ctrl-Z or
454          Ctrl-C processing.
455          If not waiting (i.e. Get_Immediate (Char, Available)),
456          don't wait for anything but timeout immediately. */
457       termios_rec.c_cc[VMIN] = waiting;
458       termios_rec.c_cc[VTIME] = 0;
459 #endif
460       tcsetattr (fd, TCSANOW, &termios_rec);
461
462       while (! good_one)
463         {
464           /* Read is used here instead of fread, because fread doesn't
465              work on Solaris5 and Sunos4 in this situation.  Maybe because we
466              are mixing calls that use file descriptors and streams. */
467           nread = read (fd, &c, 1);
468           if (nread > 0)
469             {
470               /* On Unix terminals, Ctrl-D (EOT) is an End of File. */
471               if (c == eof_ch)
472                 {
473                   *avail = 0;
474                   *end_of_file = 1;
475                   good_one = 1;
476                 }
477
478               /* Everything else is ok */
479               else if (c != eof_ch)
480                 {
481                   *avail = 1;
482                   *end_of_file = 0;
483                   good_one = 1;
484                 }
485             }
486
487           else if (! waiting)
488             {
489               *avail = 0;
490               *end_of_file = 0;
491               good_one = 1;
492             }
493           else
494             good_one = 0;
495         }
496
497       tcsetattr (fd, TCSANOW, &otermios_rec);
498       *ch = c;
499     }
500
501   else
502 #elif defined (VMS)
503   int fd = fileno (stream);
504
505   if (isatty (fd))
506     {
507       if (initted == 0)
508         {
509           decc$bsd_initscr ();
510           initted = 1;
511         }
512
513       decc$bsd_cbreak ();
514       *ch = decc$bsd_wgetch (decc$ga_stdscr);
515
516       if (*ch == 4)
517         *end_of_file = 1;
518       else
519         *end_of_file = 0;
520
521       *avail = 1;
522       decc$bsd_nocbreak ();
523     }
524   else
525 #elif defined (__MINGW32__)
526   int fd = fileno (stream);
527   int char_waiting;
528   int eot_ch = 4; /* Ctrl-D */
529
530   if (isatty (fd))
531     {
532       if (waiting)
533         {
534           *ch = getch ();
535           (*winflush_function) ();
536
537           if (*ch == eot_ch)
538             *end_of_file = 1;
539           else
540             *end_of_file = 0;
541
542           *avail = 1;
543         }
544       else /* ! waiting */
545         {
546           char_waiting = kbhit();
547
548           if (char_waiting == 1)
549             {
550               *avail = 1;
551               *ch = getch ();
552               (*winflush_function) ();
553
554               if (*ch == eot_ch)
555                 *end_of_file = 1;
556               else
557                 *end_of_file = 0;
558             }
559           else
560             {
561               *avail = 0;
562               *end_of_file = 0;
563             }
564         }
565     }
566   else
567 #elif defined (__vxworks)
568   /* Bit masks of file descriptors to read from.  */
569   struct fd_set readFds;
570   /* Timeout before select returns if nothing can be read.  */
571   struct timeval timeOut;
572   char c;
573   int fd = fileno (stream);
574   int nread;
575   int option;
576   int readable;
577   int status;
578   int width;
579
580   if (isatty (fd))
581     {
582       /* If we do not want to wait, we have to set up fd in RAW mode. This
583          should be done outside this function as setting fd in RAW mode under
584          vxWorks flushes the buffer of fd. If the RAW mode was set here, the
585          buffer would be empty and we would always return that no character
586          is available */
587       if (! waiting)
588         {
589           /* Initialization of timeOut for its use with select.  */
590           timeOut.tv_sec  = 0;
591           timeOut.tv_usec = 0;
592
593           /* Initialization of readFds for its use with select;
594              FD is the only file descriptor to be monitored */
595           FD_ZERO (&readFds);
596           FD_SET (fd, &readFds);
597           width = 2;
598
599           /* We do all this processing to emulate a non blocking read.  */
600           readable = select (width, &readFds, NULL, NULL, &timeOut);
601           if (readable == ERROR)
602             *avail = -1, *end_of_file = -1;
603           /* No character available in input.  */
604           else if (readable == 0)
605             *avail = 0, *end_of_file = 0;
606           else
607             {
608               nread = read (fd, &c, 1);
609               if (nread > 0)
610                 *avail = 1, *end_of_file = 0;
611               /* End Of File. */
612               else if (nread == 0)
613                 *avail = 0, *end_of_file = 1;
614               /* Error.  */
615               else
616                 *avail = -1, *end_of_file = -1;
617             }
618         }
619
620       /* We have to wait until we get a character */
621       else
622         {
623           *avail = -1;
624           *end_of_file = -1;
625
626           /* Save the current mode of FD.  */
627           option = ioctl (fd, FIOGETOPTIONS, 0);
628
629           /* Set FD in RAW mode.  */
630           status = ioctl (fd, FIOSETOPTIONS, OPT_RAW);
631           if (status != -1)
632             {
633               nread = read (fd, &c, 1);
634               if (nread > 0)
635                 *avail = 1, *end_of_file = 0;
636               /* End of file.  */
637               else if (nread == 0)
638                 *avail = 0, *end_of_file = 1;
639               /* Else there is an ERROR.  */
640             }
641
642           /* Revert FD to its previous mode. */
643           status = ioctl (fd, FIOSETOPTIONS, option);
644         }
645
646       *ch = c;
647     }
648   else
649 #endif
650     {
651       /* If we're not on a terminal, then we don't need any fancy processing.
652          Also this is the only thing that's left if we're not on one of the
653          supported systems; which means that for non supported systems,
654          get_immediate may wait for a carriage return on terminals. */
655       *ch = fgetc (stream);
656       if (feof (stream))
657         {
658           *end_of_file = 1;
659           *avail = 0;
660         }
661       else
662         {
663           *end_of_file = 0;
664           *avail = 1;
665         }
666     }
667 }
668
669 /* The following definitions are provided in NT to support Windows based
670    Ada programs.  */
671
672 #ifdef WINNT
673 #include <windows.h>
674
675 /* Provide functions to echo the values passed to WinMain (windows bindings
676    will want to import these).  We use the same names as the routines used
677    by AdaMagic for compatibility.  */
678
679 char *rts_get_hInstance (void);
680 char *rts_get_hPrevInstance (void);
681 char *rts_get_lpCommandLine (void);
682 int   rts_get_nShowCmd (void);
683
684 char *
685 rts_get_hInstance (void)
686 {
687   return (char *)GetModuleHandleA (0);
688 }
689
690 char *
691 rts_get_hPrevInstance (void)
692 {
693   return 0;
694 }
695
696 char *
697 rts_get_lpCommandLine (void)
698 {
699   return GetCommandLineA ();
700 }
701
702 int
703 rts_get_nShowCmd (void)
704 {
705   return 1;
706 }
707
708 #endif /* WINNT */
709 #ifdef VMS
710
711 /* This gets around a problem with using the old threads library on VMS 7.0. */
712
713 extern long get_gmtoff (void);
714
715 long
716 get_gmtoff (void)
717 {
718   time_t t;
719   struct tm *ts;
720
721   t = time ((time_t) 0);
722   ts = localtime (&t);
723   return ts->tm_gmtoff;
724 }
725 #endif
726
727 /* This value is returned as the time zone offset when a valid value
728    cannot be determined. It is simply a bizarre value that will never
729    occur. It is 3 days plus 73 seconds (offset is in seconds). */
730
731 long __gnat_invalid_tzoff = 259273;
732
733 /* Definition of __gnat_localtime_r used by a-calend.adb */
734
735 #if defined (__MINGW32__)
736
737 #ifdef CERT
738
739 /* For the Cert run times on native Windows we use dummy functions
740    for locking and unlocking tasks since we do not support multiple
741    threads on this configuration (Cert run time on native Windows). */
742
743 void dummy (void) {}
744
745 void (*Lock_Task) ()   = &dummy;
746 void (*Unlock_Task) () = &dummy;
747
748 #else
749
750 #define Lock_Task system__soft_links__lock_task
751 extern void (*Lock_Task) (void);
752
753 #define Unlock_Task system__soft_links__unlock_task
754 extern void (*Unlock_Task) (void);
755
756 #endif
757
758 /* Reentrant localtime for Windows. */
759
760 extern void
761 __gnat_localtime_tzoff (const time_t *, long *);
762
763 static const unsigned long long w32_epoch_offset = 11644473600ULL;
764 void
765 __gnat_localtime_tzoff (const time_t *timer, long *off)
766 {
767   union
768   {
769     FILETIME ft_time;
770     unsigned long long ull_time;
771   } utc_time, local_time;
772
773   SYSTEMTIME utc_sys_time, local_sys_time;
774   TIME_ZONE_INFORMATION tzi;
775
776   BOOL  status = 1;
777   DWORD tzi_status;
778
779   (*Lock_Task) ();
780
781 #ifdef RTX
782
783   tzi_status = GetTimeZoneInformation (&tzi);
784   *off = tzi.Bias;
785   if (tzi_status == TIME_ZONE_ID_STANDARD)
786      /* The system is operating in the range covered by the StandardDate
787         member. */
788      *off = *off + tzi.StandardBias;
789   else if (tzi_status == TIME_ZONE_ID_DAYLIGHT)
790      /* The system is operating in the range covered by the DaylightDate
791         member. */
792      *off = *off + tzi.DaylightBias;
793   *off = *off * -60;
794
795 #else
796
797   /* First convert unix time_t structure to windows FILETIME format.  */
798   utc_time.ull_time = ((unsigned long long) *timer + w32_epoch_offset)
799                       * 10000000ULL;
800
801   tzi_status = GetTimeZoneInformation (&tzi);
802
803   /* If GetTimeZoneInformation does not return a value between 0 and 2 then
804      it means that we were not able to retrieve timezone informations.
805      Note that we cannot use here FileTimeToLocalFileTime as Windows will use
806      in always in this case the current timezone setting. As suggested on
807      MSDN we use the following three system calls to get the right information.
808      Note also that starting with Windows Vista new functions are provided to
809      get timezone settings that depend on the year. We cannot use them as we
810      still support Windows XP and Windows 2003.  */
811   status = (tzi_status >= 0 && tzi_status <= 2)
812      && FileTimeToSystemTime (&utc_time.ft_time, &utc_sys_time)
813      && SystemTimeToTzSpecificLocalTime (&tzi, &utc_sys_time, &local_sys_time)
814      && SystemTimeToFileTime (&local_sys_time, &local_time.ft_time);
815
816   if (!status)
817      /* An error occurs so return invalid_tzoff.  */
818      *off = __gnat_invalid_tzoff;
819   else
820      if (local_time.ull_time > utc_time.ull_time)
821         *off = (long) ((local_time.ull_time - utc_time.ull_time) / 10000000ULL);
822      else
823         *off = - (long) ((utc_time.ull_time - local_time.ull_time) / 10000000ULL);
824
825 #endif
826
827   (*Unlock_Task) ();
828 }
829
830 #else
831
832 /* On Lynx, all time values are treated in GMT */
833
834 #if defined (__Lynx__)
835
836 /* As of LynxOS 3.1.0a patch level 040, LynuxWorks changes the
837    prototype to the C library function localtime_r from the POSIX.4
838    Draft 9 to the POSIX 1.c version. Before this change the following
839    spec is required. Only use when ___THREADS_POSIX4ad4__ is defined,
840    the Lynx convention when building against the legacy API. */
841
842 extern void
843 __gnat_localtime_tzoff (const time_t *, long *);
844
845 void
846 __gnat_localtime_tzoff (const time_t *timer, long *off)
847 {
848   *off = 0;
849 }
850
851 #else
852
853 /* VMS does not need __gnat_localtime_tzoff */
854
855 #if defined (VMS)
856
857 /* Other targets except Lynx, VMS and Windows provide a standard localtime_r */
858
859 #else
860
861 #define Lock_Task system__soft_links__lock_task
862 extern void (*Lock_Task) (void);
863
864 #define Unlock_Task system__soft_links__unlock_task
865 extern void (*Unlock_Task) (void);
866
867 extern void
868 __gnat_localtime_tzoff (const time_t *, long *);
869
870 void
871 __gnat_localtime_tzoff (const time_t *timer, long *off)
872 {
873   struct tm tp;
874
875 /* AIX, HPUX, SGI Irix, Sun Solaris */
876 #if defined (_AIX) || defined (__hpux__) || defined (sgi) || defined (sun)
877 {
878   (*Lock_Task) ();
879
880   localtime_r (timer, &tp);
881   *off = (long) -timezone;
882
883   (*Unlock_Task) ();
884
885   /* Correct the offset if Daylight Saving Time is in effect */
886
887   if (tp.tm_isdst > 0)
888     *off = *off + 3600;
889 }
890
891 /* VxWorks */
892 #elif defined (__vxworks)
893 #include <stdlib.h>
894 {
895   (*Lock_Task) ();
896
897   localtime_r (timer, &tp);
898
899   /* Try to read the environment variable TIMEZONE. The variable may not have
900      been initialize, in that case return an offset of zero (0) for UTC. */
901
902   char *tz_str = getenv ("TIMEZONE");
903
904   if ((tz_str == NULL) || (*tz_str == '\0'))
905     *off = 0;
906   else
907   {
908     char *tz_start, *tz_end;
909
910     /* The format of the data contained in TIMEZONE is N::U:S:E where N is the
911        name of the time zone, U are the minutes difference from UTC, S is the
912        start of DST in mmddhh and E is the end of DST in mmddhh. Extracting
913        the value of U involves setting two pointers, one at the beginning and
914        one at the end of the value. The end pointer is then set to null in
915        order to delimit a string slice for atol to process. */
916
917     tz_start = index (tz_str, ':') + 2;
918     tz_end = index (tz_start, ':');
919     tz_end = '\0';
920
921     /* The Ada layer expects an offset in seconds. Note that we must reverse
922        the sign of the result since west is positive and east is negative on
923        VxWorks targets. */
924
925     *off = -atol (tz_start) * 60;
926
927     /* Correct the offset if Daylight Saving Time is in effect */
928
929     if (tp.tm_isdst > 0)
930       *off = *off + 3600;
931   }
932
933   (*Unlock_Task) ();
934 }
935
936 /* Darwin, Free BSD, Linux, Tru64, where component tm_gmtoff is present in
937    struct tm */
938
939 #elif defined (__APPLE__) || defined (__FreeBSD__) || defined (linux) ||\
940      (defined (__alpha__) && defined (__osf__)) || defined (__GLIBC__)
941 {
942   localtime_r (timer, &tp);
943   *off = tp.tm_gmtoff;
944 }
945
946 /* Default: treat all time values in GMT */
947
948 #else
949   *off = 0;
950
951 #endif
952 }
953
954 #endif
955 #endif
956 #endif
957
958 #ifdef __vxworks
959
960 #include <taskLib.h>
961
962 /* __gnat_get_task_options is used by s-taprop.adb only for VxWorks. This
963    function returns the options to be set when creating a new task. It fetches
964    the options assigned to the current task (parent), so offering some user
965    level control over the options for a task hierarchy. It forces VX_FP_TASK
966    because it is almost always required. On processors with the SPE
967    category, VX_SPE_TASK is needed to enable the SPE. */
968 extern int __gnat_get_task_options (void);
969
970 int
971 __gnat_get_task_options (void)
972 {
973   int options;
974
975   /* Get the options for the task creator */
976   taskOptionsGet (taskIdSelf (), &options);
977
978   /* Force VX_FP_TASK because it is almost always required */
979   options |= VX_FP_TASK;
980 #if defined (__SPE__) && (! defined (__VXWORKSMILS__))
981   options |= VX_SPE_TASK;
982 #endif
983
984   /* Mask those bits that are not under user control */
985 #ifdef VX_USR_TASK_OPTIONS
986   return options & VX_USR_TASK_OPTIONS;
987 #else
988   return options;
989 #endif
990 }
991
992 #endif
993
994 int
995 __gnat_is_file_not_found_error (int errno_val) {
996    switch (errno_val) {
997       case ENOENT:
998 #ifdef __vxworks
999       /* In the case of VxWorks, we also have to take into account various
1000        * filesystem-specific variants of this error.
1001        */
1002 #if ! defined (VTHREADS)
1003       case S_dosFsLib_FILE_NOT_FOUND:
1004 #endif
1005 #if ! defined (__RTP__) && (! defined (VTHREADS) || defined (__VXWORKSMILS__))
1006       case S_nfsLib_NFSERR_NOENT:
1007 #endif
1008 #endif
1009          return 1;
1010
1011       default:
1012          return 0;
1013    }
1014 }