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