annotate gcc/ada/sysdep.c @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children 84e7813d76e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /****************************************************************************
kono
parents:
diff changeset
2 * *
kono
parents:
diff changeset
3 * GNAT COMPILER COMPONENTS *
kono
parents:
diff changeset
4 * *
kono
parents:
diff changeset
5 * S Y S D E P *
kono
parents:
diff changeset
6 * *
kono
parents:
diff changeset
7 * C Implementation File *
kono
parents:
diff changeset
8 * *
kono
parents:
diff changeset
9 * Copyright (C) 1992-2017, Free Software Foundation, Inc. *
kono
parents:
diff changeset
10 * *
kono
parents:
diff changeset
11 * GNAT is free software; you can redistribute it and/or modify it under *
kono
parents:
diff changeset
12 * terms of the GNU General Public License as published by the Free Soft- *
kono
parents:
diff changeset
13 * ware Foundation; either version 3, or (at your option) any later ver- *
kono
parents:
diff changeset
14 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
kono
parents:
diff changeset
15 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
kono
parents:
diff changeset
16 * or FITNESS FOR A PARTICULAR PURPOSE. *
kono
parents:
diff changeset
17 * *
kono
parents:
diff changeset
18 * As a special exception under Section 7 of GPL version 3, you are granted *
kono
parents:
diff changeset
19 * additional permissions described in the GCC Runtime Library Exception, *
kono
parents:
diff changeset
20 * version 3.1, as published by the Free Software Foundation. *
kono
parents:
diff changeset
21 * *
kono
parents:
diff changeset
22 * You should have received a copy of the GNU General Public License and *
kono
parents:
diff changeset
23 * a copy of the GCC Runtime Library Exception along with this program; *
kono
parents:
diff changeset
24 * see the files COPYING3 and COPYING.RUNTIME respectively. If not, see *
kono
parents:
diff changeset
25 * <http://www.gnu.org/licenses/>. *
kono
parents:
diff changeset
26 * *
kono
parents:
diff changeset
27 * GNAT was originally developed by the GNAT team at New York University. *
kono
parents:
diff changeset
28 * Extensive contributions were provided by Ada Core Technologies Inc. *
kono
parents:
diff changeset
29 * *
kono
parents:
diff changeset
30 ****************************************************************************/
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 /* This file contains system dependent symbols that are referenced in the
kono
parents:
diff changeset
33 GNAT Run Time Library */
kono
parents:
diff changeset
34
kono
parents:
diff changeset
35 #ifdef __vxworks
kono
parents:
diff changeset
36 #include "ioLib.h"
kono
parents:
diff changeset
37 #if ! defined (VTHREADS)
kono
parents:
diff changeset
38 #include "dosFsLib.h"
kono
parents:
diff changeset
39 #endif
kono
parents:
diff changeset
40 #if ! defined (__RTP__) && (! defined (VTHREADS) || defined (__VXWORKSMILS__))
kono
parents:
diff changeset
41 # include "nfsLib.h"
kono
parents:
diff changeset
42 #endif
kono
parents:
diff changeset
43 #include "selectLib.h"
kono
parents:
diff changeset
44 #include "vxWorks.h"
kono
parents:
diff changeset
45 #include "version.h"
kono
parents:
diff changeset
46 #if defined (__RTP__)
kono
parents:
diff changeset
47 # include "vwModNum.h"
kono
parents:
diff changeset
48 #endif /* __RTP__ */
kono
parents:
diff changeset
49 #endif
kono
parents:
diff changeset
50
kono
parents:
diff changeset
51 #ifdef __ANDROID__
kono
parents:
diff changeset
52 #undef __linux__
kono
parents:
diff changeset
53 #endif
kono
parents:
diff changeset
54
kono
parents:
diff changeset
55 #ifdef IN_RTS
kono
parents:
diff changeset
56 #define POSIX
kono
parents:
diff changeset
57 #include "tconfig.h"
kono
parents:
diff changeset
58 #include "tsystem.h"
kono
parents:
diff changeset
59 #include <fcntl.h>
kono
parents:
diff changeset
60 #include <sys/stat.h>
kono
parents:
diff changeset
61 #else
kono
parents:
diff changeset
62 #include "config.h"
kono
parents:
diff changeset
63 #include "system.h"
kono
parents:
diff changeset
64 #endif
kono
parents:
diff changeset
65
kono
parents:
diff changeset
66 #include <time.h>
kono
parents:
diff changeset
67 #include <errno.h>
kono
parents:
diff changeset
68
kono
parents:
diff changeset
69 #if defined (__sun__) && !defined (__vxworks)
kono
parents:
diff changeset
70 /* The declaration is present in <time.h> but conditionalized
kono
parents:
diff changeset
71 on a couple of macros we don't define. */
kono
parents:
diff changeset
72 extern struct tm *localtime_r(const time_t *, struct tm *);
kono
parents:
diff changeset
73 #endif
kono
parents:
diff changeset
74
kono
parents:
diff changeset
75 #include "adaint.h"
kono
parents:
diff changeset
76
kono
parents:
diff changeset
77 /* Don't use macros versions of this functions on VxWorks since they cause
kono
parents:
diff changeset
78 imcompatible changes in some VxWorks versions */
kono
parents:
diff changeset
79 #ifdef __vxworks
kono
parents:
diff changeset
80 #undef getchar
kono
parents:
diff changeset
81 #undef putchar
kono
parents:
diff changeset
82 #undef feof
kono
parents:
diff changeset
83 #undef ferror
kono
parents:
diff changeset
84 #undef fileno
kono
parents:
diff changeset
85 #endif
kono
parents:
diff changeset
86
kono
parents:
diff changeset
87 /*
kono
parents:
diff changeset
88 Notes:
kono
parents:
diff changeset
89
kono
parents:
diff changeset
90 (1) Opening a file with read mode fails if the file does not exist or
kono
parents:
diff changeset
91 cannot be read.
kono
parents:
diff changeset
92
kono
parents:
diff changeset
93 (2) Opening a file with append mode causes all subsequent writes to the
kono
parents:
diff changeset
94 file to be forced to the then current end-of-file, regardless of
kono
parents:
diff changeset
95 intervening calls to the fseek function.
kono
parents:
diff changeset
96
kono
parents:
diff changeset
97 (3) When a file is opened with update mode, both input and output may be
kono
parents:
diff changeset
98 performed on the associated stream. However, output may not be directly
kono
parents:
diff changeset
99 followed by input without an intervening call to the fflush function or
kono
parents:
diff changeset
100 to a file positioning function (fseek, fsetpos, or rewind), and input
kono
parents:
diff changeset
101 may not be directly followed by output without an intervening call to a
kono
parents:
diff changeset
102 file positioning function, unless the input operation encounters
kono
parents:
diff changeset
103 end-of-file.
kono
parents:
diff changeset
104
kono
parents:
diff changeset
105 The other target dependent declarations here are for the three functions
kono
parents:
diff changeset
106 __gnat_set_binary_mode, __gnat_set_text_mode and __gnat_set_mode:
kono
parents:
diff changeset
107
kono
parents:
diff changeset
108 void __gnat_set_binary_mode (int handle);
kono
parents:
diff changeset
109 void __gnat_set_text_mode (int handle);
kono
parents:
diff changeset
110 void __gnat_set_mode (int handle, int mode);
kono
parents:
diff changeset
111
kono
parents:
diff changeset
112 These functions have no effect in Unix (or similar systems where there is
kono
parents:
diff changeset
113 no distinction between binary and text files), but in DOS (and similar
kono
parents:
diff changeset
114 systems where text mode does CR/LF translation), these functions allow
kono
parents:
diff changeset
115 the mode of the stream with the given handle (fileno can be used to get
kono
parents:
diff changeset
116 the handle of a stream) to be changed dynamically. The returned result
kono
parents:
diff changeset
117 is 0 if no error occurs and -1 if an error occurs.
kono
parents:
diff changeset
118
kono
parents:
diff changeset
119 Finally there is a boolean (character) variable
kono
parents:
diff changeset
120
kono
parents:
diff changeset
121 char __gnat_text_translation_required;
kono
parents:
diff changeset
122
kono
parents:
diff changeset
123 which is zero (false) in Unix mode, and one (true) in DOS mode, with a
kono
parents:
diff changeset
124 true value indicating that text translation is required on text files
kono
parents:
diff changeset
125 and that fopen supports the trailing t and b modifiers.
kono
parents:
diff changeset
126
kono
parents:
diff changeset
127 */
kono
parents:
diff changeset
128
kono
parents:
diff changeset
129 #if defined (WINNT) || defined (__CYGWIN__) || defined (__DJGPP__)
kono
parents:
diff changeset
130
kono
parents:
diff changeset
131 const char __gnat_text_translation_required = 1;
kono
parents:
diff changeset
132
kono
parents:
diff changeset
133 #ifdef __CYGWIN__
kono
parents:
diff changeset
134 #define WIN_SETMODE setmode
kono
parents:
diff changeset
135 #include <io.h>
kono
parents:
diff changeset
136 #else
kono
parents:
diff changeset
137 #define WIN_SETMODE _setmode
kono
parents:
diff changeset
138 #endif
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140 #if defined (__DJGPP__)
kono
parents:
diff changeset
141 #include <io.h>
kono
parents:
diff changeset
142 #define _setmode setmode
kono
parents:
diff changeset
143 #endif /* __DJGPP__ */
kono
parents:
diff changeset
144
kono
parents:
diff changeset
145 void
kono
parents:
diff changeset
146 __gnat_set_binary_mode (int handle)
kono
parents:
diff changeset
147 {
kono
parents:
diff changeset
148 WIN_SETMODE (handle, O_BINARY);
kono
parents:
diff changeset
149 }
kono
parents:
diff changeset
150
kono
parents:
diff changeset
151 void
kono
parents:
diff changeset
152 __gnat_set_text_mode (int handle)
kono
parents:
diff changeset
153 {
kono
parents:
diff changeset
154 WIN_SETMODE (handle, O_TEXT);
kono
parents:
diff changeset
155 }
kono
parents:
diff changeset
156
kono
parents:
diff changeset
157 #if defined (__CYGWIN__) || defined (__DJGPP__)
kono
parents:
diff changeset
158 void
kono
parents:
diff changeset
159 __gnat_set_mode (int handle, int mode)
kono
parents:
diff changeset
160 {
kono
parents:
diff changeset
161 /* the values here must be synchronized with
kono
parents:
diff changeset
162 System.File_Control_Block.Content_Encodding:
kono
parents:
diff changeset
163
kono
parents:
diff changeset
164 None = 0
kono
parents:
diff changeset
165 Default_Text = 1
kono
parents:
diff changeset
166 Text = 2
kono
parents:
diff changeset
167 U8text = 3
kono
parents:
diff changeset
168 Wtext = 4
kono
parents:
diff changeset
169 U16text = 5 */
kono
parents:
diff changeset
170
kono
parents:
diff changeset
171 switch (mode) {
kono
parents:
diff changeset
172 case 0 : setmode(handle, O_BINARY); break;
kono
parents:
diff changeset
173 case 1 : setmode(handle, O_TEXT); break;
kono
parents:
diff changeset
174 case 2 : setmode(handle, O_TEXT); break;
kono
parents:
diff changeset
175 case 3 : setmode(handle, O_TEXT); break;
kono
parents:
diff changeset
176 case 4 : setmode(handle, O_BINARY); break;
kono
parents:
diff changeset
177 case 5 : setmode(handle, O_BINARY); break;
kono
parents:
diff changeset
178 }
kono
parents:
diff changeset
179 }
kono
parents:
diff changeset
180 #else
kono
parents:
diff changeset
181 void
kono
parents:
diff changeset
182 __gnat_set_mode (int handle, int mode)
kono
parents:
diff changeset
183 {
kono
parents:
diff changeset
184 /* the values here must be synchronized with
kono
parents:
diff changeset
185 System.File_Control_Block.Content_Encodding:
kono
parents:
diff changeset
186
kono
parents:
diff changeset
187 None = 0
kono
parents:
diff changeset
188 Default_Text = 1
kono
parents:
diff changeset
189 Text = 2
kono
parents:
diff changeset
190 U8text = 3
kono
parents:
diff changeset
191 Wtext = 4
kono
parents:
diff changeset
192 U16text = 5 */
kono
parents:
diff changeset
193
kono
parents:
diff changeset
194 switch (mode) {
kono
parents:
diff changeset
195 case 0 : WIN_SETMODE (handle, _O_BINARY); break;
kono
parents:
diff changeset
196 case 1 : WIN_SETMODE (handle, __gnat_current_ccs_encoding); break;
kono
parents:
diff changeset
197 case 2 : WIN_SETMODE (handle, _O_TEXT); break;
kono
parents:
diff changeset
198 case 3 : WIN_SETMODE (handle, _O_U8TEXT); break;
kono
parents:
diff changeset
199 case 4 : WIN_SETMODE (handle, _O_WTEXT); break;
kono
parents:
diff changeset
200 case 5 : WIN_SETMODE (handle, _O_U16TEXT); break;
kono
parents:
diff changeset
201 }
kono
parents:
diff changeset
202 }
kono
parents:
diff changeset
203 #endif
kono
parents:
diff changeset
204
kono
parents:
diff changeset
205 #ifdef __CYGWIN__
kono
parents:
diff changeset
206
kono
parents:
diff changeset
207 char *
kono
parents:
diff changeset
208 __gnat_ttyname (int filedes)
kono
parents:
diff changeset
209 {
kono
parents:
diff changeset
210 extern char *ttyname (int);
kono
parents:
diff changeset
211
kono
parents:
diff changeset
212 return ttyname (filedes);
kono
parents:
diff changeset
213 }
kono
parents:
diff changeset
214
kono
parents:
diff changeset
215 #endif /* __CYGWIN__ */
kono
parents:
diff changeset
216
kono
parents:
diff changeset
217 #if defined (__CYGWIN__) || defined (__MINGW32__)
kono
parents:
diff changeset
218 #include <windows.h>
kono
parents:
diff changeset
219
kono
parents:
diff changeset
220 int __gnat_is_windows_xp (void);
kono
parents:
diff changeset
221
kono
parents:
diff changeset
222 int
kono
parents:
diff changeset
223 __gnat_is_windows_xp (void)
kono
parents:
diff changeset
224 {
kono
parents:
diff changeset
225 static int is_win_xp=0, is_win_xp_checked=0;
kono
parents:
diff changeset
226
kono
parents:
diff changeset
227 if (!is_win_xp_checked)
kono
parents:
diff changeset
228 {
kono
parents:
diff changeset
229 OSVERSIONINFO version;
kono
parents:
diff changeset
230
kono
parents:
diff changeset
231 is_win_xp_checked = 1;
kono
parents:
diff changeset
232
kono
parents:
diff changeset
233 memset (&version, 0, sizeof (version));
kono
parents:
diff changeset
234 version.dwOSVersionInfoSize = sizeof (version);
kono
parents:
diff changeset
235
kono
parents:
diff changeset
236 is_win_xp = GetVersionEx (&version)
kono
parents:
diff changeset
237 && version.dwPlatformId == VER_PLATFORM_WIN32_NT
kono
parents:
diff changeset
238 && (version.dwMajorVersion > 5
kono
parents:
diff changeset
239 || (version.dwMajorVersion == 5 && version.dwMinorVersion >= 1));
kono
parents:
diff changeset
240 }
kono
parents:
diff changeset
241 return is_win_xp;
kono
parents:
diff changeset
242 }
kono
parents:
diff changeset
243
kono
parents:
diff changeset
244 /* Get the bounds of the stack. The stack pointer is supposed to be
kono
parents:
diff changeset
245 initialized to BASE when a thread is created and the stack can be extended
kono
parents:
diff changeset
246 to LIMIT before reaching a guard page.
kono
parents:
diff changeset
247 Note: for the main thread, the system automatically extend the stack, so
kono
parents:
diff changeset
248 LIMIT is only the current limit. */
kono
parents:
diff changeset
249
kono
parents:
diff changeset
250 void
kono
parents:
diff changeset
251 __gnat_get_stack_bounds (void **base, void **limit)
kono
parents:
diff changeset
252 {
kono
parents:
diff changeset
253 NT_TIB *tib;
kono
parents:
diff changeset
254
kono
parents:
diff changeset
255 /* We know that the first field of the TEB is the TIB. */
kono
parents:
diff changeset
256 tib = (NT_TIB *)NtCurrentTeb ();
kono
parents:
diff changeset
257
kono
parents:
diff changeset
258 *base = tib->StackBase;
kono
parents:
diff changeset
259 *limit = tib->StackLimit;
kono
parents:
diff changeset
260 }
kono
parents:
diff changeset
261
kono
parents:
diff changeset
262 #endif /* __CYGWIN__ || __MINGW32__ */
kono
parents:
diff changeset
263
kono
parents:
diff changeset
264 #ifdef __MINGW32__
kono
parents:
diff changeset
265
kono
parents:
diff changeset
266 /* Return the name of the tty. Under windows there is no name for
kono
parents:
diff changeset
267 the tty, so this function, if connected to a tty, returns the generic name
kono
parents:
diff changeset
268 "console". */
kono
parents:
diff changeset
269
kono
parents:
diff changeset
270 char *
kono
parents:
diff changeset
271 __gnat_ttyname (int filedes)
kono
parents:
diff changeset
272 {
kono
parents:
diff changeset
273 if (isatty (filedes))
kono
parents:
diff changeset
274 return "console";
kono
parents:
diff changeset
275 else
kono
parents:
diff changeset
276 return NULL;
kono
parents:
diff changeset
277 }
kono
parents:
diff changeset
278
kono
parents:
diff changeset
279 #endif /* __MINGW32__ */
kono
parents:
diff changeset
280
kono
parents:
diff changeset
281 #else
kono
parents:
diff changeset
282
kono
parents:
diff changeset
283 const char __gnat_text_translation_required = 0;
kono
parents:
diff changeset
284
kono
parents:
diff changeset
285 /* These functions do nothing in non-DOS systems. */
kono
parents:
diff changeset
286
kono
parents:
diff changeset
287 void
kono
parents:
diff changeset
288 __gnat_set_binary_mode (int handle ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
289 {
kono
parents:
diff changeset
290 }
kono
parents:
diff changeset
291
kono
parents:
diff changeset
292 void
kono
parents:
diff changeset
293 __gnat_set_text_mode (int handle ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
294 {
kono
parents:
diff changeset
295 }
kono
parents:
diff changeset
296
kono
parents:
diff changeset
297 void
kono
parents:
diff changeset
298 __gnat_set_mode (int handle ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
299 {
kono
parents:
diff changeset
300 }
kono
parents:
diff changeset
301
kono
parents:
diff changeset
302 char *
kono
parents:
diff changeset
303 __gnat_ttyname (int filedes)
kono
parents:
diff changeset
304 {
kono
parents:
diff changeset
305 #if defined (__vxworks)
kono
parents:
diff changeset
306 return "";
kono
parents:
diff changeset
307 #else
kono
parents:
diff changeset
308 extern char *ttyname (int);
kono
parents:
diff changeset
309
kono
parents:
diff changeset
310 return ttyname (filedes);
kono
parents:
diff changeset
311 #endif /* defined (__vxworks) */
kono
parents:
diff changeset
312 }
kono
parents:
diff changeset
313 #endif
kono
parents:
diff changeset
314
kono
parents:
diff changeset
315 #if defined (__linux__) || defined (__sun__) \
kono
parents:
diff changeset
316 || defined (WINNT) \
kono
parents:
diff changeset
317 || defined (__MACHTEN__) || defined (__hpux__) || defined (_AIX) \
kono
parents:
diff changeset
318 || (defined (__svr4__) && defined (__i386__)) || defined (__Lynx__) \
kono
parents:
diff changeset
319 || defined (__CYGWIN__) || defined (__FreeBSD__) || defined (__OpenBSD__) \
kono
parents:
diff changeset
320 || defined (__GLIBC__) || defined (__APPLE__) || defined (__DragonFly__)
kono
parents:
diff changeset
321
kono
parents:
diff changeset
322 # ifdef __MINGW32__
kono
parents:
diff changeset
323 # if OLD_MINGW
kono
parents:
diff changeset
324 # include <termios.h>
kono
parents:
diff changeset
325 # else
kono
parents:
diff changeset
326 # include <conio.h> /* for getch(), kbhit() */
kono
parents:
diff changeset
327 # endif
kono
parents:
diff changeset
328 # else
kono
parents:
diff changeset
329 # include <termios.h>
kono
parents:
diff changeset
330 # endif
kono
parents:
diff changeset
331
kono
parents:
diff changeset
332 #endif
kono
parents:
diff changeset
333
kono
parents:
diff changeset
334 /* Implements the common processing for getc_immediate and
kono
parents:
diff changeset
335 getc_immediate_nowait. */
kono
parents:
diff changeset
336
kono
parents:
diff changeset
337 extern void getc_immediate (FILE *, int *, int *);
kono
parents:
diff changeset
338 extern void getc_immediate_nowait (FILE *, int *, int *, int *);
kono
parents:
diff changeset
339 extern void getc_immediate_common (FILE *, int *, int *, int *, int);
kono
parents:
diff changeset
340
kono
parents:
diff changeset
341 /* Called by Get_Immediate (Foo); */
kono
parents:
diff changeset
342
kono
parents:
diff changeset
343 void
kono
parents:
diff changeset
344 getc_immediate (FILE *stream, int *ch, int *end_of_file)
kono
parents:
diff changeset
345 {
kono
parents:
diff changeset
346 int avail;
kono
parents:
diff changeset
347
kono
parents:
diff changeset
348 getc_immediate_common (stream, ch, end_of_file, &avail, 1);
kono
parents:
diff changeset
349 }
kono
parents:
diff changeset
350
kono
parents:
diff changeset
351 /* Called by Get_Immediate (Foo, Available); */
kono
parents:
diff changeset
352
kono
parents:
diff changeset
353 void
kono
parents:
diff changeset
354 getc_immediate_nowait (FILE *stream, int *ch, int *end_of_file, int *avail)
kono
parents:
diff changeset
355 {
kono
parents:
diff changeset
356 getc_immediate_common (stream, ch, end_of_file, avail, 0);
kono
parents:
diff changeset
357 }
kono
parents:
diff changeset
358
kono
parents:
diff changeset
359 /* Called by getc_immediate () and getc_immediate_nowait () */
kono
parents:
diff changeset
360
kono
parents:
diff changeset
361 void
kono
parents:
diff changeset
362 getc_immediate_common (FILE *stream,
kono
parents:
diff changeset
363 int *ch,
kono
parents:
diff changeset
364 int *end_of_file,
kono
parents:
diff changeset
365 int *avail,
kono
parents:
diff changeset
366 int waiting ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
367 {
kono
parents:
diff changeset
368 #if defined (__linux__) || defined (__sun__) \
kono
parents:
diff changeset
369 || defined (__CYGWIN32__) || defined (__MACHTEN__) || defined (__hpux__) \
kono
parents:
diff changeset
370 || defined (_AIX) || (defined (__svr4__) && defined (__i386__)) \
kono
parents:
diff changeset
371 || defined (__Lynx__) || defined (__FreeBSD__) || defined (__OpenBSD__) \
kono
parents:
diff changeset
372 || defined (__GLIBC__) || defined (__APPLE__) || defined (__DragonFly__)
kono
parents:
diff changeset
373 char c;
kono
parents:
diff changeset
374 int nread;
kono
parents:
diff changeset
375 int good_one = 0;
kono
parents:
diff changeset
376 int eof_ch = 4; /* Ctrl-D */
kono
parents:
diff changeset
377 int fd = fileno (stream);
kono
parents:
diff changeset
378 struct termios otermios_rec, termios_rec;
kono
parents:
diff changeset
379
kono
parents:
diff changeset
380 if (isatty (fd))
kono
parents:
diff changeset
381 {
kono
parents:
diff changeset
382 tcgetattr (fd, &termios_rec);
kono
parents:
diff changeset
383 memcpy (&otermios_rec, &termios_rec, sizeof (struct termios));
kono
parents:
diff changeset
384
kono
parents:
diff changeset
385 /* Set RAW mode, with no echo */
kono
parents:
diff changeset
386 termios_rec.c_lflag = termios_rec.c_lflag & ~ICANON & ~ECHO;
kono
parents:
diff changeset
387
kono
parents:
diff changeset
388 #if defined (__linux__) || defined (__sun__) \
kono
parents:
diff changeset
389 || defined (__MACHTEN__) || defined (__hpux__) \
kono
parents:
diff changeset
390 || defined (_AIX) || (defined (__svr4__) && defined (__i386__)) \
kono
parents:
diff changeset
391 || defined (__Lynx__) || defined (__FreeBSD__) || defined (__OpenBSD__) \
kono
parents:
diff changeset
392 || defined (__GLIBC__) || defined (__APPLE__) || defined (__DragonFly__)
kono
parents:
diff changeset
393 eof_ch = termios_rec.c_cc[VEOF];
kono
parents:
diff changeset
394
kono
parents:
diff changeset
395 /* If waiting (i.e. Get_Immediate (Char)), set MIN = 1 and wait for
kono
parents:
diff changeset
396 a character forever. This doesn't seem to effect Ctrl-Z or
kono
parents:
diff changeset
397 Ctrl-C processing.
kono
parents:
diff changeset
398 If not waiting (i.e. Get_Immediate (Char, Available)),
kono
parents:
diff changeset
399 don't wait for anything but timeout immediately. */
kono
parents:
diff changeset
400 termios_rec.c_cc[VMIN] = waiting;
kono
parents:
diff changeset
401 termios_rec.c_cc[VTIME] = 0;
kono
parents:
diff changeset
402 #endif
kono
parents:
diff changeset
403 tcsetattr (fd, TCSANOW, &termios_rec);
kono
parents:
diff changeset
404
kono
parents:
diff changeset
405 while (! good_one)
kono
parents:
diff changeset
406 {
kono
parents:
diff changeset
407 /* Read is used here instead of fread, because fread doesn't
kono
parents:
diff changeset
408 work on Solaris5 and Sunos4 in this situation. Maybe because we
kono
parents:
diff changeset
409 are mixing calls that use file descriptors and streams. */
kono
parents:
diff changeset
410 nread = read (fd, &c, 1);
kono
parents:
diff changeset
411 if (nread > 0)
kono
parents:
diff changeset
412 {
kono
parents:
diff changeset
413 /* On Unix terminals, Ctrl-D (EOT) is an End of File. */
kono
parents:
diff changeset
414 if (c == eof_ch)
kono
parents:
diff changeset
415 {
kono
parents:
diff changeset
416 *avail = 0;
kono
parents:
diff changeset
417 *end_of_file = 1;
kono
parents:
diff changeset
418 good_one = 1;
kono
parents:
diff changeset
419 }
kono
parents:
diff changeset
420
kono
parents:
diff changeset
421 /* Everything else is ok */
kono
parents:
diff changeset
422 else if (c != eof_ch)
kono
parents:
diff changeset
423 {
kono
parents:
diff changeset
424 *avail = 1;
kono
parents:
diff changeset
425 *end_of_file = 0;
kono
parents:
diff changeset
426 good_one = 1;
kono
parents:
diff changeset
427 }
kono
parents:
diff changeset
428 }
kono
parents:
diff changeset
429
kono
parents:
diff changeset
430 else if (! waiting)
kono
parents:
diff changeset
431 {
kono
parents:
diff changeset
432 *avail = 0;
kono
parents:
diff changeset
433 *end_of_file = 0;
kono
parents:
diff changeset
434 good_one = 1;
kono
parents:
diff changeset
435 }
kono
parents:
diff changeset
436 else
kono
parents:
diff changeset
437 good_one = 0;
kono
parents:
diff changeset
438 }
kono
parents:
diff changeset
439
kono
parents:
diff changeset
440 tcsetattr (fd, TCSANOW, &otermios_rec);
kono
parents:
diff changeset
441 *ch = c;
kono
parents:
diff changeset
442 }
kono
parents:
diff changeset
443
kono
parents:
diff changeset
444 else
kono
parents:
diff changeset
445 #elif defined (__MINGW32__)
kono
parents:
diff changeset
446 int fd = fileno (stream);
kono
parents:
diff changeset
447 int char_waiting;
kono
parents:
diff changeset
448 int eot_ch = 4; /* Ctrl-D */
kono
parents:
diff changeset
449
kono
parents:
diff changeset
450 if (isatty (fd))
kono
parents:
diff changeset
451 {
kono
parents:
diff changeset
452 if (waiting)
kono
parents:
diff changeset
453 {
kono
parents:
diff changeset
454 *ch = getch ();
kono
parents:
diff changeset
455
kono
parents:
diff changeset
456 if (*ch == eot_ch)
kono
parents:
diff changeset
457 *end_of_file = 1;
kono
parents:
diff changeset
458 else
kono
parents:
diff changeset
459 *end_of_file = 0;
kono
parents:
diff changeset
460
kono
parents:
diff changeset
461 *avail = 1;
kono
parents:
diff changeset
462 }
kono
parents:
diff changeset
463 else /* ! waiting */
kono
parents:
diff changeset
464 {
kono
parents:
diff changeset
465 char_waiting = kbhit();
kono
parents:
diff changeset
466
kono
parents:
diff changeset
467 if (char_waiting == 1)
kono
parents:
diff changeset
468 {
kono
parents:
diff changeset
469 *avail = 1;
kono
parents:
diff changeset
470 *ch = getch ();
kono
parents:
diff changeset
471
kono
parents:
diff changeset
472 if (*ch == eot_ch)
kono
parents:
diff changeset
473 *end_of_file = 1;
kono
parents:
diff changeset
474 else
kono
parents:
diff changeset
475 *end_of_file = 0;
kono
parents:
diff changeset
476 }
kono
parents:
diff changeset
477 else
kono
parents:
diff changeset
478 {
kono
parents:
diff changeset
479 *avail = 0;
kono
parents:
diff changeset
480 *end_of_file = 0;
kono
parents:
diff changeset
481 }
kono
parents:
diff changeset
482 }
kono
parents:
diff changeset
483 }
kono
parents:
diff changeset
484 else
kono
parents:
diff changeset
485 #elif defined (__vxworks)
kono
parents:
diff changeset
486 /* Bit masks of file descriptors to read from. */
kono
parents:
diff changeset
487 struct fd_set readFds;
kono
parents:
diff changeset
488 /* Timeout before select returns if nothing can be read. */
kono
parents:
diff changeset
489 struct timeval timeOut;
kono
parents:
diff changeset
490 char c;
kono
parents:
diff changeset
491 int fd = fileno (stream);
kono
parents:
diff changeset
492 int nread;
kono
parents:
diff changeset
493 int option;
kono
parents:
diff changeset
494 int readable;
kono
parents:
diff changeset
495 int status;
kono
parents:
diff changeset
496 int width;
kono
parents:
diff changeset
497
kono
parents:
diff changeset
498 if (isatty (fd))
kono
parents:
diff changeset
499 {
kono
parents:
diff changeset
500 /* If we do not want to wait, we have to set up fd in RAW mode. This
kono
parents:
diff changeset
501 should be done outside this function as setting fd in RAW mode under
kono
parents:
diff changeset
502 vxWorks flushes the buffer of fd. If the RAW mode was set here, the
kono
parents:
diff changeset
503 buffer would be empty and we would always return that no character
kono
parents:
diff changeset
504 is available */
kono
parents:
diff changeset
505 if (! waiting)
kono
parents:
diff changeset
506 {
kono
parents:
diff changeset
507 /* Initialization of timeOut for its use with select. */
kono
parents:
diff changeset
508 timeOut.tv_sec = 0;
kono
parents:
diff changeset
509 timeOut.tv_usec = 0;
kono
parents:
diff changeset
510
kono
parents:
diff changeset
511 /* Initialization of readFds for its use with select;
kono
parents:
diff changeset
512 FD is the only file descriptor to be monitored */
kono
parents:
diff changeset
513 FD_ZERO (&readFds);
kono
parents:
diff changeset
514 FD_SET (fd, &readFds);
kono
parents:
diff changeset
515 width = 2;
kono
parents:
diff changeset
516
kono
parents:
diff changeset
517 /* We do all this processing to emulate a non blocking read. */
kono
parents:
diff changeset
518 readable = select (width, &readFds, NULL, NULL, &timeOut);
kono
parents:
diff changeset
519 if (readable == ERROR)
kono
parents:
diff changeset
520 *avail = -1, *end_of_file = -1;
kono
parents:
diff changeset
521 /* No character available in input. */
kono
parents:
diff changeset
522 else if (readable == 0)
kono
parents:
diff changeset
523 *avail = 0, *end_of_file = 0;
kono
parents:
diff changeset
524 else
kono
parents:
diff changeset
525 {
kono
parents:
diff changeset
526 nread = read (fd, &c, 1);
kono
parents:
diff changeset
527 if (nread > 0)
kono
parents:
diff changeset
528 *avail = 1, *end_of_file = 0;
kono
parents:
diff changeset
529 /* End Of File. */
kono
parents:
diff changeset
530 else if (nread == 0)
kono
parents:
diff changeset
531 *avail = 0, *end_of_file = 1;
kono
parents:
diff changeset
532 /* Error. */
kono
parents:
diff changeset
533 else
kono
parents:
diff changeset
534 *avail = -1, *end_of_file = -1;
kono
parents:
diff changeset
535 }
kono
parents:
diff changeset
536 }
kono
parents:
diff changeset
537
kono
parents:
diff changeset
538 /* We have to wait until we get a character */
kono
parents:
diff changeset
539 else
kono
parents:
diff changeset
540 {
kono
parents:
diff changeset
541 *avail = -1;
kono
parents:
diff changeset
542 *end_of_file = -1;
kono
parents:
diff changeset
543
kono
parents:
diff changeset
544 /* Save the current mode of FD. */
kono
parents:
diff changeset
545 option = ioctl (fd, FIOGETOPTIONS, 0);
kono
parents:
diff changeset
546
kono
parents:
diff changeset
547 /* Set FD in RAW mode. */
kono
parents:
diff changeset
548 status = ioctl (fd, FIOSETOPTIONS, OPT_RAW);
kono
parents:
diff changeset
549 if (status != -1)
kono
parents:
diff changeset
550 {
kono
parents:
diff changeset
551 nread = read (fd, &c, 1);
kono
parents:
diff changeset
552 if (nread > 0)
kono
parents:
diff changeset
553 *avail = 1, *end_of_file = 0;
kono
parents:
diff changeset
554 /* End of file. */
kono
parents:
diff changeset
555 else if (nread == 0)
kono
parents:
diff changeset
556 *avail = 0, *end_of_file = 1;
kono
parents:
diff changeset
557 /* Else there is an ERROR. */
kono
parents:
diff changeset
558 }
kono
parents:
diff changeset
559
kono
parents:
diff changeset
560 /* Revert FD to its previous mode. */
kono
parents:
diff changeset
561 status = ioctl (fd, FIOSETOPTIONS, option);
kono
parents:
diff changeset
562 }
kono
parents:
diff changeset
563
kono
parents:
diff changeset
564 *ch = c;
kono
parents:
diff changeset
565 }
kono
parents:
diff changeset
566 else
kono
parents:
diff changeset
567 #endif
kono
parents:
diff changeset
568 {
kono
parents:
diff changeset
569 /* If we're not on a terminal, then we don't need any fancy processing.
kono
parents:
diff changeset
570 Also this is the only thing that's left if we're not on one of the
kono
parents:
diff changeset
571 supported systems; which means that for non supported systems,
kono
parents:
diff changeset
572 get_immediate may wait for a carriage return on terminals. */
kono
parents:
diff changeset
573 *ch = fgetc (stream);
kono
parents:
diff changeset
574 if (feof (stream))
kono
parents:
diff changeset
575 {
kono
parents:
diff changeset
576 *end_of_file = 1;
kono
parents:
diff changeset
577 *avail = 0;
kono
parents:
diff changeset
578 }
kono
parents:
diff changeset
579 else
kono
parents:
diff changeset
580 {
kono
parents:
diff changeset
581 *end_of_file = 0;
kono
parents:
diff changeset
582 *avail = 1;
kono
parents:
diff changeset
583 }
kono
parents:
diff changeset
584 }
kono
parents:
diff changeset
585 }
kono
parents:
diff changeset
586
kono
parents:
diff changeset
587 /* The following definitions are provided in NT to support Windows based
kono
parents:
diff changeset
588 Ada programs. */
kono
parents:
diff changeset
589
kono
parents:
diff changeset
590 #ifdef WINNT
kono
parents:
diff changeset
591 #include <windows.h>
kono
parents:
diff changeset
592
kono
parents:
diff changeset
593 /* Provide functions to echo the values passed to WinMain (windows bindings
kono
parents:
diff changeset
594 will want to import these). We use the same names as the routines used
kono
parents:
diff changeset
595 by AdaMagic for compatibility. */
kono
parents:
diff changeset
596
kono
parents:
diff changeset
597 char *rts_get_hInstance (void);
kono
parents:
diff changeset
598 char *rts_get_hPrevInstance (void);
kono
parents:
diff changeset
599 char *rts_get_lpCommandLine (void);
kono
parents:
diff changeset
600 int rts_get_nShowCmd (void);
kono
parents:
diff changeset
601
kono
parents:
diff changeset
602 char *
kono
parents:
diff changeset
603 rts_get_hInstance (void)
kono
parents:
diff changeset
604 {
kono
parents:
diff changeset
605 return (char *)GetModuleHandleA (0);
kono
parents:
diff changeset
606 }
kono
parents:
diff changeset
607
kono
parents:
diff changeset
608 char *
kono
parents:
diff changeset
609 rts_get_hPrevInstance (void)
kono
parents:
diff changeset
610 {
kono
parents:
diff changeset
611 return 0;
kono
parents:
diff changeset
612 }
kono
parents:
diff changeset
613
kono
parents:
diff changeset
614 char *
kono
parents:
diff changeset
615 rts_get_lpCommandLine (void)
kono
parents:
diff changeset
616 {
kono
parents:
diff changeset
617 return GetCommandLineA ();
kono
parents:
diff changeset
618 }
kono
parents:
diff changeset
619
kono
parents:
diff changeset
620 int
kono
parents:
diff changeset
621 rts_get_nShowCmd (void)
kono
parents:
diff changeset
622 {
kono
parents:
diff changeset
623 return 1;
kono
parents:
diff changeset
624 }
kono
parents:
diff changeset
625
kono
parents:
diff changeset
626 #endif /* WINNT */
kono
parents:
diff changeset
627
kono
parents:
diff changeset
628 /* This value is returned as the time zone offset when a valid value
kono
parents:
diff changeset
629 cannot be determined. It is simply a bizarre value that will never
kono
parents:
diff changeset
630 occur. It is 3 days plus 73 seconds (offset is in seconds). */
kono
parents:
diff changeset
631
kono
parents:
diff changeset
632 long __gnat_invalid_tzoff = 259273;
kono
parents:
diff changeset
633
kono
parents:
diff changeset
634 /* Definition of __gnat_localtime_r used by a-calend.adb */
kono
parents:
diff changeset
635
kono
parents:
diff changeset
636 #if defined (__MINGW32__)
kono
parents:
diff changeset
637
kono
parents:
diff changeset
638 /* Reentrant localtime for Windows. */
kono
parents:
diff changeset
639
kono
parents:
diff changeset
640 extern void
kono
parents:
diff changeset
641 __gnat_localtime_tzoff (const time_t *, const int *, long *);
kono
parents:
diff changeset
642
kono
parents:
diff changeset
643 static const unsigned long long w32_epoch_offset = 11644473600ULL;
kono
parents:
diff changeset
644 void
kono
parents:
diff changeset
645 __gnat_localtime_tzoff (const time_t *timer, const int *is_historic, long *off)
kono
parents:
diff changeset
646 {
kono
parents:
diff changeset
647 TIME_ZONE_INFORMATION tzi;
kono
parents:
diff changeset
648
kono
parents:
diff changeset
649 DWORD tzi_status;
kono
parents:
diff changeset
650
kono
parents:
diff changeset
651 tzi_status = GetTimeZoneInformation (&tzi);
kono
parents:
diff changeset
652
kono
parents:
diff changeset
653 /* Cases where we simply want to extract the offset of the current time
kono
parents:
diff changeset
654 zone, regardless of the date. A value of "0" for flag "is_historic"
kono
parents:
diff changeset
655 signifies that the date is NOT historic, see the
kono
parents:
diff changeset
656 body of Ada.Calendar.UTC_Time_Offset. */
kono
parents:
diff changeset
657
kono
parents:
diff changeset
658 if (*is_historic == 0) {
kono
parents:
diff changeset
659 *off = tzi.Bias;
kono
parents:
diff changeset
660
kono
parents:
diff changeset
661 /* The system is operating in the range covered by the StandardDate
kono
parents:
diff changeset
662 member. */
kono
parents:
diff changeset
663 if (tzi_status == TIME_ZONE_ID_STANDARD) {
kono
parents:
diff changeset
664 *off = *off + tzi.StandardBias;
kono
parents:
diff changeset
665 }
kono
parents:
diff changeset
666
kono
parents:
diff changeset
667 /* The system is operating in the range covered by the DaylightDate
kono
parents:
diff changeset
668 member. */
kono
parents:
diff changeset
669 else if (tzi_status == TIME_ZONE_ID_DAYLIGHT) {
kono
parents:
diff changeset
670 *off = *off + tzi.DaylightBias;
kono
parents:
diff changeset
671 }
kono
parents:
diff changeset
672
kono
parents:
diff changeset
673 *off = *off * -60;
kono
parents:
diff changeset
674 }
kono
parents:
diff changeset
675
kono
parents:
diff changeset
676 /* Time zone offset calculations for a historic or future date */
kono
parents:
diff changeset
677
kono
parents:
diff changeset
678 else {
kono
parents:
diff changeset
679 union
kono
parents:
diff changeset
680 {
kono
parents:
diff changeset
681 FILETIME ft_time;
kono
parents:
diff changeset
682 unsigned long long ull_time;
kono
parents:
diff changeset
683 } utc_time, local_time;
kono
parents:
diff changeset
684
kono
parents:
diff changeset
685 SYSTEMTIME utc_sys_time, local_sys_time;
kono
parents:
diff changeset
686 BOOL status;
kono
parents:
diff changeset
687
kono
parents:
diff changeset
688 /* First convert unix time_t structure to windows FILETIME format. */
kono
parents:
diff changeset
689 utc_time.ull_time = ((unsigned long long) *timer + w32_epoch_offset)
kono
parents:
diff changeset
690 * 10000000ULL;
kono
parents:
diff changeset
691
kono
parents:
diff changeset
692 /* If GetTimeZoneInformation does not return a value between 0 and 2 then
kono
parents:
diff changeset
693 it means that we were not able to retrieve timezone information. Note
kono
parents:
diff changeset
694 that we cannot use here FileTimeToLocalFileTime as Windows will use in
kono
parents:
diff changeset
695 always in this case the current timezone setting. As suggested on MSDN
kono
parents:
diff changeset
696 we use the following three system calls to get the right information.
kono
parents:
diff changeset
697 Note also that starting with Windows Vista new functions are provided
kono
parents:
diff changeset
698 to get timezone settings that depend on the year. We cannot use them as
kono
parents:
diff changeset
699 we still support Windows XP and Windows 2003. */
kono
parents:
diff changeset
700
kono
parents:
diff changeset
701 status = (tzi_status >= 0 && tzi_status <= 2)
kono
parents:
diff changeset
702 && FileTimeToSystemTime (&utc_time.ft_time, &utc_sys_time)
kono
parents:
diff changeset
703 && SystemTimeToTzSpecificLocalTime (&tzi, &utc_sys_time, &local_sys_time)
kono
parents:
diff changeset
704 && SystemTimeToFileTime (&local_sys_time, &local_time.ft_time);
kono
parents:
diff changeset
705
kono
parents:
diff changeset
706 /* An error has occurred, return invalid_tzoff */
kono
parents:
diff changeset
707
kono
parents:
diff changeset
708 if (!status) {
kono
parents:
diff changeset
709 *off = __gnat_invalid_tzoff;
kono
parents:
diff changeset
710 }
kono
parents:
diff changeset
711 else {
kono
parents:
diff changeset
712 if (local_time.ull_time > utc_time.ull_time) {
kono
parents:
diff changeset
713 *off = (long) ((local_time.ull_time - utc_time.ull_time)
kono
parents:
diff changeset
714 / 10000000ULL);
kono
parents:
diff changeset
715 }
kono
parents:
diff changeset
716 else {
kono
parents:
diff changeset
717 *off = - (long) ((utc_time.ull_time - local_time.ull_time)
kono
parents:
diff changeset
718 / 10000000ULL);
kono
parents:
diff changeset
719 }
kono
parents:
diff changeset
720 }
kono
parents:
diff changeset
721 }
kono
parents:
diff changeset
722 }
kono
parents:
diff changeset
723
kono
parents:
diff changeset
724 #elif defined (__Lynx__)
kono
parents:
diff changeset
725
kono
parents:
diff changeset
726 /* On Lynx, all time values are treated in GMT */
kono
parents:
diff changeset
727
kono
parents:
diff changeset
728 /* As of LynxOS 3.1.0a patch level 040, LynuxWorks changes the
kono
parents:
diff changeset
729 prototype to the C library function localtime_r from the POSIX.4
kono
parents:
diff changeset
730 Draft 9 to the POSIX 1.c version. Before this change the following
kono
parents:
diff changeset
731 spec is required. Only use when ___THREADS_POSIX4ad4__ is defined,
kono
parents:
diff changeset
732 the Lynx convention when building against the legacy API. */
kono
parents:
diff changeset
733
kono
parents:
diff changeset
734 extern void
kono
parents:
diff changeset
735 __gnat_localtime_tzoff (const time_t *, const int *, long *);
kono
parents:
diff changeset
736
kono
parents:
diff changeset
737 void
kono
parents:
diff changeset
738 __gnat_localtime_tzoff (const time_t *timer, const int *is_historic, long *off)
kono
parents:
diff changeset
739 {
kono
parents:
diff changeset
740 *off = 0;
kono
parents:
diff changeset
741 }
kono
parents:
diff changeset
742
kono
parents:
diff changeset
743 #else
kono
parents:
diff changeset
744
kono
parents:
diff changeset
745 /* Other targets except Lynx and Windows provide a standard localtime_r */
kono
parents:
diff changeset
746
kono
parents:
diff changeset
747 #define Lock_Task system__soft_links__lock_task
kono
parents:
diff changeset
748 extern void (*Lock_Task) (void);
kono
parents:
diff changeset
749
kono
parents:
diff changeset
750 #define Unlock_Task system__soft_links__unlock_task
kono
parents:
diff changeset
751 extern void (*Unlock_Task) (void);
kono
parents:
diff changeset
752
kono
parents:
diff changeset
753 extern void
kono
parents:
diff changeset
754 __gnat_localtime_tzoff (const time_t *, const int *, long *);
kono
parents:
diff changeset
755
kono
parents:
diff changeset
756 void
kono
parents:
diff changeset
757 __gnat_localtime_tzoff (const time_t *timer ATTRIBUTE_UNUSED,
kono
parents:
diff changeset
758 const int *is_historic ATTRIBUTE_UNUSED,
kono
parents:
diff changeset
759 long *off ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
760 {
kono
parents:
diff changeset
761 struct tm tp ATTRIBUTE_UNUSED;
kono
parents:
diff changeset
762
kono
parents:
diff changeset
763 /* AIX, HPUX, Sun Solaris */
kono
parents:
diff changeset
764 #if defined (_AIX) || defined (__hpux__) || defined (__sun__)
kono
parents:
diff changeset
765 {
kono
parents:
diff changeset
766 (*Lock_Task) ();
kono
parents:
diff changeset
767
kono
parents:
diff changeset
768 localtime_r (timer, &tp);
kono
parents:
diff changeset
769 *off = (long) -timezone;
kono
parents:
diff changeset
770
kono
parents:
diff changeset
771 (*Unlock_Task) ();
kono
parents:
diff changeset
772
kono
parents:
diff changeset
773 /* Correct the offset if Daylight Saving Time is in effect */
kono
parents:
diff changeset
774
kono
parents:
diff changeset
775 if (tp.tm_isdst > 0)
kono
parents:
diff changeset
776 *off = *off + 3600;
kono
parents:
diff changeset
777 }
kono
parents:
diff changeset
778
kono
parents:
diff changeset
779 /* VxWorks */
kono
parents:
diff changeset
780 #elif defined (__vxworks)
kono
parents:
diff changeset
781 #include <stdlib.h>
kono
parents:
diff changeset
782 {
kono
parents:
diff changeset
783 (*Lock_Task) ();
kono
parents:
diff changeset
784
kono
parents:
diff changeset
785 localtime_r (timer, &tp);
kono
parents:
diff changeset
786
kono
parents:
diff changeset
787 /* Try to read the environment variable TIMEZONE. The variable may not have
kono
parents:
diff changeset
788 been initialize, in that case return an offset of zero (0) for UTC. */
kono
parents:
diff changeset
789
kono
parents:
diff changeset
790 char *tz_str = getenv ("TIMEZONE");
kono
parents:
diff changeset
791
kono
parents:
diff changeset
792 if ((tz_str == NULL) || (*tz_str == '\0'))
kono
parents:
diff changeset
793 *off = 0;
kono
parents:
diff changeset
794 else
kono
parents:
diff changeset
795 {
kono
parents:
diff changeset
796 char *tz_start, *tz_end;
kono
parents:
diff changeset
797
kono
parents:
diff changeset
798 /* The format of the data contained in TIMEZONE is N::U:S:E where N is the
kono
parents:
diff changeset
799 name of the time zone, U are the minutes difference from UTC, S is the
kono
parents:
diff changeset
800 start of DST in mmddhh and E is the end of DST in mmddhh. Extracting
kono
parents:
diff changeset
801 the value of U involves setting two pointers, one at the beginning and
kono
parents:
diff changeset
802 one at the end of the value. The end pointer is then set to null in
kono
parents:
diff changeset
803 order to delimit a string slice for atol to process. */
kono
parents:
diff changeset
804
kono
parents:
diff changeset
805 tz_start = index (tz_str, ':') + 2;
kono
parents:
diff changeset
806 tz_end = index (tz_start, ':');
kono
parents:
diff changeset
807 *tz_end = '\0';
kono
parents:
diff changeset
808
kono
parents:
diff changeset
809 /* The Ada layer expects an offset in seconds. Note that we must reverse
kono
parents:
diff changeset
810 the sign of the result since west is positive and east is negative on
kono
parents:
diff changeset
811 VxWorks targets. */
kono
parents:
diff changeset
812
kono
parents:
diff changeset
813 *off = -atol (tz_start) * 60;
kono
parents:
diff changeset
814
kono
parents:
diff changeset
815 /* Correct the offset if Daylight Saving Time is in effect */
kono
parents:
diff changeset
816
kono
parents:
diff changeset
817 if (tp.tm_isdst > 0)
kono
parents:
diff changeset
818 *off = *off + 3600;
kono
parents:
diff changeset
819 }
kono
parents:
diff changeset
820
kono
parents:
diff changeset
821 (*Unlock_Task) ();
kono
parents:
diff changeset
822 }
kono
parents:
diff changeset
823
kono
parents:
diff changeset
824 /* Darwin, Free BSD, Linux, where component tm_gmtoff is present in
kono
parents:
diff changeset
825 struct tm */
kono
parents:
diff changeset
826
kono
parents:
diff changeset
827 #elif defined (__APPLE__) || defined (__FreeBSD__) || defined (__linux__) \
kono
parents:
diff changeset
828 || defined (__GLIBC__) || defined (__DragonFly__) || defined (__OpenBSD__) \
kono
parents:
diff changeset
829 || defined (__DJGPP__)
kono
parents:
diff changeset
830 {
kono
parents:
diff changeset
831 localtime_r (timer, &tp);
kono
parents:
diff changeset
832 *off = tp.tm_gmtoff;
kono
parents:
diff changeset
833 }
kono
parents:
diff changeset
834
kono
parents:
diff changeset
835 /* Default: treat all time values in GMT */
kono
parents:
diff changeset
836
kono
parents:
diff changeset
837 #else
kono
parents:
diff changeset
838 *off = 0;
kono
parents:
diff changeset
839
kono
parents:
diff changeset
840 #endif /* defined(_AIX) ... */
kono
parents:
diff changeset
841 }
kono
parents:
diff changeset
842
kono
parents:
diff changeset
843 #endif
kono
parents:
diff changeset
844
kono
parents:
diff changeset
845 #ifdef __vxworks
kono
parents:
diff changeset
846
kono
parents:
diff changeset
847 #include <taskLib.h>
kono
parents:
diff changeset
848
kono
parents:
diff changeset
849 /* __gnat_get_task_options is used by s-taprop.adb only for VxWorks. This
kono
parents:
diff changeset
850 function returns the options to be set when creating a new task. It fetches
kono
parents:
diff changeset
851 the options assigned to the current task (parent), so offering some user
kono
parents:
diff changeset
852 level control over the options for a task hierarchy. It forces VX_FP_TASK
kono
parents:
diff changeset
853 because it is almost always required. On processors with the SPE
kono
parents:
diff changeset
854 category, VX_SPE_TASK should be used instead to enable the SPE. */
kono
parents:
diff changeset
855 extern int __gnat_get_task_options (void);
kono
parents:
diff changeset
856
kono
parents:
diff changeset
857 int
kono
parents:
diff changeset
858 __gnat_get_task_options (void)
kono
parents:
diff changeset
859 {
kono
parents:
diff changeset
860 int options;
kono
parents:
diff changeset
861
kono
parents:
diff changeset
862 /* Get the options for the task creator */
kono
parents:
diff changeset
863 taskOptionsGet (taskIdSelf (), &options);
kono
parents:
diff changeset
864
kono
parents:
diff changeset
865 /* Force VX_FP_TASK or VX_SPE_TASK as needed */
kono
parents:
diff changeset
866 #if defined (__SPE__)
kono
parents:
diff changeset
867 options |= VX_SPE_TASK;
kono
parents:
diff changeset
868 #else
kono
parents:
diff changeset
869 options |= VX_FP_TASK;
kono
parents:
diff changeset
870 #endif
kono
parents:
diff changeset
871
kono
parents:
diff changeset
872 /* Mask those bits that are not under user control */
kono
parents:
diff changeset
873 #ifdef VX_USR_TASK_OPTIONS
kono
parents:
diff changeset
874 /* O810-007, TSR 00043679:
kono
parents:
diff changeset
875 Workaround a bug in Vx-7 where VX_DEALLOC_TCB == VX_PRIVATE_UMASK and:
kono
parents:
diff changeset
876 - VX_DEALLOC_TCB is an internal option not to be used by users
kono
parents:
diff changeset
877 - VX_PRIVATE_UMASK as a user-definable option
kono
parents:
diff changeset
878 This leads to VX_USR_TASK_OPTIONS allowing 0x8000 as VX_PRIVATE_UMASK but
kono
parents:
diff changeset
879 taskCreate refusing this option (VX_DEALLOC_TCB is not allowed)
kono
parents:
diff changeset
880
kono
parents:
diff changeset
881 Note that the same error occurs in both RTP and Kernel mode, but
kono
parents:
diff changeset
882 VX_DEALLOC_TCB is not defined in the RTP headers, so we need to
kono
parents:
diff changeset
883 explicitely check if VX_PRIVATE_UMASK has value 0x8000
kono
parents:
diff changeset
884 */
kono
parents:
diff changeset
885 # if defined (VX_PRIVATE_UMASK) && (0x8000 == VX_PRIVATE_UMASK)
kono
parents:
diff changeset
886 options &= ~VX_PRIVATE_UMASK;
kono
parents:
diff changeset
887 # endif
kono
parents:
diff changeset
888 options &= VX_USR_TASK_OPTIONS;
kono
parents:
diff changeset
889 #endif
kono
parents:
diff changeset
890 return options;
kono
parents:
diff changeset
891 }
kono
parents:
diff changeset
892
kono
parents:
diff changeset
893 #endif
kono
parents:
diff changeset
894
kono
parents:
diff changeset
895 int
kono
parents:
diff changeset
896 __gnat_is_file_not_found_error (int errno_val) {
kono
parents:
diff changeset
897 switch (errno_val) {
kono
parents:
diff changeset
898 case ENOENT:
kono
parents:
diff changeset
899 #ifdef __vxworks
kono
parents:
diff changeset
900 /* In the case of VxWorks, we also have to take into account various
kono
parents:
diff changeset
901 * filesystem-specific variants of this error.
kono
parents:
diff changeset
902 */
kono
parents:
diff changeset
903 #if ! defined (VTHREADS) && (_WRS_VXWORKS_MAJOR < 7)
kono
parents:
diff changeset
904 case S_dosFsLib_FILE_NOT_FOUND:
kono
parents:
diff changeset
905 #endif
kono
parents:
diff changeset
906 #if ! defined (__RTP__) && (! defined (VTHREADS) || defined (__VXWORKSMILS__))
kono
parents:
diff changeset
907 case S_nfsLib_NFSERR_NOENT:
kono
parents:
diff changeset
908 #endif
kono
parents:
diff changeset
909 #if defined (__RTP__)
kono
parents:
diff changeset
910 /* An RTP can return an NFS file not found, and the NFS bits must
kono
parents:
diff changeset
911 first be masked on to check the errno. */
kono
parents:
diff changeset
912 case M_nfsStat | ENOENT:
kono
parents:
diff changeset
913 #endif
kono
parents:
diff changeset
914 #endif
kono
parents:
diff changeset
915 return 1;
kono
parents:
diff changeset
916
kono
parents:
diff changeset
917 default:
kono
parents:
diff changeset
918 return 0;
kono
parents:
diff changeset
919 }
kono
parents:
diff changeset
920 }
kono
parents:
diff changeset
921
kono
parents:
diff changeset
922 #if defined (__linux__)
kono
parents:
diff changeset
923
kono
parents:
diff changeset
924 /* Note well: If this code is modified, it should be tested by hand,
kono
parents:
diff changeset
925 because automated testing doesn't exercise it.
kono
parents:
diff changeset
926 */
kono
parents:
diff changeset
927
kono
parents:
diff changeset
928 /* HAVE_CAPABILITY is supposed to be defined if sys/capability.h exists on the
kono
parents:
diff changeset
929 system where this is being compiled. If this macro is defined, we #include
kono
parents:
diff changeset
930 the header. Otherwise we have the relevant declarations textually here.
kono
parents:
diff changeset
931 */
kono
parents:
diff changeset
932
kono
parents:
diff changeset
933 #if defined (HAVE_CAPABILITY)
kono
parents:
diff changeset
934 #include <sys/capability.h>
kono
parents:
diff changeset
935 #else
kono
parents:
diff changeset
936
kono
parents:
diff changeset
937 /* HAVE_CAPABILITY is not defined, so sys/capability.h does might not exist. */
kono
parents:
diff changeset
938
kono
parents:
diff changeset
939 typedef struct _cap_struct *cap_t;
kono
parents:
diff changeset
940 typedef enum {
kono
parents:
diff changeset
941 CAP_CLEAR=0,
kono
parents:
diff changeset
942 CAP_SET=1
kono
parents:
diff changeset
943 } cap_flag_value_t;
kono
parents:
diff changeset
944 #define CAP_SYS_NICE 23
kono
parents:
diff changeset
945 typedef enum {
kono
parents:
diff changeset
946 CAP_EFFECTIVE=0, /* Specifies the effective flag */
kono
parents:
diff changeset
947 CAP_PERMITTED=1, /* Specifies the permitted flag */
kono
parents:
diff changeset
948 CAP_INHERITABLE=2 /* Specifies the inheritable flag */
kono
parents:
diff changeset
949 } cap_flag_t;
kono
parents:
diff changeset
950
kono
parents:
diff changeset
951 typedef int cap_value_t;
kono
parents:
diff changeset
952
kono
parents:
diff changeset
953 extern cap_t cap_get_proc(void);
kono
parents:
diff changeset
954 extern int cap_get_flag(cap_t, cap_value_t, cap_flag_t, cap_flag_value_t *);
kono
parents:
diff changeset
955 extern int cap_free(void *);
kono
parents:
diff changeset
956
kono
parents:
diff changeset
957 #endif
kono
parents:
diff changeset
958
kono
parents:
diff changeset
959 /* __gnat_has_cap_sys_nice returns 1 if the current process has the
kono
parents:
diff changeset
960 CAP_SYS_NICE capability. This capability is necessary to use the
kono
parents:
diff changeset
961 Ceiling_Locking policy. Returns 0 otherwise. Note that this is
kono
parents:
diff changeset
962 defined only for Linux.
kono
parents:
diff changeset
963 */
kono
parents:
diff changeset
964
kono
parents:
diff changeset
965 /* Define these as weak symbols, so if support for capabilities is not present,
kono
parents:
diff changeset
966 programs can still link. On Ubuntu, support for capabilities can be
kono
parents:
diff changeset
967 installed via "sudo apt-get --assume-yes install libcap-dev".
kono
parents:
diff changeset
968 In addition, the user must link with "-lcap", or else these
kono
parents:
diff changeset
969 symbols will be 0, and __gnat_has_cap_sys_nice will return 0.
kono
parents:
diff changeset
970 */
kono
parents:
diff changeset
971
kono
parents:
diff changeset
972 static cap_t cap_get_proc_weak(void)
kono
parents:
diff changeset
973 __attribute__ ((weakref ("cap_get_proc")));
kono
parents:
diff changeset
974 static int cap_get_flag_weak(cap_t, cap_value_t, cap_flag_t, cap_flag_value_t *)
kono
parents:
diff changeset
975 __attribute__ ((weakref ("cap_get_flag")));
kono
parents:
diff changeset
976 static int cap_free_weak(void *)
kono
parents:
diff changeset
977 __attribute__ ((weakref ("cap_free")));
kono
parents:
diff changeset
978
kono
parents:
diff changeset
979 int
kono
parents:
diff changeset
980 __gnat_has_cap_sys_nice () {
kono
parents:
diff changeset
981 /* If the address of cap_get_proc_weak is 0, this means support for
kono
parents:
diff changeset
982 capabilities is not present, so we return 0. */
kono
parents:
diff changeset
983 if (&cap_get_proc_weak == 0)
kono
parents:
diff changeset
984 return 0;
kono
parents:
diff changeset
985
kono
parents:
diff changeset
986 cap_t caps = cap_get_proc_weak();
kono
parents:
diff changeset
987 if (caps == NULL)
kono
parents:
diff changeset
988 return 0;
kono
parents:
diff changeset
989
kono
parents:
diff changeset
990 cap_flag_value_t value;
kono
parents:
diff changeset
991
kono
parents:
diff changeset
992 if (cap_get_flag_weak(caps, CAP_SYS_NICE, CAP_EFFECTIVE, &value) == -1)
kono
parents:
diff changeset
993 return 0;
kono
parents:
diff changeset
994
kono
parents:
diff changeset
995 if (cap_free_weak(caps) == -1)
kono
parents:
diff changeset
996 return 0;
kono
parents:
diff changeset
997
kono
parents:
diff changeset
998 if (value == CAP_SET)
kono
parents:
diff changeset
999 return 1;
kono
parents:
diff changeset
1000
kono
parents:
diff changeset
1001 return 0;
kono
parents:
diff changeset
1002 }
kono
parents:
diff changeset
1003 #endif
kono
parents:
diff changeset
1004
kono
parents:
diff changeset
1005 #ifdef __ANDROID__
kono
parents:
diff changeset
1006
kono
parents:
diff changeset
1007 /* Provide extern symbols for sig* as needed by the tasking run-time, instead
kono
parents:
diff changeset
1008 of static inline functions. */
kono
parents:
diff changeset
1009
kono
parents:
diff changeset
1010 #include <signal.h>
kono
parents:
diff changeset
1011
kono
parents:
diff changeset
1012 int
kono
parents:
diff changeset
1013 _sigismember (sigset_t *set, int signum)
kono
parents:
diff changeset
1014 {
kono
parents:
diff changeset
1015 return sigismember (set, signum);
kono
parents:
diff changeset
1016 }
kono
parents:
diff changeset
1017
kono
parents:
diff changeset
1018 int
kono
parents:
diff changeset
1019 _sigaddset (sigset_t *set, int signum)
kono
parents:
diff changeset
1020 {
kono
parents:
diff changeset
1021 return sigaddset (set, signum);
kono
parents:
diff changeset
1022 }
kono
parents:
diff changeset
1023
kono
parents:
diff changeset
1024 int
kono
parents:
diff changeset
1025 _sigdelset (sigset_t *set, int signum)
kono
parents:
diff changeset
1026 {
kono
parents:
diff changeset
1027 return sigdelset (set, signum);
kono
parents:
diff changeset
1028 }
kono
parents:
diff changeset
1029
kono
parents:
diff changeset
1030 int
kono
parents:
diff changeset
1031 _sigemptyset (sigset_t *set)
kono
parents:
diff changeset
1032 {
kono
parents:
diff changeset
1033 return sigemptyset (set);
kono
parents:
diff changeset
1034 }
kono
parents:
diff changeset
1035
kono
parents:
diff changeset
1036 int
kono
parents:
diff changeset
1037 _sigfillset (sigset_t *set)
kono
parents:
diff changeset
1038 {
kono
parents:
diff changeset
1039 return sigfillset (set);
kono
parents:
diff changeset
1040 }
kono
parents:
diff changeset
1041
kono
parents:
diff changeset
1042 #include <unistd.h>
kono
parents:
diff changeset
1043 int
kono
parents:
diff changeset
1044 _getpagesize (void)
kono
parents:
diff changeset
1045 {
kono
parents:
diff changeset
1046 return getpagesize ();
kono
parents:
diff changeset
1047 }
kono
parents:
diff changeset
1048 #endif