1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- I N T E R F A C E S . C _ S T R E A M S --
9 -- Copyright (C) 1995-2006, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 -- This package is a thin binding to selected functions in the C
35 -- library that provide a complete interface for handling C streams.
39 package Interfaces
.C_Streams
is
42 subtype chars
is System
.CRTL
.chars
;
43 subtype FILEs
is System
.CRTL
.FILEs
;
44 subtype int
is System
.CRTL
.int
;
45 subtype long
is System
.CRTL
.long
;
46 subtype size_t
is System
.CRTL
.size_t
;
47 subtype voids
is System
.Address
;
49 NULL_Stream
: constant FILEs
;
50 -- Value returned (NULL in C) to indicate an fdopen/fopen/tmpfile error
52 ----------------------------------
53 -- Constants Defined in stdio.h --
54 ----------------------------------
57 -- Used by a number of routines to indicate error or end of file
62 -- Used to indicate buffering mode for setvbuf call
64 L_tmpnam
: constant int
;
65 -- Maximum length of file name that can be returned by tmpnam
67 SEEK_CUR
: constant int
;
68 SEEK_END
: constant int
;
69 SEEK_SET
: constant int
;
70 -- Used to indicate origin for fseek call
72 function stdin
return FILEs
;
73 function stdout
return FILEs
;
74 function stderr
return FILEs
;
75 -- Streams associated with standard files
77 --------------------------
78 -- Standard C functions --
79 --------------------------
81 -- The functions selected below are ones that are available in DOS,
82 -- OS/2, UNIX and Xenix (but not necessarily in ANSI C). These are
83 -- very thin interfaces which copy exactly the C headers. For more
84 -- documentation on these functions, see the Microsoft C "Run-Time
85 -- Library Reference" (Microsoft Press, 1990, ISBN 1-55615-225-6),
86 -- which includes useful information on system compatibility.
88 procedure clearerr
(stream
: FILEs
) renames System
.CRTL
.clearerr
;
90 function fclose
(stream
: FILEs
) return int
renames System
.CRTL
.fclose
;
92 function fdopen
(handle
: int
; mode
: chars
) return FILEs
93 renames System
.CRTL
.fdopen
;
95 function feof
(stream
: FILEs
) return int
;
97 function ferror
(stream
: FILEs
) return int
;
99 function fflush
(stream
: FILEs
) return int
renames System
.CRTL
.fflush
;
101 function fgetc
(stream
: FILEs
) return int
renames System
.CRTL
.fgetc
;
103 function fgets
(strng
: chars
; n
: int
; stream
: FILEs
) return chars
104 renames System
.CRTL
.fgets
;
106 function fileno
(stream
: FILEs
) return int
;
111 encoding
: System
.CRTL
.Filename_Encoding
:= System
.CRTL
.UTF8
)
113 renames System
.CRTL
.fopen
;
114 -- Note: to maintain target independence, use text_translation_required,
115 -- a boolean variable defined in sysdep.c to deal with the target
116 -- dependent text translation requirement. If this variable is set,
117 -- then b/t should be appended to the standard mode argument to set
118 -- the text translation mode off or on as required.
120 function fputc
(C
: int
; stream
: FILEs
) return int
121 renames System
.CRTL
.fputc
;
123 function fputs
(Strng
: chars
; Stream
: FILEs
) return int
124 renames System
.CRTL
.fputs
;
130 stream
: FILEs
) return size_t
;
137 stream
: FILEs
) return size_t
;
138 -- Same as normal fread, but has a parameter 'index' that indicates
139 -- the starting index for the read within 'buffer' (which must be the
140 -- address of the beginning of a whole array object with an assumed
141 -- zero base). This is needed for systems that do not support taking
142 -- the address of an element within an array.
148 encoding
: System
.CRTL
.Filename_Encoding
:= System
.CRTL
.UTF8
)
150 renames System
.CRTL
.freopen
;
155 origin
: int
) return int
156 renames System
.CRTL
.fseek
;
158 function ftell
(stream
: FILEs
) return long
159 renames System
.CRTL
.ftell
;
165 stream
: FILEs
) return size_t
;
167 function isatty
(handle
: int
) return int
renames System
.CRTL
.isatty
;
169 procedure mktemp
(template
: chars
) renames System
.CRTL
.mktemp
;
170 -- The return value (which is just a pointer to template) is discarded
172 procedure rewind
(stream
: FILEs
) renames System
.CRTL
.rewind
;
178 size
: size_t
) return int
;
180 procedure tmpnam
(string : chars
) renames System
.CRTL
.tmpnam
;
181 -- The parameter must be a pointer to a string buffer of at least L_tmpnam
182 -- bytes (the call with a null parameter is not supported). The returned
183 -- value, which is just a copy of the input argument, is discarded.
185 function tmpfile
return FILEs
renames System
.CRTL
.tmpfile
;
187 function ungetc
(c
: int
; stream
: FILEs
) return int
188 renames System
.CRTL
.ungetc
;
190 function unlink
(filename
: chars
) return int
191 renames System
.CRTL
.unlink
;
193 ---------------------
194 -- Extra functions --
195 ---------------------
197 -- These functions supply slightly thicker bindings than those above.
198 -- They are derived from functions in the C Run-Time Library, but may
199 -- do a bit more work than just directly calling one of the Library
202 function file_exists
(name
: chars
) return int
;
203 -- Tests if given name corresponds to an existing file
205 function is_regular_file
(handle
: int
) return int
;
206 -- Tests if given handle is for a regular file (result 1) or for a
207 -- non-regular file (pipe or device, result 0).
209 ---------------------------------
210 -- Control of Text/Binary Mode --
211 ---------------------------------
213 -- If text_translation_required is true, then the following functions may
214 -- be used to dynamically switch a file from binary to text mode or vice
215 -- versa. These functions have no effect if text_translation_required is
216 -- false (i.e. in normal unix mode). Use fileno to get a stream handle.
218 procedure set_binary_mode
(handle
: int
);
219 procedure set_text_mode
(handle
: int
);
221 ----------------------------
222 -- Full Path Name support --
223 ----------------------------
225 procedure full_name
(nam
: chars
; buffer
: chars
);
226 -- Given a NUL terminated string representing a file name, returns in
227 -- buffer a NUL terminated string representing the full path name for
228 -- the file name. On systems where it is relevant the drive is also part
229 -- of the full path name. It is the responsibility of the caller to
230 -- pass an actual parameter for buffer that is big enough for any full
231 -- path name. Use max_path_len given below as the size of buffer.
233 max_path_len
: Integer;
234 -- Maximum length of an allowable full path name on the system,
235 -- including a terminating NUL character.
238 -- The following functions are specialized in the body depending on the
241 pragma Inline
(fread
);
242 pragma Inline
(fwrite
);
243 pragma Inline
(setvbuf
);
245 pragma Import
(C
, file_exists
, "__gnat_file_exists");
246 pragma Import
(C
, is_regular_file
, "__gnat_is_regular_file_fd");
248 pragma Import
(C
, set_binary_mode
, "__gnat_set_binary_mode");
249 pragma Import
(C
, set_text_mode
, "__gnat_set_text_mode");
251 pragma Import
(C
, max_path_len
, "__gnat_max_path_len");
252 pragma Import
(C
, full_name
, "__gnat_full_name");
254 -- The following may be implemented as macros, and so are supported
255 -- via an interface function in the a-cstrea.c file.
257 pragma Import
(C
, feof
, "__gnat_feof");
258 pragma Import
(C
, ferror
, "__gnat_ferror");
259 pragma Import
(C
, fileno
, "__gnat_fileno");
261 pragma Import
(C
, EOF
, "__gnat_constant_eof");
262 pragma Import
(C
, IOFBF
, "__gnat_constant_iofbf");
263 pragma Import
(C
, IOLBF
, "__gnat_constant_iolbf");
264 pragma Import
(C
, IONBF
, "__gnat_constant_ionbf");
265 pragma Import
(C
, SEEK_CUR
, "__gnat_constant_seek_cur");
266 pragma Import
(C
, SEEK_END
, "__gnat_constant_seek_end");
267 pragma Import
(C
, SEEK_SET
, "__gnat_constant_seek_set");
268 pragma Import
(C
, L_tmpnam
, "__gnat_constant_l_tmpnam");
270 pragma Import
(C
, stderr
, "__gnat_constant_stderr");
271 pragma Import
(C
, stdin
, "__gnat_constant_stdin");
272 pragma Import
(C
, stdout
, "__gnat_constant_stdout");
274 NULL_Stream
: constant FILEs
:= System
.Null_Address
;
276 end Interfaces
.C_Streams
;