OSDN Git Service

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