1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
|
/****************************************************************************/
/* */
/* GNAT COMPILER COMPONENTS */
/* */
/* A - A D A I N T */
/* */
/* $Revision: 1.78 $ */
/* */
/* C Implementation File */
/* */
/* Copyright (C) 1992-1997, Free Software Foundation, Inc. */
/* */
/* GNAT is free software; you can redistribute it and/or modify it under */
/* terms of the GNU General Public License as published by the Free Soft- */
/* ware Foundation; either version 2, or (at your option) any later ver- */
/* sion. GNAT is distributed in the hope that it will be useful, but WITH- */
/* OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY */
/* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License */
/* for more details. You should have received a copy of the GNU General */
/* Public License distributed with GNAT; see file COPYING. If not, write */
/* to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, */
/* MA 02111-1307, USA. */
/* */
/* As a special exception, if you link this file with other files to */
/* produce an executable, this file does not by itself cause the resulting */
/* executable to be covered by the GNU General Public License. This except- */
/* ion does not however invalidate any other reasons why the executable */
/* file might be covered by the GNU Public License. */
/* */
/* GNAT was originally developed by the GNAT team at New York University. */
/* It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). */
/* */
/****************************************************************************/
/* This file contains those routines named by Import pragmas in package */
/* GNAT.OS_Lib. Many of the subprograms in OS_Lib import standard library */
/* calls directly. This file contains all other routines. */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include "config.h" /* this may define MSDOS */
#if defined (__EMX__) || defined (MSDOS) || defined (_WIN32)
#include <process.h>
#include <string.h>
#endif
#if defined (__EMX__)
#include <sys/emx.h>
#endif
#include "a-adaint.h"
/* Define symbols O_BINARY and O_TEXT as harmless zeroes if they are not
defined in the current system. On DOS-like systems these flags control
whether the file is opened/created in text-translation mode (CR/LF in
external file mapped to LF in internal file), but in Unix-like systems,
no text translation is required, so these flags have no effect.
*/
#if defined (__EMX__)
#include <os2.h>
#endif
#if defined (MSDOS)
#include <dos.h>
#endif
#ifndef O_BINARY
#define O_BINARY 0
#endif
#ifndef O_TEXT
#define O_TEXT 0
#endif
extern char *getenv ();
#ifndef EXECUTABLE_SUFFIX
#define EXECUTABLE_SUFFIX ""
#endif
#ifndef OBJECT_SUFFIX
#define OBJECT_SUFFIX ".o"
#endif
#ifndef PATH_SEPARATOR
#define PATH_SEPARATOR ':'
#endif
#ifndef DIR_SEPARATOR
#define DIR_SEPARATOR '/'
#endif
void
to_gm_time (p_time, p_year, p_month, p_day, p_hours, p_mins, p_secs)
time_t *p_time;
int *p_year, *p_month, *p_day, *p_hours, *p_mins, *p_secs;
{
struct tm *res = gmtime (p_time);
if (res) {
*p_year = res->tm_year;
*p_month = res->tm_mon;
*p_day = res->tm_mday;
*p_hours = res->tm_hour;
*p_mins = res->tm_min;
*p_secs = res->tm_sec;
} else {
*p_year = *p_month = *p_day = *p_hours = *p_mins = *p_secs = 0;
}
}
/* Return the maximum file name length. */
int
Get_Maximum_File_Name_Length ()
{
#if defined(MSDOS)
return 8;
#elif defined (VMS)
return 39;
#else
return -1;
#endif
}
/* Return the default switch character. */
char
Get_Switch_Character ()
{
/* Under MSDOS, the switch character is not normally a hyphen, but this is
the convention DJGPP uses. Similarly under OS2, the switch character is
not normally a hypen, but this is the convention EMX uses.
*/
return '-';
}
/* Return nonzero if file names are case sensitive. */
int
Get_File_Names_Case_Sensitive ()
{
#if defined (__EMX__) || defined (MSDOS) || defined (VMS) || defined(WINNT)
return 0;
#else
return 1;
#endif
}
char
Get_Default_Identifier_Character_Set ()
{
#if defined (__EMX__) || defined (MSDOS)
return 'p';
#else
return '1';
#endif
}
char
Get_Dirsep_Char ()
{
return DIR_SEPARATOR;
}
/* Return the path separator. */
char
Get_Pathsep_Char ()
{
return PATH_SEPARATOR;
}
/* Return the suffix for object files */
void
get_object_suffix_ptr (len, value)
int *len;
char **value;
{
*value = OBJECT_SUFFIX;
if (!*value)
*len = 0;
else
*len = strlen (*value);
return;
}
/* Return the suffix for executable files */
void
get_executable_suffix_ptr (len, value)
int *len;
char **value;
{
*value = EXECUTABLE_SUFFIX;
if (!*value)
*len = 0;
else
*len = strlen (*value);
return;
}
/* Return the suffix for debuggable files. Usually this is the same as the
executable extension. */
void
get_debuggable_suffix_ptr (len, value)
int *len;
char **value;
{
#ifndef MSDOS
*value = EXECUTABLE_SUFFIX;
#else
/* On DOS, the extensionless COFF file is what gdb likes. */
*value = "";
#endif
if (!*value)
*len = 0;
else
*len = strlen (*value);
return;
}
int
open_read (path, fmode)
char *path;
int fmode;
{
int fd;
#ifdef VMS
/* Optional arguments increase read performance, may be applicable to
write too, but not sure. */
if (fmode)
fd = open (path, O_RDONLY | O_TEXT, 0444,
"mbc=16", "deq=64", "fop=tef");
else
fd = open (path, O_RDONLY | O_BINARY, 0444,
"mbc=16", "deq=64", "fop=tef");
#else
if (fmode)
#ifdef __vxworks
fd = open (path, O_RDONLY | O_TEXT, 0444);
#else
fd = open (path, O_RDONLY | O_TEXT);
#endif
else
#ifdef __vxworks
fd = open (path, O_RDONLY | O_BINARY, 0444);
#else
fd = open (path, O_RDONLY | O_BINARY);
#endif
#endif
return fd < 0 ? -1 : fd;
}
#if defined (__EMX__)
#define PERM (S_IREAD | S_IWRITE)
#else
#define PERM (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
#endif
int
open_rw (path, fmode)
char *path;
int fmode;
{
int fd;
if (fmode)
fd = open (path, O_RDWR | O_TEXT, PERM);
else
fd = open (path, O_RDWR | O_BINARY, PERM);
return fd < 0 ? -1 : fd;
}
int
open_create (path, fmode)
char *path;
int fmode;
{
int fd;
if (fmode)
fd = open (path, O_WRONLY | O_CREAT | O_TRUNC | O_TEXT, PERM);
else
fd = open (path, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, PERM);
return fd < 0 ? -1 : fd;
}
/* Open a new file. Return error (-1) if the file already exists. */
int
open_new (path, fmode)
char *path;
int fmode;
{
int fd;
if (fmode)
fd = open (path, O_WRONLY | O_CREAT | O_EXCL | O_TEXT, PERM);
else
fd = open (path, O_WRONLY | O_CREAT | O_EXCL | O_BINARY, PERM);
return fd < 0 ? -1 : fd;
}
/* Return the number of bytes in the specified file. */
long
file_length (fd)
int fd;
{
int ret;
struct stat statbuf;
ret = fstat (fd, &statbuf);
if (ret || !S_ISREG (statbuf.st_mode))
return 0;
return (statbuf.st_size);
}
/* Return a GNAT time stamp given a file name. */
time_t
file_time_name (name)
char *name;
{
struct stat statbuf;
#if defined (__EMX__) || defined (MSDOS)
int fd = open (name, O_RDONLY | O_BINARY);
time_t ret = file_time_fd (fd);
close (fd);
return ret;
#else
int ret = stat (name, &statbuf);
#ifdef VMS
/* VMS has file versioning */
return statbuf.st_ctime;
#else
return statbuf.st_mtime;
#endif
#endif
}
/* Return a GNAT time stamp given a file descriptor. */
time_t
file_time_fd (fd)
int fd;
{
/* The following workaround code is due to the fact that under EMX and DJGPP
fstat attempts to convert time values to GMT rather than keep the actual
OS timestamp of the file. By using the OS2/DOS functions directly the GNAT
timestamp are independent of this behavior, which is desired to facilitate
the distribution of GNAT compiled libraries. */
#if defined (__EMX__) || defined (MSDOS)
#ifdef __EMX__
FILESTATUS fs;
int ret = DosQueryFileInfo (fd, 1, (unsigned char *) &fs,
sizeof (FILESTATUS));
unsigned file_year = fs.fdateLastWrite.year;
unsigned file_month = fs.fdateLastWrite.month;
unsigned file_day = fs.fdateLastWrite.day;
unsigned file_hour = fs.ftimeLastWrite.hours;
unsigned file_min = fs.ftimeLastWrite.minutes;
unsigned file_tsec = fs.ftimeLastWrite.twosecs;
#else
struct ftime fs;
int ret = getftime (fd, &fs);
unsigned file_year = fs.ft_year;
unsigned file_month = fs.ft_month;
unsigned file_day = fs.ft_day;
unsigned file_hour = fs.ft_hour;
unsigned file_min = fs.ft_min;
unsigned file_tsec = fs.ft_tsec;
#endif
/* Calculate the seconds since epoch from the time components. First count
the whole days passed. The value for years returned by the DOS and OS2
functions count years from 1980, so to compensate for the UNIX epoch which
begins in 1970 start with 10 years worth of days and add days for each
four year period since then. */
time_t tot_secs;
int cum_days [12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
int days_passed = 3652 + (file_year / 4) * 1461;
int years_since_leap = file_year % 4;
if (years_since_leap == 1) days_passed += 366;
else if (years_since_leap == 2) days_passed += 731;
else if (years_since_leap == 3) days_passed += 1096;
if (file_year > 20) days_passed -= 1;
days_passed += cum_days [file_month - 1];
if (years_since_leap == 0 && file_year != 20
&& file_month > 2) days_passed++;
days_passed += file_day - 1;
/* OK - have whole days. Multiply -- then add in other parts. */
tot_secs = days_passed * 86400;
tot_secs += file_hour * 3600;
tot_secs += file_min * 60;
tot_secs += file_tsec * 2;
return tot_secs;
#else
struct stat statbuf;
int ret = fstat (fd, &statbuf);
#ifdef VMS
/* VMS has file versioning */
return statbuf.st_ctime;
#else
return statbuf.st_mtime;
#endif
#endif
}
void
get_env_value_ptr (name, len, value)
char *name;
int *len;
char **value;
{
*value = getenv (name);
if (!*value)
*len = 0;
else
*len = strlen (*value);
return;
}
int
file_exists (name)
char *name;
{
struct stat statbuf;
return !stat (name, &statbuf);
}
int
is_regular_file (name)
char *name;
{
int ret;
struct stat statbuf;
ret = stat (name, &statbuf);
return (!ret && S_ISREG (statbuf.st_mode));
}
int
is_directory (name)
char *name;
{
int ret;
struct stat statbuf;
ret = stat (name, &statbuf);
return (!ret && S_ISDIR (statbuf.st_mode));
}
int
is_writable_file (name)
char *name;
{
int ret;
int mode;
struct stat statbuf;
ret = stat (name, &statbuf);
mode = statbuf.st_mode & S_IWUSR;
return (!ret && mode);
}
#ifdef VMS
/* Defined in VMS header files */
#define fork() (decc$$alloc_vfork_blocks() >= 0 ? \
lib$get_current_invo_context(decc$$get_vfork_jmpbuf()) : -1)
#endif
int
portable_spawn (args)
char *args[];
{
int status;
int finished;
int pid;
#if defined (__EMX__) || defined (MSDOS) || defined (_WIN32)
if (spawnvp (P_WAIT, args [0], args) != 0)
return (4);
#else
pid = fork ();
if (pid == -1)
return (4);
if (pid == 0) {
/* The child */
execv (args [0], args);
return (4);
}
/* The parent */
#ifdef VMS
/* Wait doesn't do the right thing on VMS */
finished = waitpid (-1, &status, 0);
#else
finished = wait (&status);
#endif
if (finished != pid || status & 0xffff)
return 4;
#endif
return 0;
}
int
portable_no_block_spawn (args)
char *args[];
{
int pid = 0;
#if defined (__EMX__) || defined (MSDOS) || defined (_WIN32)
/* ??? For PC machines I (Franco) don't know the system calls to
implement this routine. So I'll fake it as follows. This routine
will behave exactly like the blocking portable_spawn and will
systematically return a pid of 0 unless the spawned task did not
complete successfully, in which case we return a pid of -1. To
synchronize with this the portable_wait below systematically
returns a pid of 0 and reports that the subprocess terminated
successfully. */
if (spawnvp (P_WAIT, args [0], args) != 0)
return (-1);
#else
pid = fork ();
if (pid == 0) {
/* The child */
execv (args [0], args);
return (-1);
}
#endif
return pid;
}
int
portable_wait (process_status)
int *process_status;
{
int status = 0;
int pid = 0;
#if defined (__EMX__) || defined (MSDOS) || defined (_WIN32)
/* ??? See corresponding comment in portable_no_block_spawn. */
#else
#ifdef VMS
/* Wait doesn't do the right thing on VMS */
pid = waitpid (-1, &status, 0);
#else
pid = wait (&status);
#endif
status = status & 0xffff;
#endif
*process_status = status;
return pid;
}
void
os_exit (status)
int status;
{
#ifdef VMS
/* Exit without changing 0 to 1 */
__posix_exit (status);
#else
exit (status);
#endif
}
/* Locate a regular file, give a Path value */
char *
locate_regular_file (file_name, path_val)
char *file_name;
char *path_val;
{
int len;
char *ptr;
/* Handle absolute pathnames. */
for (ptr = file_name; *ptr && *ptr != '/' && *ptr != DIR_SEPARATOR; ptr++)
;
if (*ptr != 0
#if defined(__EMX__) || defined(MSDOS) || defined(WINNT)
|| isalpha (file_name [0]) && file_name [1] == ':'
#endif
)
{
if (is_regular_file (file_name))
return xstrdup (file_name);
return 0;
}
if (path_val == 0)
return 0;
{
/* The result has to be smaller than path_val + file_name. */
char *file_path = alloca (strlen (path_val) + strlen (file_name) + 1);
char pathsep_char = Get_Pathsep_Char ();
for (;;)
{
for (; *path_val == pathsep_char ; path_val++)
;
if (*path_val == 0)
return 0;
for (ptr = file_path; *path_val && *path_val != pathsep_char; )
*ptr++ = *path_val++;
ptr--;
if (*ptr != '/' && *ptr != DIR_SEPARATOR)
*++ptr = DIR_SEPARATOR;
strcpy (++ptr, file_name);
if (is_regular_file (file_path))
return xstrdup (file_path);
}
}
return 0;
}
/* Locate an executable given a Path argument. This routine is only used by
gnatbl and should not be used otherwise. Use locate_exec_on_path
instead. */
char *
locate_exec (exec_name, path_val)
char *exec_name;
char *path_val;
{
if (!strstr (exec_name, EXECUTABLE_SUFFIX))
{
char *full_exec_name
= alloca (strlen(exec_name) + strlen(EXECUTABLE_SUFFIX) + 1);
strcpy (full_exec_name, exec_name);
strcat (full_exec_name, EXECUTABLE_SUFFIX);
return locate_regular_file (full_exec_name, path_val);
}
else
return locate_regular_file (exec_name, path_val);
}
/* Locate an executable using the Systems default PATH */
char *
locate_exec_on_path (exec_name)
char *exec_name;
{
#ifdef VMS
char *path_val = TO_NORMAL_FILE_SPEC (getenv ("VAXC$PATH"));
#else
char *path_val = getenv ("PATH");
#endif
char *apath_val = alloca (strlen (path_val) + 1);
strcpy (apath_val, path_val);
return locate_exec (exec_name, apath_val);
}
#ifdef VMS
void
adjust_os_resource_limits ()
{
sys$adjwsl (131072, 0);
}
#else
void
adjust_os_resource_limits () {;}
#endif
|