annotate gcc/ada/libgnat/s-os_lib.ads @ 111:04ced10e8804

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