1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- S Y S T E M . C R T L --
9 -- Copyright (C) 2003-2014, 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 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 -- This package provides the low level interface to the C runtime library
34 pragma Compiler_Unit_Warning
;
36 with System
.Parameters
;
38 package System
.CRTL
is
41 subtype chars
is System
.Address
;
42 -- Pointer to null-terminated array of characters
43 -- Should use Interfaces.C.Strings types instead, but this causes bootstrap
44 -- issues as i-c contains Ada 2005 specific features, not compatible with
45 -- older, Ada 95-only base compilers???
47 subtype DIRs
is System
.Address
;
48 -- Corresponds to the C type DIR*
50 subtype FILEs
is System
.Address
;
51 -- Corresponds to the C type FILE*
53 subtype int
is Integer;
55 type long
is range -(2 ** (System
.Parameters
.long_bits
- 1))
56 .. +(2 ** (System
.Parameters
.long_bits
- 1)) - 1;
58 subtype off_t
is Long_Integer;
60 type size_t
is mod 2 ** Standard
'Address_Size;
62 type ssize_t
is range -(2 ** (Standard
'Address_Size - 1))
63 .. +(2 ** (Standard
'Address_Size - 1)) - 1;
65 type int64
is new Long_Long_Integer;
66 -- Note: we use Long_Long_Integer'First instead of -2 ** 63 to allow this
67 -- unit to compile when using custom target configuration files where the
68 -- maximum integer is 32 bits. This is useful for static analysis tools
69 -- such as SPARK or CodePeer. In the normal case, Long_Long_Integer is
70 -- always 64-bits so there is no difference.
72 type Filename_Encoding
is (UTF8
, ASCII_8bits
, Unspecified
);
73 for Filename_Encoding
use (UTF8
=> 0, ASCII_8bits
=> 1, Unspecified
=> 2);
74 pragma Convention
(C
, Filename_Encoding
);
75 -- Describes the filename's encoding
81 -- The following functions are imported with convention Intrinsic so that
82 -- we take advantage of back-end builtins if present (else we fall back
83 -- to C library functions by the same names).
85 function strlen
(A
: System
.Address
) return size_t
;
86 pragma Import
(Intrinsic
, strlen
, "strlen");
88 procedure strncpy
(dest
, src
: System
.Address
; n
: size_t
);
89 pragma Import
(Intrinsic
, strncpy
, "strncpy");
91 -------------------------------
92 -- Other C runtime functions --
93 -------------------------------
95 function atoi
(A
: System
.Address
) return Integer;
96 pragma Import
(C
, atoi
, "atoi");
98 procedure clearerr
(stream
: FILEs
);
99 pragma Import
(C
, clearerr
, "clearerr");
101 function dup
(handle
: int
) return int
;
102 pragma Import
(C
, dup
, "dup");
104 function dup2
(from
, to
: int
) return int
;
105 pragma Import
(C
, dup2
, "dup2");
107 function fclose
(stream
: FILEs
) return int
;
108 pragma Import
(C
, fclose
, "fclose");
110 function fdopen
(handle
: int
; mode
: chars
) return FILEs
;
111 pragma Import
(C
, fdopen
, "fdopen");
113 function fflush
(stream
: FILEs
) return int
;
114 pragma Import
(C
, fflush
, "fflush");
116 function fgetc
(stream
: FILEs
) return int
;
117 pragma Import
(C
, fgetc
, "fgetc");
119 function fgets
(strng
: chars
; n
: int
; stream
: FILEs
) return chars
;
120 pragma Import
(C
, fgets
, "fgets");
125 encoding
: Filename_Encoding
:= Unspecified
) return FILEs
;
126 pragma Import
(C
, fopen
, "__gnat_fopen");
128 function fputc
(C
: int
; stream
: FILEs
) return int
;
129 pragma Import
(C
, fputc
, "fputc");
131 function fputwc
(C
: int
; stream
: FILEs
) return int
;
132 pragma Import
(C
, fputwc
, "__gnat_fputwc");
134 function fputs
(Strng
: chars
; Stream
: FILEs
) return int
;
135 pragma Import
(C
, fputs
, "fputs");
137 procedure free
(Ptr
: System
.Address
);
138 pragma Import
(C
, free
, "free");
144 encoding
: Filename_Encoding
:= Unspecified
) return FILEs
;
145 pragma Import
(C
, freopen
, "__gnat_freopen");
150 origin
: int
) return int
;
151 pragma Import
(C
, fseek
, "fseek");
156 origin
: int
) return int
;
157 pragma Import
(C
, fseek64
, "__gnat_fseek64");
159 function ftell
(stream
: FILEs
) return long
;
160 pragma Import
(C
, ftell
, "ftell");
162 function ftell64
(stream
: FILEs
) return int64
;
163 pragma Import
(C
, ftell64
, "__gnat_ftell64");
165 function getenv
(S
: String) return System
.Address
;
166 pragma Import
(C
, getenv
, "getenv");
168 function isatty
(handle
: int
) return int
;
169 pragma Import
(C
, isatty
, "isatty");
171 function lseek
(fd
: int
; offset
: off_t
; direction
: int
) return off_t
;
172 pragma Import
(C
, lseek
, "lseek");
174 function malloc
(Size
: size_t
) return System
.Address
;
175 pragma Import
(C
, malloc
, "malloc");
177 procedure memcpy
(S1
: System
.Address
; S2
: System
.Address
; N
: size_t
);
178 pragma Import
(C
, memcpy
, "memcpy");
180 procedure memmove
(S1
: System
.Address
; S2
: System
.Address
; N
: size_t
);
181 pragma Import
(C
, memmove
, "memmove");
183 procedure mktemp
(template
: chars
);
184 pragma Import
(C
, mktemp
, "mktemp");
186 function pclose
(stream
: System
.Address
) return int
;
187 pragma Import
(C
, pclose
, "pclose");
189 function popen
(command
, mode
: System
.Address
) return System
.Address
;
190 pragma Import
(C
, popen
, "popen");
193 (Ptr
: System
.Address
; Size
: size_t
) return System
.Address
;
194 pragma Import
(C
, realloc
, "realloc");
196 procedure rewind
(stream
: FILEs
);
197 pragma Import
(C
, rewind
, "rewind");
199 function rmdir
(dir_name
: String) return int
;
200 pragma Import
(C
, rmdir
, "__gnat_rmdir");
202 function chdir
(dir_name
: String) return int
;
203 pragma Import
(C
, chdir
, "__gnat_chdir");
207 encoding
: Filename_Encoding
:= Unspecified
) return int
;
208 pragma Import
(C
, mkdir
, "__gnat_mkdir");
214 size
: size_t
) return int
;
215 pragma Import
(C
, setvbuf
, "setvbuf");
217 procedure tmpnam
(str
: chars
);
218 pragma Import
(C
, tmpnam
, "tmpnam");
220 function tmpfile
return FILEs
;
221 pragma Import
(C
, tmpfile
, "tmpfile");
223 function ungetc
(c
: int
; stream
: FILEs
) return int
;
224 pragma Import
(C
, ungetc
, "ungetc");
226 function unlink
(filename
: chars
) return int
;
227 pragma Import
(C
, unlink
, "__gnat_unlink");
229 function open
(filename
: chars
; oflag
: int
) return int
;
230 pragma Import
(C
, open
, "__gnat_open");
232 function close
(fd
: int
) return int
;
233 pragma Import
(C
, close
, "close");
235 function read
(fd
: int
; buffer
: chars
; count
: size_t
) return ssize_t
;
236 pragma Import
(C
, read
, "read");
238 function write
(fd
: int
; buffer
: chars
; count
: size_t
) return ssize_t
;
239 pragma Import
(C
, write
, "write");