OSDN Git Service

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