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-2013, 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 Filename_Encoding is (UTF8, ASCII_8bits, Unspecified);
66   for Filename_Encoding use (UTF8 => 0, ASCII_8bits => 1, Unspecified => 2);
67   pragma Convention (C, Filename_Encoding);
68   --  Describes the filename's encoding
69
70   --------------------
71   -- GCC intrinsics --
72   --------------------
73
74   --  The following functions are imported with convention Intrinsic so that
75   --  we take advantage of back-end builtins if present (else we fall back
76   --  to C library functions by the same names).
77
78   function strlen (A : System.Address) return size_t;
79   pragma Import (Intrinsic, strlen, "strlen");
80
81   procedure strncpy (dest, src : System.Address; n : size_t);
82   pragma Import (Intrinsic, strncpy, "strncpy");
83
84   -------------------------------
85   -- Other C runtime functions --
86   -------------------------------
87
88   function atoi (A : System.Address) return Integer;
89   pragma Import (C, atoi, "atoi");
90
91   procedure clearerr (stream : FILEs);
92   pragma Import (C, clearerr, "clearerr");
93
94   function dup  (handle : int) return int;
95   pragma Import (C, dup, "dup");
96
97   function dup2 (from, to : int) return int;
98   pragma Import (C, dup2, "dup2");
99
100   function fclose (stream : FILEs) return int;
101   pragma Import (C, fclose, "fclose");
102
103   function fdopen (handle : int; mode : chars) return FILEs;
104   pragma Import (C, fdopen, "fdopen");
105
106   function fflush (stream : FILEs) return int;
107   pragma Import (C, fflush, "fflush");
108
109   function fgetc (stream : FILEs) return int;
110   pragma Import (C, fgetc, "fgetc");
111
112   function fgets (strng : chars; n : int; stream : FILEs) return chars;
113   pragma Import (C, fgets, "fgets");
114
115   function fopen
116     (filename : chars;
117      mode     : chars;
118      encoding : Filename_Encoding := Unspecified;
119      vms_form : chars := System.Null_Address) return FILEs;
120   pragma Import (C, fopen, "__gnat_fopen");
121
122   function fputc (C : int; stream : FILEs) return int;
123   pragma Import (C, fputc, "fputc");
124
125   function fputs (Strng : chars; Stream : FILEs) return int;
126   pragma Import (C, fputs, "fputs");
127
128   procedure free (Ptr : System.Address);
129   pragma Import (C, free, "free");
130
131   function freopen
132     (filename : chars;
133      mode     : chars;
134      stream   : FILEs;
135      encoding : Filename_Encoding := Unspecified;
136      vms_form : chars := System.Null_Address) return FILEs;
137   pragma Import (C, freopen, "__gnat_freopen");
138
139   function fseek
140     (stream : FILEs;
141      offset : long;
142      origin : int) return int;
143   pragma Import (C, fseek, "fseek");
144
145   function fseek64
146     (stream : FILEs;
147      offset : ssize_t;
148      origin : int) return int;
149   pragma Import (C, fseek64, "__gnat_fseek64");
150
151   function ftell (stream : FILEs) return long;
152   pragma Import (C, ftell, "ftell");
153
154   function ftell64 (stream : FILEs) return ssize_t;
155   pragma Import (C, ftell64, "__gnat_ftell64");
156
157   function getenv (S : String) return System.Address;
158   pragma Import (C, getenv, "getenv");
159
160   function isatty (handle : int) return int;
161   pragma Import (C, isatty, "isatty");
162
163   function lseek (fd : int; offset : off_t; direction : int) return off_t;
164   pragma Import (C, lseek, "lseek");
165
166   function malloc (Size : size_t) return System.Address;
167   pragma Import (C, malloc, "malloc");
168
169   procedure memcpy (S1 : System.Address; S2 : System.Address; N : size_t);
170   pragma Import (C, memcpy, "memcpy");
171
172   procedure memmove (S1 : System.Address; S2 : System.Address; N : size_t);
173   pragma Import (C, memmove, "memmove");
174
175   procedure mktemp (template : chars);
176   pragma Import (C, mktemp, "mktemp");
177
178   function pclose (stream : System.Address) return int;
179   pragma Import (C, pclose, "pclose");
180
181   function popen (command, mode : System.Address) return System.Address;
182   pragma Import (C, popen, "popen");
183
184   function realloc
185     (Ptr : System.Address; Size : size_t) return System.Address;
186   pragma Import (C, realloc, "realloc");
187
188   procedure rewind (stream : FILEs);
189   pragma Import (C, rewind, "rewind");
190
191   function rmdir (dir_name : String) return int;
192   pragma Import (C, rmdir, "__gnat_rmdir");
193
194   function chdir (dir_name : String) return int;
195   pragma Import (C, chdir, "__gnat_chdir");
196
197   function mkdir
198     (dir_name : String;
199      encoding : Filename_Encoding := Unspecified) return int;
200   pragma Import (C, mkdir, "__gnat_mkdir");
201
202   function setvbuf
203     (stream : FILEs;
204      buffer : chars;
205      mode   : int;
206      size   : size_t) return int;
207   pragma Import (C, setvbuf, "setvbuf");
208
209   procedure tmpnam (str : chars);
210   pragma Import (C, tmpnam, "tmpnam");
211
212   function tmpfile return FILEs;
213   pragma Import (C, tmpfile, "tmpfile");
214
215   function ungetc (c : int; stream : FILEs) return int;
216   pragma Import (C, ungetc, "ungetc");
217
218   function unlink (filename : chars) return int;
219   pragma Import (C, unlink, "__gnat_unlink");
220
221   function open (filename : chars; oflag : int) return int;
222   pragma Import (C, open, "open");
223
224   function close (fd : int) return int;
225   pragma Import (C, close, "close");
226
227   function read (fd : int; buffer : chars; count : size_t) return ssize_t;
228   pragma Import (C, read, "read");
229
230   function write (fd : int; buffer : chars; count : size_t) return ssize_t;
231   pragma Import (C, write, "write");
232
233end System.CRTL;
234