1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . O S _ L I B --
11 -- Copyright (C) 1995-2001 Ada Core Technologies, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
31 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com). --
33 ------------------------------------------------------------------------------
35 -- Operating system interface facilities
37 -- This package contains types and procedures for interfacing to the
38 -- underlying OS. It is used by the GNAT compiler and by tools associated
39 -- with the GNAT compiler, and therefore works for the various operating
40 -- systems to which GNAT has been ported. This package will undoubtedly
41 -- grow as new services are needed by various tools.
43 -- This package tends to use fairly low-level Ada in order to not bring
44 -- in large portions of the RTL. For example, functions return access
45 -- to string as part of avoiding functions returning unconstrained types;
46 -- types related to dates are defined here instead of using the types
47 -- from Calendar, since use of Calendar forces linking in of tasking code.
49 -- Except where specifically noted, these routines are portable across
50 -- all GNAT implementations on all supported operating systems.
53 with Unchecked_Deallocation
;
55 package GNAT
.OS_Lib
is
56 pragma Elaborate_Body
(OS_Lib
);
58 type String_Access
is access all String;
60 procedure Free
is new Unchecked_Deallocation
61 (Object
=> String, Name
=> String_Access
);
67 -- The OS's notion of time is represented by the private type OS_Time.
68 -- This is the type returned by the File_Time_Stamp functions to obtain
69 -- the time stamp of a specified file. Functions and a procedure (modeled
70 -- after the similar subprograms in package Calendar) are provided for
71 -- extracting information from a value of this type. Although these are
72 -- called GM, the intention is not that they provide GMT times in all
73 -- cases but rather the actual (time-zone independent) time stamp of the
74 -- file (of course in Unix systems, this *is* in GMT form).
76 type OS_Time
is private;
78 subtype Year_Type
is Integer range 1900 .. 2099;
79 subtype Month_Type
is Integer range 1 .. 12;
80 subtype Day_Type
is Integer range 1 .. 31;
81 subtype Hour_Type
is Integer range 0 .. 23;
82 subtype Minute_Type
is Integer range 0 .. 59;
83 subtype Second_Type
is Integer range 0 .. 59;
85 function GM_Year
(Date
: OS_Time
) return Year_Type
;
86 function GM_Month
(Date
: OS_Time
) return Month_Type
;
87 function GM_Day
(Date
: OS_Time
) return Day_Type
;
88 function GM_Hour
(Date
: OS_Time
) return Hour_Type
;
89 function GM_Minute
(Date
: OS_Time
) return Minute_Type
;
90 function GM_Second
(Date
: OS_Time
) return Second_Type
;
95 Month
: out Month_Type
;
98 Minute
: out Minute_Type
;
99 Second
: out Second_Type
);
105 -- These routines give access to the open/creat/close/read/write level
106 -- of I/O routines in the typical C library (these functions are not
107 -- part of the ANSI C standard, but are typically available in all
108 -- systems). See also package Interfaces.C_Streams for access to the
109 -- stream level routines.
111 -- Note on file names. If a file name is passed as type String in any
112 -- of the following specifications, then the name is a normal Ada string
113 -- and need not be NUL-terminated. However, a trailing NUL character is
114 -- permitted, and will be ignored (more accurately, the NUL and any
115 -- characters that follow it will be ignored).
117 type File_Descriptor
is private;
118 -- Corresponds to the int file handle values used in the C routines,
120 Standin
: constant File_Descriptor
;
121 Standout
: constant File_Descriptor
;
122 Standerr
: constant File_Descriptor
;
123 -- File descriptors for standard input output files
125 Invalid_FD
: constant File_Descriptor
;
126 -- File descriptor returned when error in opening/creating file;
128 type Mode
is (Binary
, Text
);
129 for Mode
'Size use Integer'Size;
130 for Mode
use (Binary
=> 0, Text
=> 1);
131 -- Used in all the Open and Create calls to specify if the file is to be
132 -- opened in binary mode or text mode. In systems like Unix, this has no
133 -- effect, but in systems capable of text mode translation, the use of
134 -- Text as the mode parameter causes the system to do CR/LF translation
135 -- and also to recognize the DOS end of file character on input. The use
136 -- of Text where appropriate allows programs to take a portable Unix view
137 -- of DOs-format files and process them appropriately.
142 return File_Descriptor
;
143 -- Open file Name for reading, returning file descriptor File descriptor
144 -- returned is Invalid_FD if file cannot be opened.
146 function Open_Read_Write
149 return File_Descriptor
;
150 -- Open file Name for both reading and writing, returning file
151 -- descriptor. File descriptor returned is Invalid_FD if file cannot be
157 return File_Descriptor
;
158 -- Creates new file with given name for writing, returning file descriptor
159 -- for subsequent use in Write calls. File descriptor returned is
160 -- Invalid_FD if file cannot be successfully created
162 function Create_New_File
165 return File_Descriptor
;
166 -- Create new file with given name for writing, returning file descriptor
167 -- for subsequent use in Write calls. This differs from Create_File in
168 -- that it fails if the file already exists. File descriptor returned is
169 -- Invalid_FD if the file exists or cannot be created.
171 Temp_File_Len
: constant Integer := 12;
172 -- Length of name returned by Create_Temp_File call (GNAT-XXXXXX & NUL)
174 subtype Temp_File_Name
is String (1 .. Temp_File_Len
);
175 -- String subtype set by Create_Temp_File
177 procedure Create_Temp_File
178 (FD
: out File_Descriptor
;
179 Name
: out Temp_File_Name
);
180 -- Create and open for writing a temporary file. The name of the
181 -- file and the File Descriptor are returned. The File Descriptor
182 -- returned is Invalid_FD in the case of failure. No mode parameter
183 -- is provided. Since this is a temporary file, there is no point in
184 -- doing text translation on it.
186 procedure Close
(FD
: File_Descriptor
);
187 pragma Import
(C
, Close
, "close");
188 -- Close file referenced by FD
190 procedure Delete_File
(Name
: String; Success
: out Boolean);
191 -- Deletes file. Success is set True or False indicating if the delete is
194 procedure Rename_File
197 Success
: out Boolean);
198 -- Rename a file. Successis set True or False indicating if the rename is
202 (FD
: File_Descriptor
;
206 pragma Import
(C
, Read
, "read");
207 -- Read N bytes to address A from file referenced by FD. Returned value
208 -- is count of bytes actually read, which can be less than N at EOF.
211 (FD
: File_Descriptor
;
215 pragma Import
(C
, Write
, "write");
216 -- Write N bytes from address A to file referenced by FD. The returned
217 -- value is the number of bytes written, which can be less than N if
218 -- a disk full condition was detected.
220 Seek_Cur
: constant := 1;
221 Seek_End
: constant := 2;
222 Seek_Set
: constant := 0;
223 -- Used to indicate origin for Lseek call
226 (FD
: File_Descriptor
;
227 offset
: Long_Integer;
229 pragma Import
(C
, Lseek
, "lseek");
230 -- Sets the current file pointer to the indicated offset value,
231 -- relative to the current position (origin = SEEK_CUR), end of
232 -- file (origin = SEEK_END), or start of file (origin = SEEK_SET).
234 function File_Length
(FD
: File_Descriptor
) return Long_Integer;
235 pragma Import
(C
, File_Length
, "__gnat_file_length");
236 -- Get length of file from file descriptor FD
238 function File_Time_Stamp
(Name
: String) return OS_Time
;
239 -- Given the name of a file or directory, Name, obtains and returns the
240 -- time stamp. This function can be used for an unopend file.
242 function File_Time_Stamp
(FD
: File_Descriptor
) return OS_Time
;
243 -- Get time stamp of file from file descriptor FD
245 function Normalize_Pathname
247 Directory
: String := "")
249 -- Returns a file name as an absolute path name, resolving all relative
250 -- directories, and symbolic links. The parameter Directory is a fully
251 -- resolved path name for a directory, or the empty string (the default).
252 -- Name is the name of a file, which is either relative to the given
253 -- directory name, if Directory is non-null, or to the current working
254 -- directory if Directory is null. The result returned is the normalized
255 -- name of the file. For most cases, if two file names designate the same
256 -- file through different paths, Normalize_Pathname will return the same
257 -- canonical name in both cases. However, there are cases when this is
258 -- not true; for example, this is not true in Unix for two hard links
259 -- designating the same file.
261 -- If Name cannot be resolved or is null on entry (for example if there is
262 -- a circularity in symbolic links: A is a symbolic link for B, while B is
263 -- a symbolic link for A), then Normalize_Pathname returns an empty string.
265 -- In VMS, if Name follows the VMS syntax file specification, it is first
266 -- converted into Unix syntax. If the conversion fails, Normalize_Pathname
267 -- returns an empty string.
269 function Is_Absolute_Path
(Name
: String) return Boolean;
270 -- Returns True if Name is an absolute path name, i.e. it designates
271 -- a directory absolutely, rather than relative to another directory.
273 function Is_Regular_File
(Name
: String) return Boolean;
274 -- Determines if the given string, Name, is the name of an existing
275 -- regular file. Returns True if so, False otherwise.
277 function Is_Directory
(Name
: String) return Boolean;
278 -- Determines if the given string, Name, is the name of a directory.
279 -- Returns True if so, False otherwise.
281 function Is_Writable_File
(Name
: String) return Boolean;
282 -- Determines if the given string, Name, is the name of an existing
283 -- file that is writable. Returns True if so, False otherwise.
285 function Locate_Exec_On_Path
287 return String_Access
;
288 -- Try to locate an executable whose name is given by Exec_Name in the
289 -- directories listed in the environment Path. If the Exec_Name doesn't
290 -- have the executable suffix, it will be appended before the search.
291 -- Otherwise works like Locate_Regular_File below.
293 -- Note that this function allocates some memory for the returned value.
294 -- This memory needs to be deallocated after use.
296 function Locate_Regular_File
299 return String_Access
;
300 -- Try to locate a regular file whose name is given by File_Name in the
301 -- directories listed in Path. If a file is found, its full pathname is
302 -- returned; otherwise, a null pointer is returned. If the File_Name given
303 -- is an absolute pathname, then Locate_Regular_File just checks that the
304 -- file exists and is a regular file. Otherwise, the Path argument is
305 -- parsed according to OS conventions, and for each directory in the Path
306 -- a check is made if File_Name is a relative pathname of a regular file
307 -- from that directory.
309 -- Note that this function allocates some memory for the returned value.
310 -- This memory needs to be deallocated after use.
312 function Get_Debuggable_Suffix
return String_Access
;
313 -- Return the debuggable suffix convention. Usually this is the same as
314 -- the convention for Get_Executable_Suffix.
316 -- Note that this function allocates some memory for the returned value.
317 -- This memory needs to be deallocated after use.
319 function Get_Executable_Suffix
return String_Access
;
320 -- Return the executable suffix convention.
322 -- Note that this function allocates some memory for the returned value.
323 -- This memory needs to be deallocated after use.
325 function Get_Object_Suffix
return String_Access
;
326 -- Return the object suffix convention.
328 -- Note that this function allocates some memory for the returned value.
329 -- This memory needs to be deallocated after use.
331 -- The following section contains low-level routines using addresses to
332 -- pass file name and executable name. In each routine the name must be
333 -- Nul-Terminated. For complete documentation refer to the equivalent
334 -- routine (but using string) defined above.
336 subtype C_File_Name
is System
.Address
;
337 -- This subtype is used to document that a parameter is the address
338 -- of a null-terminated string containing the name of a file.
343 return File_Descriptor
;
345 function Open_Read_Write
348 return File_Descriptor
;
353 return File_Descriptor
;
355 function Create_New_File
358 return File_Descriptor
;
360 procedure Delete_File
(Name
: C_File_Name
; Success
: out Boolean);
362 procedure Rename_File
363 (Old_Name
: C_File_Name
;
364 New_Name
: C_File_Name
;
365 Success
: out Boolean);
367 function File_Time_Stamp
(Name
: C_File_Name
) return OS_Time
;
369 function Is_Regular_File
(Name
: C_File_Name
) return Boolean;
371 function Is_Directory
(Name
: C_File_Name
) return Boolean;
373 function Is_Writable_File
(Name
: C_File_Name
) return Boolean;
375 function Locate_Regular_File
376 (File_Name
: C_File_Name
;
378 return String_Access
;
384 type Argument_List
is array (Positive range <>) of String_Access
;
385 -- Type used for argument list in call to Spawn. The lower bound
386 -- of the array should be 1, and the length of the array indicates
387 -- the number of arguments.
389 type Argument_List_Access
is access all Argument_List
;
390 -- Type used to return an Argument_List without dragging in secondary
394 (Program_Name
: String;
395 Args
: Argument_List
;
396 Success
: out Boolean);
397 -- The first parameter of function Spawn is the name of the executable.
398 -- The second parameter contains the arguments to be passed to the
399 -- program. Success is False if the named program could not be spawned
400 -- or its execution completed unsuccessfully. Note that the caller will
401 -- be blocked until the execution of the spawned program is complete.
402 -- For maximum portability, use a full path name for the Program_Name
403 -- argument. On some systems (notably Unix systems) a simple file
404 -- name may also work (if the executable can be located in the path).
406 -- Note: Arguments that contain spaces and/or quotes such as
407 -- "--GCC=gcc -v" or "--GCC=""gcc-v""" are not portable
408 -- across OSes. They may or may not have the desired effect.
411 (Program_Name
: String;
412 Args
: Argument_List
)
414 -- Like above, but as function returning the exact exit status
416 type Process_Id
is private;
417 -- A private type used to identify a process activated by the following
418 -- non-blocking call. The only meaningful operation on this type is a
419 -- comparison for equality.
421 Invalid_Pid
: constant Process_Id
;
422 -- A special value used to indicate errors, as described below.
424 function Non_Blocking_Spawn
425 (Program_Name
: String;
426 Args
: Argument_List
)
428 -- This is a non blocking call. The Process_Id of the spawned process
429 -- is returned. Parameters are to be used as in Spawn. If Invalid_Id
430 -- is returned the program could not be spawned.
432 procedure Wait_Process
(Pid
: out Process_Id
; Success
: out Boolean);
433 -- Wait for the completion of any of the processes created by previous
434 -- calls to Non_Blocking_Spawn. The caller will be suspended until one
435 -- of these processes terminates (normally or abnormally). If any of
436 -- these subprocesses terminates prior to the call to Wait_Process (and
437 -- has not been returned by a previous call to Wait_Process), then the
438 -- call to Wait_Process is immediate. Pid identifies the process that
439 -- has terminated (matching the value returned from Non_Blocking_Spawn).
440 -- Success is set to True if this sub-process terminated successfully.
441 -- If Pid = Invalid_Id, there were no subprocesses left to wait on.
443 function Argument_String_To_List
444 (Arg_String
: String)
445 return Argument_List_Access
;
446 -- Take a string that is a program and it's arguments and parse it into
453 function Getenv
(Name
: String) return String_Access
;
454 -- Get the value of the environment variable. Returns an access
455 -- to the empty string if the environment variable does not exist
456 -- or has an explicit null value (in some operating systems these
457 -- are distinct cases, in others they are not; this interface
458 -- abstracts away that difference.
460 procedure Setenv
(Name
: String; Value
: String);
461 -- Set the value of the environment variable Name to Value. This call
462 -- modifies the current environment, but does not modify the parent
463 -- process environment. After a call to Setenv, Getenv (Name) will
464 -- always return a String_Access referencing the same String as Value.
465 -- This is true also for the null string case (the actual effect may
466 -- be to either set an explicit null as the value, or to remove the
467 -- entry, this is operating system dependent). Note that any following
468 -- calls to Spawn will pass an environment to the spawned process that
469 -- includes the changes made by Setenv calls. This procedure is not
470 -- available under VMS.
472 procedure OS_Exit
(Status
: Integer);
473 pragma Import
(C
, OS_Exit
, "__gnat_os_exit");
474 -- Exit to OS with given status code (program is terminated)
477 pragma Import
(C
, OS_Abort
, "abort");
478 -- Exit to OS signalling an abort (traceback or other appropriate
479 -- diagnostic information should be given if possible, or entry made
480 -- to the debugger if that is possible).
482 function Errno
return Integer;
483 pragma Import
(C
, Errno
, "__get_errno");
484 -- Return the task-safe last error number.
486 procedure Set_Errno
(Errno
: Integer);
487 pragma Import
(C
, Set_Errno
, "__set_errno");
488 -- Set the task-safe error number.
490 Directory_Separator
: constant Character;
491 -- The character that is used to separate parts of a pathname.
493 Path_Separator
: constant Character;
494 -- The character to separate paths in an environment variable value.
497 pragma Import
(C
, Path_Separator
, "__gnat_path_separator");
498 pragma Import
(C
, Directory_Separator
, "__gnat_dir_separator");
500 type OS_Time
is new Integer;
502 type File_Descriptor
is new Integer;
504 Standin
: constant File_Descriptor
:= 0;
505 Standout
: constant File_Descriptor
:= 1;
506 Standerr
: constant File_Descriptor
:= 2;
507 Invalid_FD
: constant File_Descriptor
:= -1;
509 type Process_Id
is new Integer;
510 Invalid_Pid
: constant Process_Id
:= -1;