1------------------------------------------------------------------------------ 2-- -- 3-- GNAT COMPILER COMPONENTS -- 4-- -- 5-- S Y S T E M . O S _ L I B -- 6-- -- 7-- S p e c -- 8-- -- 9-- Copyright (C) 1995-2020, 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-- Operating system interface facilities 33 34-- This package contains types and procedures for interfacing to the 35-- underlying OS. It is used by the GNAT compiler and by tools associated 36-- with the GNAT compiler, and therefore works for the various operating 37-- systems to which GNAT has been ported. This package will undoubtedly grow 38-- as new services are needed by various tools. 39 40-- This package tends to use fairly low-level Ada in order to not bring in 41-- large portions of the RTL. For example, functions return access to string 42-- as part of avoiding functions returning unconstrained types. 43 44-- Except where specifically noted, these routines are portable across all 45-- GNAT implementations on all supported operating systems. 46 47-- Note: this package is in the System hierarchy so that it can be directly 48-- be used by other predefined packages. User access to this package is via 49-- a renaming of this package in GNAT.OS_Lib (file g-os_lib.ads). 50 51pragma Compiler_Unit_Warning; 52 53with System; 54with System.Strings; 55 56package System.OS_Lib is 57 pragma Preelaborate; 58 59 ----------------------- 60 -- String Operations -- 61 ----------------------- 62 63 -- These are reexported from package Strings (which was introduced to 64 -- avoid different packages declaring different types unnecessarily). 65 -- See package System.Strings for details. 66 67 subtype String_Access is Strings.String_Access; 68 69 function "=" (Left : String_Access; Right : String_Access) return Boolean 70 renames Strings."="; 71 72 procedure Free (X : in out String_Access) renames Strings.Free; 73 74 subtype String_List is Strings.String_List; 75 76 function "=" (Left : String_List; Right : String_List) return Boolean 77 renames Strings."="; 78 79 function "&" (Left : String_Access; Right : String_Access) 80 return String_List renames Strings."&"; 81 function "&" (Left : String_Access; Right : String_List) 82 return String_List renames Strings."&"; 83 function "&" (Left : String_List; Right : String_Access) 84 return String_List renames Strings."&"; 85 function "&" (Left : String_List; Right : String_List) 86 return String_List renames Strings."&"; 87 88 subtype String_List_Access is Strings.String_List_Access; 89 90 function "=" 91 (Left : String_List_Access; 92 Right : String_List_Access) return Boolean renames Strings."="; 93 94 procedure Free (Arg : in out String_List_Access) renames Strings.Free; 95 96 --------------------- 97 -- Time/Date Stuff -- 98 --------------------- 99 100 type OS_Time is private; 101 -- The OS's notion of time is represented by the private type OS_Time. This 102 -- is the type returned by the File_Time_Stamp functions to obtain the time 103 -- stamp of a specified file. Functions and a procedure (modeled after the 104 -- similar subprograms in package Calendar) are provided for extracting 105 -- information from a value of this type. Although these are called GM, the 106 -- intention in the case of time stamps is not that they provide GMT times 107 -- in all cases but rather the actual (time-zone independent) time stamp of 108 -- the file (of course in Unix systems, this *is* in GMT form). 109 110 Invalid_Time : constant OS_Time; 111 -- A special unique value used to flag an invalid time stamp value 112 113 function "<" (X : OS_Time; Y : OS_Time) return Boolean; 114 function ">" (X : OS_Time; Y : OS_Time) return Boolean; 115 function ">=" (X : OS_Time; Y : OS_Time) return Boolean; 116 function "<=" (X : OS_Time; Y : OS_Time) return Boolean; 117 -- Basic comparison operators on OS_Time with obvious meanings. Note that 118 -- these have Intrinsic convention, so for example it is not permissible 119 -- to create accesses to any of these functions. 120 121 subtype Year_Type is Integer range 1900 .. 2099; 122 subtype Month_Type is Integer range 1 .. 12; 123 subtype Day_Type is Integer range 1 .. 31; 124 subtype Hour_Type is Integer range 0 .. 23; 125 subtype Minute_Type is Integer range 0 .. 59; 126 subtype Second_Type is Integer range 0 .. 59; 127 -- Declarations similar to those in Calendar, breaking down the time 128 129 function Current_Time return OS_Time; 130 -- Return the system clock value as OS_Time 131 132 function Current_Time_String return String; 133 -- Returns current local time in the form YYYY-MM-DD HH:MM:SS. The result 134 -- has bounds 1 .. 19. 135 136 function GM_Year (Date : OS_Time) return Year_Type; 137 function GM_Month (Date : OS_Time) return Month_Type; 138 function GM_Day (Date : OS_Time) return Day_Type; 139 function GM_Hour (Date : OS_Time) return Hour_Type; 140 function GM_Minute (Date : OS_Time) return Minute_Type; 141 function GM_Second (Date : OS_Time) return Second_Type; 142 -- Functions to extract information from OS_Time value in GMT form 143 144 procedure GM_Split 145 (Date : OS_Time; 146 Year : out Year_Type; 147 Month : out Month_Type; 148 Day : out Day_Type; 149 Hour : out Hour_Type; 150 Minute : out Minute_Type; 151 Second : out Second_Type); 152 -- Analogous to the Split routine in Ada.Calendar, takes an OS_Time and 153 -- provides a representation of it as a set of component parts, to be 154 -- interpreted as a date point in UTC. 155 156 function GM_Time_Of 157 (Year : Year_Type; 158 Month : Month_Type; 159 Day : Day_Type; 160 Hour : Hour_Type; 161 Minute : Minute_Type; 162 Second : Second_Type) return OS_Time; 163 -- Analogous to the Time_Of routine in Ada.Calendar, takes a set of time 164 -- component parts to be interpreted in the local time zone, and returns 165 -- an OS_Time. Returns Invalid_Time if the creation fails. 166 167 subtype time_t is Long_Integer; 168 -- C time_t type of the time representation 169 170 function To_C (Time : OS_Time) return time_t; 171 -- Convert OS_Time to C time_t type 172 173 function To_Ada (Time : time_t) return OS_Time; 174 -- Convert C time_t type to OS_Time 175 176 ---------------- 177 -- File Stuff -- 178 ---------------- 179 180 -- These routines give access to the open/creat/close/read/write level of 181 -- I/O routines in the typical C library (these functions are not part of 182 -- the ANSI C standard, but are typically available in all systems). See 183 -- also package Interfaces.C_Streams for access to the stream level 184 -- routines. 185 186 -- Note on file names. If a file name is passed as type String in any of 187 -- the following specifications, then the name is a normal Ada string and 188 -- need not be NUL-terminated. However, a trailing NUL character is 189 -- permitted, and will be ignored (more accurately, the NUL and any 190 -- characters that follow it will be ignored). 191 192 type File_Descriptor is new Integer; 193 -- Corresponds to the int file handle values used in the C routines 194 195 Standin : constant File_Descriptor := 0; 196 Standout : constant File_Descriptor := 1; 197 Standerr : constant File_Descriptor := 2; 198 -- File descriptors for standard input output files 199 200 Invalid_FD : constant File_Descriptor := -1; 201 -- File descriptor returned when error in opening/creating file 202 203 Null_FD : constant File_Descriptor := -2; 204 -- Uninitialized file descriptor 205 206 procedure Close (FD : File_Descriptor; Status : out Boolean); 207 -- Close file referenced by FD. Status is False if the underlying service 208 -- failed. Reasons for failure include: disk full, disk quotas exceeded 209 -- and invalid file descriptor (the file may have been closed twice). 210 211 procedure Close (FD : File_Descriptor); 212 -- Close file referenced by FD. This form is used when the caller wants to 213 -- ignore any possible error (see above for error cases). 214 215 type Copy_Mode is 216 (Copy, 217 -- Copy the file. It is an error if the target file already exists. The 218 -- time stamps and other file attributes are preserved in the copy. 219 220 Overwrite, 221 -- If the target file exists, the file is replaced otherwise the file 222 -- is just copied. The time stamps and other file attributes are 223 -- preserved in the copy. 224 225 Append); 226 -- If the target file exists, the contents of the source file is 227 -- appended at the end. Otherwise the source file is just copied. The 228 -- time stamps and other file attributes are preserved if the 229 -- destination file does not exist. 230 231 type Attribute is 232 (Time_Stamps, 233 -- Copy time stamps from source file to target file. All other 234 -- attributes are set to normal default values for file creation. 235 236 Full, 237 -- All attributes are copied from the source file to the target file. 238 -- This includes the timestamps, and for example also includes 239 -- read/write/execute attributes in Unix systems. 240 241 None); 242 -- No attributes are copied. All attributes including the time stamp 243 -- values are set to normal default values for file creation. 244 245 -- Note: The default is Time_Stamps, which corresponds to the normal 246 -- default on Windows style systems. Full corresponds to the typical 247 -- effect of "cp -p" on Unix systems, and None corresponds to the typical 248 -- effect of "cp" on Unix systems. 249 250 -- Note: Time_Stamps and Full are not supported on VxWorks 5 251 252 procedure Copy_File 253 (Name : String; 254 Pathname : String; 255 Success : out Boolean; 256 Mode : Copy_Mode := Copy; 257 Preserve : Attribute := Time_Stamps); 258 -- Copy a file. Name must designate a single file (no wildcards allowed). 259 -- Pathname can be a filename or directory name. In the latter case Name 260 -- is copied into the directory preserving the same file name. Mode 261 -- defines the kind of copy, see above with the default being a normal 262 -- copy in which the target file must not already exist. Success is set to 263 -- True or False indicating if the copy is successful (depending on the 264 -- specified Mode). 265 266 procedure Copy_File_Attributes 267 (From : String; 268 To : String; 269 Success : out Boolean; 270 Copy_Timestamp : Boolean := True; 271 Copy_Permissions : Boolean := True); 272 -- Copy some of the file attributes from one file to another. Both files 273 -- must exist, or Success is set to False. 274 275 procedure Copy_Time_Stamps 276 (Source : String; 277 Dest : String; 278 Success : out Boolean); 279 -- Copy Source file time stamps (last modification and last access time 280 -- stamps) to Dest file. Source and Dest must be valid filenames, 281 -- furthermore Dest must be writable. Success will be set to True if the 282 -- operation was successful and False otherwise. 283 -- 284 -- Note: this procedure is not supported on VxWorks 5. On this platform, 285 -- Success is always set to False. 286 287 type Mode is (Binary, Text); 288 for Mode'Size use Integer'Size; 289 for Mode use (Binary => 0, Text => 1); 290 -- Used in all the Open and Create calls to specify if the file is to be 291 -- opened in binary mode or text mode. In systems like Unix, this has no 292 -- effect, but in systems capable of text mode translation, the use of 293 -- Text as the mode parameter causes the system to do CR/LF translation 294 -- and also to recognize the DOS end of file character on input. The use 295 -- of Text where appropriate allows programs to take a portable Unix view 296 -- of DOS-format files and process them appropriately. 297 298 function Create_File 299 (Name : String; 300 Fmode : Mode) return File_Descriptor; 301 -- Creates new file with given name for writing, returning file descriptor 302 -- for subsequent use in Write calls. If the file already exists, it is 303 -- overwritten. File descriptor returned is Invalid_FD if file cannot be 304 -- successfully created. 305 306 function Create_New_File 307 (Name : String; 308 Fmode : Mode) return File_Descriptor; 309 -- Create new file with given name for writing, returning file descriptor 310 -- for subsequent use in Write calls. This differs from Create_File in 311 -- that it fails if the file already exists. File descriptor returned is 312 -- Invalid_FD if the file exists or cannot be created. 313 314 function Create_Output_Text_File (Name : String) return File_Descriptor; 315 -- Creates new text file with given name suitable to redirect standard 316 -- output, returning file descriptor. File descriptor returned is 317 -- Invalid_FD if file cannot be successfully created. 318 319 Temp_File_Len : constant Integer := 12; 320 -- Length of name returned by Create_Temp_File call (GNAT-XXXXXX & NUL) 321 322 subtype Temp_File_Name is String (1 .. Temp_File_Len); 323 -- String subtype set by Create_Temp_File 324 325 procedure Create_Temp_File 326 (FD : out File_Descriptor; 327 Name : out Temp_File_Name); 328 -- Create and open for writing a temporary file in the current working 329 -- directory. The name of the file and the File Descriptor are returned. 330 -- The File Descriptor returned is Invalid_FD in the case of failure. No 331 -- mode parameter is provided. Since this is a temporary file, there is no 332 -- point in doing text translation on it. 333 -- 334 -- On some operating systems, the maximum number of temp files that can be 335 -- created with this procedure may be limited. When the maximum is reached, 336 -- this procedure returns Invalid_FD. On some operating systems, there may 337 -- be a race condition between processes trying to create temp files at the 338 -- same time in the same directory using this procedure. 339 340 procedure Create_Temp_File 341 (FD : out File_Descriptor; 342 Name : out String_Access); 343 -- Create and open for writing a temporary file in the current working 344 -- directory. The name of the file and the File Descriptor are returned. 345 -- It is the responsibility of the caller to deallocate the access value 346 -- returned in Name. 347 -- 348 -- The file is opened in binary mode (no text translation). 349 -- 350 -- This procedure will always succeed if the current working directory is 351 -- writable. If the current working directory is not writable, then 352 -- Invalid_FD is returned for the file descriptor and null for the Name. 353 -- There is no race condition problem between processes trying to create 354 -- temp files at the same time in the same directory. 355 356 procedure Create_Temp_Output_File 357 (FD : out File_Descriptor; 358 Name : out String_Access); 359 -- Create and open for writing a temporary file in the current working 360 -- directory suitable to redirect standard output. The name of the file and 361 -- the File Descriptor are returned. It is the responsibility of the caller 362 -- to deallocate the access value returned in Name. 363 -- 364 -- The file is opened in text mode 365 -- 366 -- This procedure will always succeed if the current working directory is 367 -- writable. If the current working directory is not writable, then 368 -- Invalid_FD is returned for the file descriptor and null for the Name. 369 -- There is no race condition problem between processes trying to create 370 -- temp files at the same time in the same directory. 371 372 procedure Delete_File (Name : String; Success : out Boolean); 373 -- Deletes file. Success is set True or False indicating if the delete is 374 -- successful. 375 376 function File_Length (FD : File_Descriptor) return Long_Integer; 377 pragma Import (C, File_Length, "__gnat_file_length_long"); 378 379 type Large_File_Size is range -2**63 .. 2**63 - 1; 380 -- Maximum supported size for a file (8 exabytes = 8 million terabytes, 381 -- should be enough to accommodate all possible needs for quite a while). 382 383 function File_Length64 (FD : File_Descriptor) return Large_File_Size; 384 pragma Import (C, File_Length64, "__gnat_file_length"); 385 -- Get length of file from file descriptor FD 386 387 function File_Time_Stamp (Name : String) return OS_Time; 388 -- Given the name of a file or directory, Name, obtains and returns the 389 -- time stamp. This function can be used for an unopened file. Returns 390 -- Invalid_Time if Name doesn't correspond to an existing file. 391 392 function File_Time_Stamp (FD : File_Descriptor) return OS_Time; 393 -- Get time stamp of file from file descriptor FD Returns Invalid_Time is 394 -- FD doesn't correspond to an existing file. 395 396 function Get_Debuggable_Suffix return String_Access; 397 -- Return the debuggable suffix convention. Usually this is the same as 398 -- the convention for Get_Executable_Suffix. The result is allocated on 399 -- the heap and should be freed after use to avoid storage leaks. 400 401 function Get_Executable_Suffix return String_Access; 402 -- Return the executable suffix convention. The result is allocated on the 403 -- heap and should be freed after use to avoid storage leaks. 404 405 function Get_Object_Suffix return String_Access; 406 -- Return the object suffix convention. The result is allocated on the heap 407 -- and should be freed after use to avoid storage leaks. 408 409 function Get_Target_Debuggable_Suffix return String_Access; 410 -- Return the target debuggable suffix convention. Usually this is the same 411 -- as the convention for Get_Executable_Suffix. The result is allocated on 412 -- the heap and should be freed after use to avoid storage leaks. 413 414 function Get_Target_Executable_Suffix return String_Access; 415 -- Return the target executable suffix convention. The result is allocated 416 -- on the heap and should be freed after use to avoid storage leaks. 417 418 function Get_Target_Object_Suffix return String_Access; 419 -- Return the target object suffix convention. The result is allocated on 420 -- the heap and should be freed after use to avoid storage leaks. 421 422 function Is_Absolute_Path (Name : String) return Boolean; 423 -- Returns True if Name is an absolute path name, i.e. it designates a 424 -- file or directory absolutely rather than relative to another directory. 425 426 function Is_Directory (Name : String) return Boolean; 427 -- Determines if the given string, Name, is the name of a directory. 428 -- Returns True if so, False otherwise. Name may be an absolute path 429 -- name or a relative path name, including a simple file name. If it is 430 -- a relative path name, it is relative to the current working directory. 431 432 function Is_Executable_File (Name : String) return Boolean; 433 -- Determines if the given string, Name, is the name of an existing file 434 -- that is executable. Returns True if so, False otherwise. Note that this 435 -- function simply interrogates the file attributes (e.g. using the C 436 -- function stat), so it does not indicate a situation in which a file may 437 -- not actually be readable due to some other process having exclusive 438 -- access. 439 440 function Is_Owner_Readable_File (Name : String) return Boolean; 441 -- Determines if the given string, Name, is the name of an existing file 442 -- that is readable. Returns True if so, False otherwise. Note that this 443 -- function simply interrogates the file attributes (e.g. using the C 444 -- function stat), so it does not indicate a situation in which a file may 445 -- not actually be readable due to some other process having exclusive 446 -- access. 447 448 function Is_Regular_File (Name : String) return Boolean; 449 -- Determines if the given string, Name, is the name of an existing 450 -- regular file. Returns True if so, False otherwise. Name may be an 451 -- absolute path name or a relative path name, including a simple file 452 -- name. If it is a relative path name, it is relative to the current 453 -- working directory. 454 455 function Is_Symbolic_Link (Name : String) return Boolean; 456 -- Determines if the given string, Name, is the path of a symbolic link on 457 -- systems that support it. Returns True if so, False if the path is not a 458 -- symbolic link or if the system does not support symbolic links. 459 -- 460 -- A symbolic link is an indirect pointer to a file; its directory entry 461 -- contains the name of the file to which it is linked. Symbolic links may 462 -- span file systems and may refer to directories. 463 464 function Is_Owner_Writable_File (Name : String) return Boolean; 465 -- Determines if the given string, Name, is the name of an existing file 466 -- that is writable. Returns True if so, False otherwise. Note that this 467 -- function simply interrogates the file attributes (e.g. using the C 468 -- function stat), so it does not indicate a situation in which a file may 469 -- not actually be writable due to some other process having exclusive 470 -- access. 471 472 function Is_Read_Accessible_File (Name : String) return Boolean; 473 -- Determines if the given string, Name, is the name of an existing file 474 -- that is readable. Returns True if so, False otherwise. 475 476 function Is_Write_Accessible_File (Name : String) return Boolean; 477 -- Determines if the given string, Name, is the name of an existing file 478 -- that is writable. Returns True if so, False otherwise. 479 480 function Is_Readable_File (Name : String) return Boolean 481 renames Is_Read_Accessible_File; 482 function Is_Writable_File (Name : String) return Boolean 483 renames Is_Write_Accessible_File; 484 -- These subprograms provided for backward compatibility and should not be 485 -- used. Use Is_Owner_Readable_File/Is_Owner_Writable_File or 486 -- Is_Read_Accessible_File/Is_Write_Accessible_File instead. 487 488 function Locate_Exec_On_Path (Exec_Name : String) return String_Access; 489 -- Try to locate an executable whose name is given by Exec_Name in the 490 -- directories listed in the environment Path. If the Exec_Name does not 491 -- have the executable suffix, it will be appended before the search. 492 -- Otherwise works like Locate_Regular_File below. If the executable is 493 -- not found, null is returned. 494 -- 495 -- Note that this function allocates memory for the returned value. This 496 -- memory needs to be deallocated after use. 497 498 function Locate_Regular_File 499 (File_Name : String; 500 Path : String) return String_Access; 501 -- Try to locate a regular file whose name is given by File_Name in the 502 -- directories listed in Path. If a file is found, its full pathname is 503 -- returned; otherwise, a null pointer is returned. If the File_Name given 504 -- is an absolute pathname, then Locate_Regular_File just checks that the 505 -- file exists and is a regular file. Otherwise, if the File_Name given 506 -- includes directory information, Locate_Regular_File first checks if the 507 -- file exists relative to the current directory. If it does not, or if 508 -- the File_Name given is a simple file name, the Path argument is parsed 509 -- according to OS conventions, and for each directory in the Path a check 510 -- is made if File_Name is a relative pathname of a regular file from that 511 -- directory. 512 -- 513 -- Note that this function allocates some memory for the returned value. 514 -- This memory needs to be deallocated after use. 515 516 Seek_Cur : constant := 1; 517 Seek_End : constant := 2; 518 Seek_Set : constant := 0; 519 -- Used to indicate origin for Lseek call 520 521 procedure Lseek 522 (FD : File_Descriptor; 523 offset : Long_Integer; 524 origin : Integer); 525 pragma Import (C, Lseek, "__gnat_lseek"); 526 -- Sets the current file pointer to the indicated offset value, relative 527 -- to the current position (origin = SEEK_CUR), end of file (origin = 528 -- SEEK_END), or start of file (origin = SEEK_SET). 529 530 function Normalize_Pathname 531 (Name : String; 532 Directory : String := ""; 533 Resolve_Links : Boolean := True; 534 Case_Sensitive : Boolean := True) return String; 535 -- Returns a file name as an absolute path name, resolving all relative 536 -- directories, and symbolic links. If Name is a relative path, it is 537 -- interpreted relative to Directory, or to the current directory if 538 -- Directory is the empty string (the default). The result returned is 539 -- the normalized name of the file, containing no "." or ".." components, 540 -- and no duplicated directory separators. For most cases, if two file 541 -- names designate the same file through different paths, 542 -- Normalize_Pathname will return the same canonical name in both cases. 543 -- However, there are cases when this is not true; for example, this is 544 -- not true in Unix for two hard links designating the same file. 545 -- 546 -- On Windows, the returned path will start with a drive letter. If 547 -- Directory is empty (the default) and Name is a relative path or an 548 -- absolute path without drive letter, the letter of the current drive 549 -- will start the returned path. If Case_Sensitive is True (the default), 550 -- then this drive letter will be forced to upper case ("C:\..."). 551 -- 552 -- If Resolve_Links is set to True, then the symbolic links, on systems 553 -- that support them, will be fully converted to the name of the file or 554 -- directory pointed to. This is slightly less efficient, since it 555 -- requires system calls. 556 -- 557 -- If Name is empty or the path contains symbolic links that can't be 558 -- resolved (for example there is a symbolic link circularity, 559 -- e.g. A is a symbolic link for B, and B is a symbolic link for A), then 560 -- Normalize_Pathname returns an empty string. 561 -- 562 -- For case-sensitive file systems, the value of Case_Sensitive parameter 563 -- is ignored. For file systems that are not case-sensitive, such as 564 -- Windows, if this parameter is set to False, then the file and directory 565 -- names are folded to lower case. This allows checking whether two files 566 -- are the same by applying this function to their names and comparing the 567 -- results. If Case_Sensitive is set to True, this function does not change 568 -- the casing of file and directory names. 569 570 function Open_Append 571 (Name : String; 572 Fmode : Mode) return File_Descriptor; 573 -- Opens file Name for appending, returning its file descriptor. File 574 -- descriptor returned is Invalid_FD if the file cannot be successfully 575 -- opened. 576 577 function Open_Read 578 (Name : String; 579 Fmode : Mode) return File_Descriptor; 580 -- Open file Name for reading, returning its file descriptor. File 581 -- descriptor returned is Invalid_FD if the file cannot be opened. 582 583 function Open_Read_Write 584 (Name : String; 585 Fmode : Mode) return File_Descriptor; 586 -- Open file Name for both reading and writing, returning its file 587 -- descriptor. File descriptor returned is Invalid_FD if the file 588 -- cannot be opened. 589 590 function Read 591 (FD : File_Descriptor; 592 A : System.Address; 593 N : Integer) return Integer; 594 -- Read N bytes to address A from file referenced by FD. Returned value is 595 -- count of bytes actually read, which can be less than N at EOF. 596 597 procedure Rename_File 598 (Old_Name : String; 599 New_Name : String; 600 Success : out Boolean); 601 -- Rename a file. Success is set True or False indicating if the rename is 602 -- successful or not. 603 -- 604 -- WARNING: In one very important respect, this function is significantly 605 -- non-portable. If New_Name already exists then on Unix systems, the call 606 -- deletes the existing file, and the call signals success. On Windows, the 607 -- call fails, without doing the rename operation. See also the procedure 608 -- Ada.Directories.Rename, which portably provides the windows semantics, 609 -- i.e. fails if the output file already exists. 610 611 -- The following defines the mode for the Copy_File procedure below. Note 612 -- that "time stamps and other file attributes" in the descriptions below 613 -- refers to the creation and last modification times, and also the file 614 -- access (read/write/execute) status flags. 615 616 procedure Set_Close_On_Exec 617 (FD : File_Descriptor; 618 Close_On_Exec : Boolean; 619 Status : out Boolean); 620 -- When Close_On_Exec is True, mark FD to be closed automatically when new 621 -- program is executed by the calling process (i.e. prevent FD from being 622 -- inherited by child processes). When Close_On_Exec is False, mark FD to 623 -- not be closed on exec (i.e. allow it to be inherited). Status is False 624 -- if the operation could not be performed. 625 626 S_Owner : constant := 1; 627 S_Group : constant := 2; 628 S_Others : constant := 4; 629 -- Constants for use in Mode parameter to Set_Executable 630 631 procedure Set_Executable (Name : String; Mode : Positive := S_Owner); 632 -- Change permissions on the file given by Name to make it executable 633 -- for its owner, group or others, according to the setting of Mode. 634 -- As indicated, the default if no Mode parameter is given is owner. 635 636 procedure Set_File_Last_Modify_Time_Stamp (Name : String; Time : OS_Time); 637 -- Given the name of a file or directory, Name, set the last modification 638 -- time stamp. This function must be used for an unopened file. 639 640 procedure Set_Non_Readable (Name : String); 641 -- Change permissions on the named file to make it non-readable for 642 -- its owner. The writable and executable permissions are not 643 -- modified. 644 645 procedure Set_Non_Writable (Name : String); 646 -- Change permissions on the named file to make it non-writable for its 647 -- owner. The readable and executable permissions are not modified. 648 649 procedure Set_Read_Only (Name : String) renames Set_Non_Writable; 650 -- This renaming is provided for backwards compatibility with previous 651 -- versions. The use of Set_Non_Writable is preferred (clearer name). 652 653 procedure Set_Readable (Name : String); 654 -- Change permissions on the named file to make it readable for its 655 -- owner. 656 657 procedure Set_Writable (Name : String); 658 -- Change permissions on the named file to make it writable for its owner 659 660 function Write 661 (FD : File_Descriptor; 662 A : System.Address; 663 N : Integer) return Integer; 664 -- Write N bytes from address A to file referenced by FD. The returned 665 -- value is the number of bytes written, which can be less than N if a 666 -- disk full condition was detected. 667 668 -- The following section contains low-level routines using addresses to 669 -- pass file name and executable name. In each routine the name must be 670 -- Nul-Terminated. For complete documentation refer to the equivalent 671 -- routine (using String in place of C_File_Name) defined above. 672 673 subtype C_File_Name is System.Address; 674 -- This subtype is used to document that a parameter is the address of a 675 -- null-terminated string containing the name of a file. 676 677 procedure Copy_File 678 (Name : C_File_Name; 679 Pathname : C_File_Name; 680 Success : out Boolean; 681 Mode : Copy_Mode := Copy; 682 Preserve : Attribute := Time_Stamps); 683 684 procedure Copy_Time_Stamps 685 (Source : C_File_Name; 686 Dest : C_File_Name; 687 Success : out Boolean); 688 689 function Create_File 690 (Name : C_File_Name; 691 Fmode : Mode) return File_Descriptor; 692 693 function Create_New_File 694 (Name : C_File_Name; 695 Fmode : Mode) return File_Descriptor; 696 697 procedure Delete_File (Name : C_File_Name; Success : out Boolean); 698 699 function File_Time_Stamp (Name : C_File_Name) return OS_Time; 700 701 function Is_Directory (Name : C_File_Name) return Boolean; 702 function Is_Executable_File (Name : C_File_Name) return Boolean; 703 function Is_Owner_Readable_File (Name : C_File_Name) return Boolean; 704 function Is_Regular_File (Name : C_File_Name) return Boolean; 705 function Is_Symbolic_Link (Name : C_File_Name) return Boolean; 706 function Is_Owner_Writable_File (Name : C_File_Name) return Boolean; 707 708 function Locate_Regular_File 709 (File_Name : C_File_Name; 710 Path : C_File_Name) return String_Access; 711 712 function Open_Append 713 (Name : C_File_Name; 714 Fmode : Mode) return File_Descriptor; 715 716 function Open_Read 717 (Name : C_File_Name; 718 Fmode : Mode) return File_Descriptor; 719 720 function Open_Read_Write 721 (Name : C_File_Name; 722 Fmode : Mode) return File_Descriptor; 723 724 procedure Rename_File 725 (Old_Name : C_File_Name; 726 New_Name : C_File_Name; 727 Success : out Boolean); 728 729 ------------------ 730 -- Subprocesses -- 731 ------------------ 732 733 subtype Argument_List is String_List; 734 -- Type used for argument list in call to Spawn. The lower bound of the 735 -- array should be 1, and the length of the array indicates the number of 736 -- arguments. 737 738 subtype Argument_List_Access is String_List_Access; 739 -- Type used to return Argument_List without dragging in secondary stack. 740 -- Note that there is a Free procedure declared for this subtype which 741 -- frees the array and all referenced strings. 742 743 type Process_Id is private; 744 -- A private type used to identify a process activated by the following 745 -- non-blocking calls. The only meaningful operation on this type is a 746 -- comparison for equality. 747 748 Invalid_Pid : constant Process_Id; 749 -- A special value used to indicate errors, as described below 750 751 function Current_Process_Id return Process_Id; 752 -- Returns the current process id or Invalid_Pid if not supported by the 753 -- runtime. 754 755 function Argument_String_To_List 756 (Arg_String : String) return Argument_List_Access; 757 -- Take a string that is a program and its arguments and parse it into an 758 -- Argument_List. Note that the result is allocated on the heap, and must 759 -- be freed by the programmer (when it is no longer needed) to avoid 760 -- memory leaks. 761 -- On Windows, backslashes are used as directory separators. On Unix, 762 -- however, they are used to escape the following character, so that for 763 -- instance "-d=name\ with\ space" is a single argument. In the result 764 -- list, the backslashes have been cleaned up when needed. The previous 765 -- example will thus result a single-element array, where the element is 766 -- "-d=name with space" (Unix) or "-d=name\ with\ space" (windows). 767 768 procedure Kill (Pid : Process_Id; Hard_Kill : Boolean := True); 769 -- Kill the process designated by Pid. Does nothing if Pid is Invalid_Pid 770 -- or on platforms where it is not supported, such as VxWorks. Hard_Kill 771 -- is True by default, and when True the process is terminated immediately. 772 -- If Hard_Kill is False, then a signal SIGINT is sent to the process on 773 -- POSIX OS or a ctrl-C event on Windows, allowing the process a chance to 774 -- terminate properly using a corresponding handler. 775 776 procedure Kill_Process_Tree (Pid : Process_Id; Hard_Kill : Boolean := True); 777 -- Kill the process designated by Pid and all it's children processes. 778 -- Does nothing if Pid is Invalid_Pid or on platforms where it is not 779 -- supported, such as VxWorks. Hard_Kill is True by default, and when True 780 -- the processes are terminated immediately. If Hard_Kill is False, then a 781 -- signal SIGINT is sent to the processes on POSIX OS or a ctrl-C event 782 -- on Windows, allowing the processes a chance to terminate properly 783 -- using a corresponding handler. 784 -- 785 -- Note that this routine is not atomic and is supported only on Linux 786 -- and Windows. On other OS it will only kill the process identified by 787 -- Pid. 788 789 function Non_Blocking_Spawn 790 (Program_Name : String; 791 Args : Argument_List) return Process_Id; 792 -- This is a non blocking call. The Process_Id of the spawned process is 793 -- returned. Parameters are to be used as in Spawn. If Invalid_Pid is 794 -- returned the program could not be spawned. 795 -- 796 -- Spawning processes from tasking programs is not recommended. See 797 -- "NOTE: Spawn in tasking programs" below. 798 -- 799 -- This function will always return Invalid_Pid under VxWorks, since there 800 -- is no notion of executables under this OS. 801 802 function Non_Blocking_Spawn 803 (Program_Name : String; 804 Args : Argument_List; 805 Output_File_Descriptor : File_Descriptor; 806 Err_To_Out : Boolean := True) return Process_Id; 807 -- Similar to the procedure above, but redirects the output to the file 808 -- designated by Output_File_Descriptor. If Err_To_Out is True, then the 809 -- Standard Error output is also redirected. Invalid_Pid is returned 810 -- if the program could not be spawned successfully. 811 -- 812 -- Spawning processes from tasking programs is not recommended. See 813 -- "NOTE: Spawn in tasking programs" below. 814 -- 815 -- This function will always return Invalid_Pid under VxWorks, since there 816 -- is no notion of executables under this OS. 817 818 function Non_Blocking_Spawn 819 (Program_Name : String; 820 Args : Argument_List; 821 Output_File : String; 822 Err_To_Out : Boolean := True) return Process_Id; 823 -- Similar to the procedure above, but saves the output of the command to 824 -- a file with the name Output_File. 825 -- 826 -- Invalid_Pid is returned if the output file could not be created or if 827 -- the program could not be spawned successfully. 828 -- 829 -- Spawning processes from tasking programs is not recommended. See 830 -- "NOTE: Spawn in tasking programs" below. 831 -- 832 -- This function will always return Invalid_Pid under VxWorks, since there 833 -- is no notion of executables under this OS. 834 835 function Non_Blocking_Spawn 836 (Program_Name : String; 837 Args : Argument_List; 838 Stdout_File : String; 839 Stderr_File : String) return Process_Id; 840 -- Similar to the procedure above, but saves the standard output of the 841 -- command to a file with the name Stdout_File and the standard output 842 -- of the command to a file with the name Stderr_File. 843 844 procedure Normalize_Arguments (Args : in out Argument_List); 845 -- Normalize all arguments in the list. This ensure that the argument list 846 -- is compatible with the running OS and will works fine with Spawn and 847 -- Non_Blocking_Spawn for example. If Normalize_Arguments is called twice 848 -- on the same list it will do nothing the second time. Note that Spawn 849 -- and Non_Blocking_Spawn call Normalize_Arguments automatically, but 850 -- since there is a guarantee that a second call does nothing, this 851 -- internal call will have no effect if Normalize_Arguments is called 852 -- before calling Spawn. The call to Normalize_Arguments assumes that the 853 -- individual referenced arguments in Argument_List are on the heap, and 854 -- may free them and reallocate if they are modified. 855 856 function Pid_To_Integer (Pid : Process_Id) return Integer; 857 -- Convert a process id to an Integer. Useful for writing hash functions 858 -- for type Process_Id or to compare two Process_Id (e.g. for sorting). 859 860 procedure Spawn 861 (Program_Name : String; 862 Args : Argument_List; 863 Success : out Boolean); 864 -- This procedure spawns a program with a given list of arguments. The 865 -- first parameter of is the name of the executable. The second parameter 866 -- contains the arguments to be passed to this program. Success is False 867 -- if the named program could not be spawned or its execution completed 868 -- unsuccessfully. Note that the caller will be blocked until the 869 -- execution of the spawned program is complete. For maximum portability, 870 -- use a full path name for the Program_Name argument. On some systems 871 -- (notably Unix systems) a simple file name may also work (if the 872 -- executable can be located in the path). 873 -- 874 -- Spawning processes from tasking programs is not recommended. See 875 -- "NOTE: Spawn in tasking programs" below. 876 -- 877 -- Note: Arguments in Args that contain spaces and/or quotes such as 878 -- "--GCC=gcc -v" or "--GCC=""gcc -v""" are not portable across all 879 -- operating systems, and would not have the desired effect if they were 880 -- passed directly to the operating system. To avoid this problem, Spawn 881 -- makes an internal call to Normalize_Arguments, which ensures that such 882 -- arguments are modified in a manner that ensures that the desired effect 883 -- is obtained on all operating systems. The caller may call 884 -- Normalize_Arguments explicitly before the call (e.g. to print out the 885 -- exact form of arguments passed to the operating system). In this case 886 -- the guarantee a second call to Normalize_Arguments has no effect 887 -- ensures that the internal call will not affect the result. Note that 888 -- the implicit call to Normalize_Arguments may free and reallocate some 889 -- of the individual arguments. 890 -- 891 -- This function will always set Success to False under VxWorks and other 892 -- similar operating systems which have no notion of the concept of 893 -- dynamically executable file. Otherwise Success is set True if the exit 894 -- status of the spawned process is zero. 895 896 function Spawn 897 (Program_Name : String; 898 Args : Argument_List) return Integer; 899 -- Similar to the above procedure, but returns the actual status returned 900 -- by the operating system, or -1 under VxWorks and any other similar 901 -- operating systems which have no notion of separately spawnable programs. 902 -- 903 -- Spawning processes from tasking programs is not recommended. See 904 -- "NOTE: Spawn in tasking programs" below. 905 906 procedure Spawn 907 (Program_Name : String; 908 Args : Argument_List; 909 Output_File_Descriptor : File_Descriptor; 910 Return_Code : out Integer; 911 Err_To_Out : Boolean := True); 912 -- Similar to the procedure above, but redirects the output to the file 913 -- designated by Output_File_Descriptor. If Err_To_Out is True, then the 914 -- Standard Error output is also redirected. 915 -- Return_Code is set to the status code returned by the operating system 916 -- 917 -- Spawning processes from tasking programs is not recommended. See 918 -- "NOTE: Spawn in tasking programs" below. 919 920 procedure Spawn 921 (Program_Name : String; 922 Args : Argument_List; 923 Output_File : String; 924 Success : out Boolean; 925 Return_Code : out Integer; 926 Err_To_Out : Boolean := True); 927 -- Similar to the procedure above, but saves the output of the command to 928 -- a file with the name Output_File. 929 -- 930 -- Success is set to True if the command is executed and its output 931 -- successfully written to the file. If Success is True, then Return_Code 932 -- will be set to the status code returned by the operating system. 933 -- Otherwise, Return_Code is undefined. 934 -- 935 -- Spawning processes from tasking programs is not recommended. See 936 -- "NOTE: Spawn in tasking programs" below. 937 938 procedure Wait_Process (Pid : out Process_Id; Success : out Boolean); 939 -- Wait for the completion of any of the processes created by previous 940 -- calls to Non_Blocking_Spawn. The caller will be suspended until one of 941 -- these processes terminates (normally or abnormally). If any of these 942 -- subprocesses terminates prior to the call to Wait_Process (and has not 943 -- been returned by a previous call to Wait_Process), then the call to 944 -- Wait_Process is immediate. Pid identifies the process that has 945 -- terminated (matching the value returned from Non_Blocking_Spawn). 946 -- Success is set to True if this sub-process terminated successfully. If 947 -- Pid = Invalid_Pid, there were no subprocesses left to wait on. 948 -- 949 -- This function will always set success to False under VxWorks, since 950 -- there is no notion of executables under this OS. 951 952 procedure Non_Blocking_Wait_Process 953 (Pid : out Process_Id; Success : out Boolean); 954 -- Same as Wait_Process, except if there are no completed child processes, 955 -- return immediately without blocking, and return Invalid_Pid in Pid. 956 -- Not supported on all platforms; Success = False if not supported. 957 958 ------------------------------------- 959 -- NOTE: Spawn in Tasking Programs -- 960 ------------------------------------- 961 962 -- Spawning processes in tasking programs using the above Spawn and 963 -- Non_Blocking_Spawn subprograms is not recommended, because there are 964 -- subtle interactions between creating a process and signals/locks that 965 -- can cause trouble. These issues are not specific to Ada; they depend 966 -- primarily on the operating system. 967 968 -- If you need to spawn processes in a tasking program, you will need to 969 -- understand the semantics of your operating system, and you are likely to 970 -- write non-portable code, because operating systems differ in this area. 971 972 -- The Spawn and Non_Blocking_Spawn subprograms call the following 973 -- operating system functions: 974 975 -- On Windows: spawnvp (blocking) or CreateProcess (non-blocking) 976 977 -- On Solaris: fork1, followed in the child process by execv 978 979 -- On other Unix-like systems: fork, followed in the child 980 -- process by execv. 981 982 -- On vxworks, spawning of processes is not supported 983 984 -- For details, look at the functions __gnat_portable_spawn and 985 -- __gnat_portable_no_block_spawn in adaint.c. 986 987 -- You should read the operating-system-specific documentation for the 988 -- above functions, paying special attention to subtle interactions with 989 -- threading, signals, locks, and file descriptors. Most of the issues are 990 -- related to the fact that on Unix, there is a window of time between fork 991 -- and execv; Windows does not have this problem, because spawning is done 992 -- in a single operation. 993 994 -- On Posix-compliant systems, such as Linux, fork duplicates just the 995 -- calling thread. (On Solaris, fork1 is the Posix-compliant version of 996 -- fork.) 997 998 -- You should avoid using signals while spawning. This includes signals 999 -- used internally by the Ada run-time system, such as timer signals used 1000 -- to implement delay statements. 1001 1002 -- It is best to spawn any subprocesses very early, before the parent 1003 -- process creates tasks, locks, or installs signal handlers. Certainly 1004 -- avoid doing simultaneous spawns from multiple threads of the same 1005 -- process. 1006 1007 -- There is no problem spawning a subprocess that uses tasking: the 1008 -- problems are caused only by tasking in the parent. 1009 1010 -- If the parent is using tasking, and needs to spawn subprocesses at 1011 -- arbitrary times, one technique is for the parent to spawn (very early) 1012 -- a particular spawn-manager subprocess whose job is to spawn other 1013 -- processes. The spawn-manager must avoid tasking. The parent sends 1014 -- messages to the spawn-manager requesting it to spawn processes, using 1015 -- whatever inter-process communication mechanism you like, such as 1016 -- sockets. 1017 1018 -- In short, mixing spawning of subprocesses with tasking is a tricky 1019 -- business, and should be avoided if possible, but if it is necessary, 1020 -- the above guidelines should be followed, and you should beware of 1021 -- portability problems. 1022 1023 ------------------- 1024 -- Miscellaneous -- 1025 ------------------- 1026 1027 function Errno return Integer; 1028 pragma Import (C, Errno, "__get_errno"); 1029 -- Return the task-safe last error number 1030 1031 function Errno_Message 1032 (Err : Integer := Errno; 1033 Default : String := "") return String; 1034 -- Return a message describing the given Errno value. If none is provided 1035 -- by the system, return Default if not empty, else return a generic 1036 -- message indicating the numeric errno value. 1037 1038 function Getenv (Name : String) return String_Access; 1039 -- Get the value of the environment variable. Returns an access to the 1040 -- empty string if the environment variable does not exist or has an 1041 -- explicit null value (in some operating systems these are distinct 1042 -- cases, in others they are not; this interface abstracts away that 1043 -- difference. The argument is allocated on the heap (even in the null 1044 -- case), and needs to be freed explicitly when no longer needed to avoid 1045 -- memory leaks. 1046 1047 procedure OS_Abort; 1048 pragma Import (C, OS_Abort, "abort"); 1049 pragma No_Return (OS_Abort); 1050 -- Exit to OS signalling an abort (traceback or other appropriate 1051 -- diagnostic information should be given if possible, or entry made to 1052 -- the debugger if that is possible). 1053 1054 procedure OS_Exit (Status : Integer); 1055 pragma No_Return (OS_Exit); 1056 -- Exit to OS with given status code (program is terminated). Note that 1057 -- this is abrupt termination. All tasks are immediately terminated. There 1058 -- are no finalization or other Ada-specific cleanup actions performed. On 1059 -- systems with atexit handlers (such as Unix and Windows), atexit handlers 1060 -- are called. 1061 1062 type OS_Exit_Subprogram is access procedure (Status : Integer); 1063 1064 procedure OS_Exit_Default (Status : Integer); 1065 pragma No_Return (OS_Exit_Default); 1066 -- Default implementation of procedure OS_Exit 1067 1068 OS_Exit_Ptr : OS_Exit_Subprogram := OS_Exit_Default'Access; 1069 -- OS_Exit is implemented through this access value. It it then possible to 1070 -- change the implementation of OS_Exit by redirecting OS_Exit_Ptr to an 1071 -- other implementation. 1072 1073 procedure Set_Errno (Errno : Integer); 1074 pragma Import (C, Set_Errno, "__set_errno"); 1075 -- Set the task-safe error number 1076 1077 procedure Setenv (Name : String; Value : String); 1078 -- Set the value of the environment variable Name to Value. This call 1079 -- modifies the current environment, but does not modify the parent 1080 -- process environment. After a call to Setenv, Getenv (Name) will always 1081 -- return a String_Access referencing the same String as Value. This is 1082 -- true also for the null string case (the actual effect may be to either 1083 -- set an explicit null as the value, or to remove the entry, this is 1084 -- operating system dependent). Note that any following calls to Spawn 1085 -- will pass an environment to the spawned process that includes the 1086 -- changes made by Setenv calls. 1087 1088 Directory_Separator : constant Character; 1089 -- The character that is used to separate parts of a pathname 1090 1091 Path_Separator : constant Character; 1092 -- The character to separate paths in an environment variable value 1093 1094private 1095 pragma Import (C, Path_Separator, "__gnat_path_separator"); 1096 pragma Import (C, Directory_Separator, "__gnat_dir_separator"); 1097 pragma Import (C, Current_Time, "__gnat_current_time"); 1098 pragma Import (C, Current_Process_Id, "__gnat_current_process_id"); 1099 1100 type OS_Time is 1101 range -(2 ** (Standard'Address_Size - Integer'(1))) .. 1102 +(2 ** (Standard'Address_Size - Integer'(1)) - 1); 1103 -- Type used for timestamps in the compiler. This type is used to hold 1104 -- time stamps, but may have a different representation than C's time_t. 1105 -- This type needs to match the declaration of OS_Time in adaint.h. 1106 1107 -- Add pragma Inline statements for comparison operations on OS_Time. It 1108 -- would actually be nice to use pragma Import (Intrinsic) here, but this 1109 -- was not properly supported till GNAT 3.15a, so that would cause 1110 -- bootstrap path problems. To be changed later ??? 1111 1112 Invalid_Time : constant OS_Time := -1; 1113 -- This value should match the return value from __gnat_file_time_* 1114 1115 pragma Inline ("<"); 1116 pragma Inline (">"); 1117 pragma Inline ("<="); 1118 pragma Inline (">="); 1119 pragma Inline (To_C); 1120 pragma Inline (To_Ada); 1121 1122 type Process_Id is new Integer; 1123 Invalid_Pid : constant Process_Id := -1; 1124 1125end System.OS_Lib; 1126