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