1------------------------------------------------------------------------------
2--                                                                          --
3--                        GNAT RUN-TIME COMPONENTS                          --
4--                                                                          --
5--                          S Y S T E M . C R T L                           --
6--                                                                          --
7--                                 S p e c                                  --
8--                                                                          --
9--          Copyright (C) 2003-2014, Free Software Foundation, Inc.         --
10--                                                                          --
11-- GNAT is free software;  you can  redistribute it  and/or modify it under --
12-- terms of the  GNU General Public License as published  by the Free Soft- --
13-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16-- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
17--                                                                          --
18-- As a special exception under Section 7 of GPL version 3, you are granted --
19-- additional permissions described in the GCC Runtime Library Exception,   --
20-- version 3.1, as published by the Free Software Foundation.               --
21--                                                                          --
22-- You should have received a copy of the GNU General Public License and    --
23-- a copy of the GCC Runtime Library Exception along with this program;     --
24-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
25-- <http://www.gnu.org/licenses/>.                                          --
26--                                                                          --
27-- GNAT was originally developed  by the GNAT team at  New York University. --
28-- Extensive contributions were provided by Ada Core Technologies Inc.      --
29--                                                                          --
30------------------------------------------------------------------------------
31
32--  This package provides the low level interface to the C runtime library
33
34pragma Compiler_Unit_Warning;
35
36with System.Parameters;
37
38package System.CRTL is
39   pragma Preelaborate;
40
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???
46
47   subtype DIRs is System.Address;
48   --  Corresponds to the C type DIR*
49
50   subtype FILEs is System.Address;
51   --  Corresponds to the C type FILE*
52
53   subtype int is Integer;
54
55   type long is range -(2 ** (System.Parameters.long_bits - 1))
56                   .. +(2 ** (System.Parameters.long_bits - 1)) - 1;
57
58   subtype off_t is Long_Integer;
59
60   type size_t is mod 2 ** Standard'Address_Size;
61
62   type ssize_t is range -(2 ** (Standard'Address_Size - 1))
63                      .. +(2 ** (Standard'Address_Size - 1)) - 1;
64
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.
71
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
76
77   --------------------
78   -- GCC intrinsics --
79   --------------------
80
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).
84
85   function strlen (A : System.Address) return size_t;
86   pragma Import (Intrinsic, strlen, "strlen");
87
88   procedure strncpy (dest, src : System.Address; n : size_t);
89   pragma Import (Intrinsic, strncpy, "strncpy");
90
91   -------------------------------
92   -- Other C runtime functions --
93   -------------------------------
94
95   function atoi (A : System.Address) return Integer;
96   pragma Import (C, atoi, "atoi");
97
98   procedure clearerr (stream : FILEs);
99   pragma Import (C, clearerr, "clearerr");
100
101   function dup  (handle : int) return int;
102   pragma Import (C, dup, "dup");
103
104   function dup2 (from, to : int) return int;
105   pragma Import (C, dup2, "dup2");
106
107   function fclose (stream : FILEs) return int;
108   pragma Import (C, fclose, "fclose");
109
110   function fdopen (handle : int; mode : chars) return FILEs;
111   pragma Import (C, fdopen, "fdopen");
112
113   function fflush (stream : FILEs) return int;
114   pragma Import (C, fflush, "fflush");
115
116   function fgetc (stream : FILEs) return int;
117   pragma Import (C, fgetc, "fgetc");
118
119   function fgets (strng : chars; n : int; stream : FILEs) return chars;
120   pragma Import (C, fgets, "fgets");
121
122   function fopen
123     (filename : chars;
124      mode     : chars;
125      encoding : Filename_Encoding := Unspecified) return FILEs;
126   pragma Import (C, fopen, "__gnat_fopen");
127
128   function fputc (C : int; stream : FILEs) return int;
129   pragma Import (C, fputc, "fputc");
130
131   function fputwc (C : int; stream : FILEs) return int;
132   pragma Import (C, fputwc, "__gnat_fputwc");
133
134   function fputs (Strng : chars; Stream : FILEs) return int;
135   pragma Import (C, fputs, "fputs");
136
137   procedure free (Ptr : System.Address);
138   pragma Import (C, free, "free");
139
140   function freopen
141     (filename : chars;
142      mode     : chars;
143      stream   : FILEs;
144      encoding : Filename_Encoding := Unspecified) return FILEs;
145   pragma Import (C, freopen, "__gnat_freopen");
146
147   function fseek
148     (stream : FILEs;
149      offset : long;
150      origin : int) return int;
151   pragma Import (C, fseek, "fseek");
152
153   function fseek64
154     (stream : FILEs;
155      offset : int64;
156      origin : int) return int;
157   pragma Import (C, fseek64, "__gnat_fseek64");
158
159   function ftell (stream : FILEs) return long;
160   pragma Import (C, ftell, "ftell");
161
162   function ftell64 (stream : FILEs) return int64;
163   pragma Import (C, ftell64, "__gnat_ftell64");
164
165   function getenv (S : String) return System.Address;
166   pragma Import (C, getenv, "getenv");
167
168   function isatty (handle : int) return int;
169   pragma Import (C, isatty, "isatty");
170
171   function lseek (fd : int; offset : off_t; direction : int) return off_t;
172   pragma Import (C, lseek, "lseek");
173
174   function malloc (Size : size_t) return System.Address;
175   pragma Import (C, malloc, "malloc");
176
177   procedure memcpy (S1 : System.Address; S2 : System.Address; N : size_t);
178   pragma Import (C, memcpy, "memcpy");
179
180   procedure memmove (S1 : System.Address; S2 : System.Address; N : size_t);
181   pragma Import (C, memmove, "memmove");
182
183   procedure mktemp (template : chars);
184   pragma Import (C, mktemp, "mktemp");
185
186   function pclose (stream : System.Address) return int;
187   pragma Import (C, pclose, "pclose");
188
189   function popen (command, mode : System.Address) return System.Address;
190   pragma Import (C, popen, "popen");
191
192   function realloc
193     (Ptr : System.Address; Size : size_t) return System.Address;
194   pragma Import (C, realloc, "realloc");
195
196   procedure rewind (stream : FILEs);
197   pragma Import (C, rewind, "rewind");
198
199   function rmdir (dir_name : String) return int;
200   pragma Import (C, rmdir, "__gnat_rmdir");
201
202   function chdir (dir_name : String) return int;
203   pragma Import (C, chdir, "__gnat_chdir");
204
205   function mkdir
206     (dir_name : String;
207      encoding : Filename_Encoding := Unspecified) return int;
208   pragma Import (C, mkdir, "__gnat_mkdir");
209
210   function setvbuf
211     (stream : FILEs;
212      buffer : chars;
213      mode   : int;
214      size   : size_t) return int;
215   pragma Import (C, setvbuf, "setvbuf");
216
217   procedure tmpnam (str : chars);
218   pragma Import (C, tmpnam, "tmpnam");
219
220   function tmpfile return FILEs;
221   pragma Import (C, tmpfile, "tmpfile");
222
223   function ungetc (c : int; stream : FILEs) return int;
224   pragma Import (C, ungetc, "ungetc");
225
226   function unlink (filename : chars) return int;
227   pragma Import (C, unlink, "__gnat_unlink");
228
229   function open (filename : chars; oflag : int) return int;
230   pragma Import (C, open, "__gnat_open");
231
232   function close (fd : int) return int;
233   pragma Import (C, close, "close");
234
235   function read (fd : int; buffer : chars; count : size_t) return ssize_t;
236   pragma Import (C, read, "read");
237
238   function write (fd : int; buffer : chars; count : size_t) return ssize_t;
239   pragma Import (C, write, "write");
240
241end System.CRTL;
242