OSDN Git Service

2010-06-22 Ed Schonberg <schonberg@adacore.com>
[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
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
305
306 #endif
307
308 #else
309
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;
323
324 /* These functions do nothing in non-DOS systems. */
325
326 void
327 __gnat_set_binary_mode (int handle ATTRIBUTE_UNUSED)
328 {
329 }
330
331 void
332 __gnat_set_text_mode (int handle ATTRIBUTE_UNUSED)
333 {
334 }
335 char *
336 __gnat_ttyname (int filedes)
337 {
338 #if defined (__vxworks) || defined (__nucleus)
339   return "";
340 #else
341   extern char *ttyname (int);
342
343   return ttyname (filedes);
344 #endif /* defined (__vxworks) || defined (__nucleus) */
345 }
346 #endif
347 \f
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__)
354
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
364
365 #else
366 #if defined (VMS)
367 extern char *decc$ga_stdscr;
368 static int initted = 0;
369 #endif
370 #endif
371
372 /* Implements the common processing for getc_immediate and
373    getc_immediate_nowait. */
374
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);
378
379 /* Called by Get_Immediate (Foo); */
380
381 void
382 getc_immediate (FILE *stream, int *ch, int *end_of_file)
383 {
384   int avail;
385
386   getc_immediate_common (stream, ch, end_of_file, &avail, 1);
387 }
388
389 /* Called by Get_Immediate (Foo, Available); */
390
391 void
392 getc_immediate_nowait (FILE *stream, int *ch, int *end_of_file, int *avail)
393 {
394   getc_immediate_common (stream, ch, end_of_file, avail, 0);
395 }
396
397 /* Called by getc_immediate () and getc_immediate_nowait () */
398
399 void
400 getc_immediate_common (FILE *stream,
401                        int *ch,
402                        int *end_of_file,
403                        int *avail,
404                        int waiting)
405 {
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;
418
419   if (isatty (fd))
420     {
421       tcgetattr (fd, &termios_rec);
422       memcpy (&otermios_rec, &termios_rec, sizeof (struct termios));
423
424       /* Set RAW mode, with no echo */
425       termios_rec.c_lflag = termios_rec.c_lflag & ~ICANON & ~ECHO;
426
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];
433
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);
443
444       while (! good_one)
445         {
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)
451             {
452               /* On Unix terminals, Ctrl-D (EOT) is an End of File. */
453               if (c == eof_ch)
454                 {
455                   *avail = 0;
456                   *end_of_file = 1;
457                   good_one = 1;
458                 }
459
460               /* Everything else is ok */
461               else if (c != eof_ch)
462                 {
463                   *avail = 1;
464                   *end_of_file = 0;
465                   good_one = 1;
466                 }
467             }
468
469           else if (! waiting)
470             {
471               *avail = 0;
472               *end_of_file = 0;
473               good_one = 1;
474             }
475           else
476             good_one = 0;
477         }
478
479       tcsetattr (fd, TCSANOW, &otermios_rec);
480       *ch = c;
481     }
482
483   else
484 #elif defined (VMS)
485   int fd = fileno (stream);
486
487   if (isatty (fd))
488     {
489       if (initted == 0)
490         {
491           decc$bsd_initscr ();
492           initted = 1;
493         }
494
495       decc$bsd_cbreak ();
496       *ch = decc$bsd_wgetch (decc$ga_stdscr);
497
498       if (*ch == 4)
499         *end_of_file = 1;
500       else
501         *end_of_file = 0;
502
503       *avail = 1;
504       decc$bsd_nocbreak ();
505     }
506   else
507 #elif defined (__MINGW32__)
508   int fd = fileno (stream);
509   int char_waiting;
510   int eot_ch = 4; /* Ctrl-D */
511
512   if (isatty (fd))
513     {
514       if (waiting)
515         {
516           *ch = getch ();
517           (*winflush_function) ();
518
519           if (*ch == eot_ch)
520             *end_of_file = 1;
521           else
522             *end_of_file = 0;
523
524           *avail = 1;
525         }
526       else /* ! waiting */
527         {
528           char_waiting = kbhit();
529
530           if (char_waiting == 1)
531             {
532               *avail = 1;
533               *ch = getch ();
534               (*winflush_function) ();
535
536               if (*ch == eot_ch)
537                 *end_of_file = 1;
538               else
539                 *end_of_file = 0;
540             }
541           else
542             {
543               *avail = 0;
544               *end_of_file = 0;
545             }
546         }
547     }
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;
561
562   if (isatty (fd))
563     {
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)
570         {
571           /* Initialization of timeOut for its use with select.  */
572           timeOut.tv_sec  = 0;
573           timeOut.tv_usec = 0;
574
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;
580
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
589             {
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;
599             }
600         }
601
602       /* We have to wait until we get a character */
603       else
604         {
605           *avail = -1;
606           *end_of_file = -1;
607
608           /* Save the current mode of FD.  */
609           option = ioctl (fd, FIOGETOPTIONS, 0);
610
611           /* Set FD in RAW mode.  */
612           status = ioctl (fd, FIOSETOPTIONS, OPT_RAW);
613           if (status != -1)
614             {
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.  */
622             }
623
624           /* Revert FD to its previous mode. */
625           status = ioctl (fd, FIOSETOPTIONS, option);
626         }
627
628       *ch = c;
629     }
630   else
631 #endif
632     {
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))
639         {
640           *end_of_file = 1;
641           *avail = 0;
642         }
643       else
644         {
645           *end_of_file = 0;
646           *avail = 1;
647         }
648     }
649 }
650
651 /* The following definitions are provided in NT to support Windows based
652    Ada programs.  */
653
654 #ifdef WINNT
655 #include <windows.h>
656
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.  */
660
661 char *rts_get_hInstance (void);
662 char *rts_get_hPrevInstance (void);
663 char *rts_get_lpCommandLine (void);
664 int   rts_get_nShowCmd (void);
665
666 char *
667 rts_get_hInstance (void)
668 {
669   return (char *)GetModuleHandleA (0);
670 }
671
672 char *
673 rts_get_hPrevInstance (void)
674 {
675   return 0;
676 }
677
678 char *
679 rts_get_lpCommandLine (void)
680 {
681   return GetCommandLineA ();
682 }
683
684 int
685 rts_get_nShowCmd (void)
686 {
687   return 1;
688 }
689
690 #endif /* WINNT */
691 #ifdef VMS
692
693 /* This gets around a problem with using the old threads library on VMS 7.0. */
694
695 extern long get_gmtoff (void);
696
697 long
698 get_gmtoff (void)
699 {
700   time_t t;
701   struct tm *ts;
702
703   t = time ((time_t) 0);
704   ts = localtime (&t);
705   return ts->tm_gmtoff;
706 }
707 #endif
708
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). */
712
713 long __gnat_invalid_tzoff = 259273;
714
715 /* Definition of __gnat_localtime_r used by a-calend.adb */
716
717 #if defined (__MINGW32__)
718
719 #ifdef CERT
720
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). */
724
725 void dummy (void) {}
726
727 void (*Lock_Task) ()   = &dummy;
728 void (*Unlock_Task) () = &dummy;
729
730 #else
731
732 #define Lock_Task system__soft_links__lock_task
733 extern void (*Lock_Task) (void);
734
735 #define Unlock_Task system__soft_links__unlock_task
736 extern void (*Unlock_Task) (void);
737
738 #endif
739
740 /* Reentrant localtime for Windows. */
741
742 extern void
743 __gnat_localtime_tzoff (const time_t *, long *);
744
745 static const unsigned long long w32_epoch_offset = 11644473600ULL;
746 void
747 __gnat_localtime_tzoff (const time_t *timer, long *off)
748 {
749   union
750   {
751     FILETIME ft_time;
752     unsigned long long ull_time;
753   } utc_time, local_time;
754
755   SYSTEMTIME utc_sys_time, local_sys_time;
756   TIME_ZONE_INFORMATION tzi;
757
758   BOOL  status = 1;
759   DWORD tzi_status;
760
761   (*Lock_Task) ();
762
763 #ifdef RTX
764
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;
776
777 #else
778
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;
782
783   tzi_status = GetTimeZoneInformation (&tzi);
784
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);
797
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);
806
807 #endif
808
809   (*Unlock_Task) ();
810 }
811
812 #else
813
814 /* On Lynx, all time values are treated in GMT */
815
816 #if defined (__Lynx__)
817
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. */
823
824 extern void
825 __gnat_localtime_tzoff (const time_t *, long *);
826
827 void
828 __gnat_localtime_tzoff (const time_t *timer, long *off)
829 {
830   *off = 0;
831 }
832
833 #else
834
835 /* VMS does not need __gnat_locatime_tzoff */
836
837 #if defined (VMS)
838
839 /* Other targets except Lynx, VMS and Windows provide a standard locatime_r */
840
841 #else
842
843 #define Lock_Task system__soft_links__lock_task
844 extern void (*Lock_Task) (void);
845
846 #define Unlock_Task system__soft_links__unlock_task
847 extern void (*Unlock_Task) (void);
848
849 extern void
850 __gnat_localtime_tzoff (const time_t *, long *);
851
852 void
853 __gnat_localtime_tzoff (const time_t *timer, long *off)
854 {
855   struct tm tp;
856
857 /* AIX, HPUX, SGI Irix, Sun Solaris */
858 #if defined (_AIX) || defined (__hpux__) || defined (sgi) || defined (sun)
859 {
860   (*Lock_Task) ();
861
862   localtime_r (timer, &tp);
863   *off = (long) -timezone;
864
865   (*Unlock_Task) ();
866
867   /* Correct the offset if Daylight Saving Time is in effect */
868
869   if (tp.tm_isdst > 0)
870     *off = *off + 3600;
871 }
872
873 /* VxWorks */
874 #elif defined (__vxworks)
875 #include <stdlib.h>
876 {
877   (*Lock_Task) ();
878
879   localtime_r (timer, &tp);
880
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. */
883
884   char *tz_str = getenv ("TIMEZONE");
885
886   if ((tz_str == NULL) || (*tz_str == '\0'))
887     *off = 0;
888   else
889   {
890     char *tz_start, *tz_end;
891
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. */
898
899     tz_start = index (tz_str, ':') + 2;
900     tz_end = index (tz_start, ':');
901     tz_end = '\0';
902
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. */
906
907     *off = -atol (tz_start) * 60;
908
909     /* Correct the offset if Daylight Saving Time is in effect */
910
911     if (tp.tm_isdst > 0)
912       *off = *off + 3600;
913   }
914
915   (*Unlock_Task) ();
916 }
917
918 /* Darwin, Free BSD, Linux, Tru64, where component tm_gmtoff is present in
919    struct tm */
920
921 #elif defined (__APPLE__) || defined (__FreeBSD__) || defined (linux) ||\
922      (defined (__alpha__) && defined (__osf__)) || defined (__GLIBC__)
923 {
924   localtime_r (timer, &tp);
925   *off = tp.tm_gmtoff;
926 }
927
928 /* Default: treat all time values in GMT */
929
930 #else
931   *off = 0;
932
933 #endif
934 }
935
936 #endif
937 #endif
938 #endif
939
940 #ifdef __vxworks
941
942 #include <taskLib.h>
943
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);
951
952 int
953 __gnat_get_task_options (void)
954 {
955   int options;
956
957   /* Get the options for the task creator */
958   taskOptionsGet (taskIdSelf (), &options);
959
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
965
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
972 }
973
974 #endif
975
976 int
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.
983        */
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;
992
993       default:
994          return 0;
995    }
996 }