111
|
1 /****************************************************************************
|
|
2 * *
|
|
3 * GNAT RUN-TIME COMPONENTS *
|
|
4 * *
|
|
5 * C S T R E A M S *
|
|
6 * *
|
|
7 * Auxiliary C functions for Interfaces.C.Streams *
|
|
8 * *
|
131
|
9 * Copyright (C) 1992-2018, Free Software Foundation, Inc. *
|
111
|
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 /* Routines required for implementing routines in Interfaces.C.Streams. */
|
|
33
|
|
34 #ifndef _LARGEFILE_SOURCE
|
|
35 #define _LARGEFILE_SOURCE
|
|
36 #endif
|
|
37 #define _FILE_OFFSET_BITS 64
|
|
38 /* the define above will make off_t a 64bit type on GNU/Linux */
|
|
39
|
|
40 #include <stdio.h>
|
|
41 #include <sys/types.h>
|
|
42 #include <sys/stat.h>
|
|
43 #include <unistd.h>
|
|
44
|
|
45 #ifdef _AIX
|
|
46 /* needed to avoid conflicting declarations */
|
|
47 #include <unistd.h>
|
|
48 #include <sys/mman.h>
|
|
49 #endif
|
|
50
|
|
51 #ifdef __vxworks
|
|
52 #include "vxWorks.h"
|
|
53 #endif
|
|
54
|
|
55 #ifdef IN_RTS
|
|
56 #include "tconfig.h"
|
|
57 #include "tsystem.h"
|
|
58 #include <sys/stat.h>
|
|
59 #else
|
|
60 #include "config.h"
|
|
61 #include "system.h"
|
|
62 #endif
|
|
63
|
|
64 #include "adaint.h"
|
|
65
|
|
66 #ifdef __cplusplus
|
|
67 extern "C" {
|
|
68 #endif
|
|
69
|
|
70 #ifdef __linux__
|
|
71 /* Don't use macros on GNU/Linux since they cause incompatible changes between
|
|
72 glibc 2.0 and 2.1 */
|
|
73
|
|
74 #ifdef stderr
|
|
75 # undef stderr
|
|
76 #endif
|
|
77 #ifdef stdin
|
|
78 # undef stdin
|
|
79 #endif
|
|
80 #ifdef stdout
|
|
81 # undef stdout
|
|
82 #endif
|
|
83
|
|
84 #endif
|
|
85
|
|
86 /* Don't use macros versions of this functions on VxWorks since they cause
|
|
87 imcompatible changes in some VxWorks versions */
|
|
88 #ifdef __vxworks
|
|
89 #undef getchar
|
|
90 #undef putchar
|
|
91 #undef feof
|
|
92 #undef ferror
|
|
93 #undef fileno
|
|
94 #endif
|
|
95
|
|
96 /* The _IONBF value in MINGW32 stdio.h is wrong. */
|
|
97 #if defined (WINNT) || defined (_WINNT)
|
|
98 #if OLD_MINGW
|
|
99 #undef _IONBF
|
|
100 #define _IONBF 0004
|
|
101 #endif
|
|
102 #endif
|
|
103
|
|
104 int
|
|
105 __gnat_feof (FILE *stream)
|
|
106 {
|
|
107 return (feof (stream));
|
|
108 }
|
|
109
|
|
110 int
|
|
111 __gnat_ferror (FILE *stream)
|
|
112 {
|
|
113 return (ferror (stream));
|
|
114 }
|
|
115
|
|
116 int
|
|
117 __gnat_fileno (FILE *stream)
|
|
118 {
|
|
119 return (fileno (stream));
|
|
120 }
|
|
121
|
|
122 /* on some systems, the constants for seek are not defined, if so, then
|
|
123 provide the conventional definitions */
|
|
124
|
|
125 #ifndef SEEK_SET
|
|
126 #define SEEK_SET 0 /* Set file pointer to offset */
|
|
127 #define SEEK_CUR 1 /* Set file pointer to its current value plus offset */
|
|
128 #define SEEK_END 2 /* Set file pointer to the size of the file plus offset */
|
|
129 #endif
|
|
130
|
|
131 /* if L_tmpnam is not set, use a large number that should be safe */
|
|
132 #ifndef L_tmpnam
|
|
133 #define L_tmpnam 256
|
|
134 #endif
|
|
135
|
|
136 int __gnat_constant_eof = EOF;
|
|
137 int __gnat_constant_iofbf = _IOFBF;
|
|
138 int __gnat_constant_iolbf = _IOLBF;
|
|
139 int __gnat_constant_ionbf = _IONBF;
|
|
140 int __gnat_constant_l_tmpnam = L_tmpnam;
|
|
141 int __gnat_constant_seek_cur = SEEK_CUR;
|
|
142 int __gnat_constant_seek_end = SEEK_END;
|
|
143 int __gnat_constant_seek_set = SEEK_SET;
|
|
144
|
|
145 FILE *
|
|
146 __gnat_constant_stderr (void)
|
|
147 {
|
|
148 return stderr;
|
|
149 }
|
|
150
|
|
151 FILE *
|
|
152 __gnat_constant_stdin (void)
|
|
153 {
|
|
154 return stdin;
|
|
155 }
|
|
156
|
|
157 FILE *
|
|
158 __gnat_constant_stdout (void)
|
|
159 {
|
|
160 return stdout;
|
|
161 }
|
|
162
|
|
163 char *
|
|
164 __gnat_full_name (char *nam, char *buffer)
|
|
165 {
|
|
166 #ifdef RTSS
|
|
167 /* RTSS applications have no current-directory notion, so RTSS file I/O
|
|
168 requests must use fully qualified path names, such as:
|
|
169 c:\temp\MyFile.txt (for a file system object)
|
|
170 \\.\MyDevice0 (for a device object)
|
|
171 */
|
|
172 if (nam[1] == ':' || nam[0] == '\\')
|
|
173 strcpy (buffer, nam);
|
|
174 else
|
|
175 buffer[0] = '\0';
|
|
176
|
|
177 #elif defined (__MINGW32__)
|
|
178 /* If this is a device file return it as is;
|
|
179 under Windows NT a device file ends with ":". */
|
|
180 if (nam[strlen (nam) - 1] == ':')
|
|
181 strcpy (buffer, nam);
|
|
182 else
|
|
183 {
|
|
184 char *p;
|
|
185
|
|
186 _fullpath (buffer, nam, __gnat_max_path_len);
|
|
187
|
|
188 for (p = buffer; *p; p++)
|
|
189 if (*p == '/')
|
|
190 *p = '\\';
|
|
191 }
|
|
192
|
|
193 #elif defined (__FreeBSD__) || defined (__DragonFly__) || defined (__OpenBSD__)
|
|
194
|
|
195 /* Use realpath function which resolves links and references to . and ..
|
|
196 on those Unix systems that support it. Note that GNU/Linux provides it but
|
|
197 cannot handle more than 5 symbolic links in a full name, so we use the
|
|
198 getcwd approach instead. */
|
|
199 realpath (nam, buffer);
|
|
200
|
131
|
201 #elif defined (__QNX__)
|
|
202
|
|
203 int length;
|
|
204
|
|
205 if (__gnat_is_absolute_path (nam, strlen (nam)))
|
|
206 realpath (nam, buffer);
|
|
207 else
|
|
208 {
|
|
209 length = __gnat_max_path_len;
|
|
210 __gnat_get_current_dir (buffer, &length);
|
|
211 strncat (buffer, nam, __gnat_max_path_len - length - 1);
|
|
212 }
|
|
213
|
111
|
214 #elif defined (__vxworks)
|
|
215
|
|
216 /* On VxWorks systems, an absolute path can be represented (depending on
|
|
217 the host platform) as either /dir/file, or device:/dir/file, or
|
|
218 device:drive_letter:/dir/file. Use the __gnat_is_absolute_path
|
|
219 to verify it. */
|
|
220
|
|
221 int length;
|
|
222
|
|
223 if (__gnat_is_absolute_path (nam, strlen (nam)))
|
|
224 strcpy (buffer, nam);
|
|
225
|
|
226 else
|
|
227 {
|
|
228 length = __gnat_max_path_len;
|
|
229 __gnat_get_current_dir (buffer, &length);
|
|
230 strncat (buffer, nam, __gnat_max_path_len - length - 1);
|
|
231 }
|
|
232
|
|
233 #else
|
|
234 if (nam[0] != '/')
|
|
235 {
|
|
236 char *p = getcwd (buffer, __gnat_max_path_len);
|
|
237
|
|
238 if (p == 0)
|
|
239 {
|
|
240 buffer[0] = '\0';
|
|
241 return 0;
|
|
242 }
|
|
243
|
|
244
|
|
245 /* If the name returned is an absolute path, it is safe to append '/'
|
|
246 to the path and concatenate the name of the file. */
|
|
247 if (buffer[0] == '/')
|
|
248 strcat (buffer, "/");
|
|
249
|
|
250 strcat (buffer, nam);
|
|
251 }
|
|
252 else
|
|
253 strcpy (buffer, nam);
|
|
254 #endif
|
|
255
|
|
256 return buffer;
|
|
257 }
|
|
258
|
|
259 #ifdef _WIN32
|
|
260 /* On Windows we want to use the fseek/fteel supporting large files. This
|
|
261 issue is due to the fact that a long on Win64 is still a 32 bits value */
|
|
262 __int64
|
|
263 __gnat_ftell64 (FILE *stream)
|
|
264 {
|
|
265 return _ftelli64 (stream);
|
|
266 }
|
|
267
|
|
268 int
|
|
269 __gnat_fseek64 (FILE *stream, __int64 offset, int origin)
|
|
270 {
|
|
271 return _fseeki64 (stream, offset, origin);
|
|
272 }
|
|
273
|
|
274 #elif defined (__linux__) || defined (__sun__) || defined (__FreeBSD__) \
|
|
275 || defined (__APPLE__)
|
|
276 /* section for platforms having ftello/fseeko */
|
|
277
|
|
278 __int64
|
|
279 __gnat_ftell64 (FILE *stream)
|
|
280 {
|
|
281 return (__int64)ftello (stream);
|
|
282 }
|
|
283
|
|
284 int
|
|
285 __gnat_fseek64 (FILE *stream, __int64 offset, int origin)
|
|
286 {
|
|
287 /* make sure that the offset is not bigger than the OS off_t, if so return
|
|
288 with error as this mean that we are trying to handle files larger than
|
|
289 2Gb on a patform not supporting it. */
|
|
290 if ((off_t)offset == offset)
|
|
291 return fseeko (stream, (off_t) offset, origin);
|
|
292 else
|
|
293 return -1;
|
|
294 }
|
|
295
|
|
296 #else
|
|
297
|
|
298 __int64
|
|
299 __gnat_ftell64 (FILE *stream)
|
|
300 {
|
|
301 return (__int64)ftell (stream);
|
|
302 }
|
|
303
|
|
304 int
|
|
305 __gnat_fseek64 (FILE *stream, __int64 offset, int origin)
|
|
306 {
|
|
307 /* make sure that the offset is not bigger than the OS off_t, if so return
|
|
308 with error as this mean that we are trying to handle files larger than
|
|
309 2Gb on a patform not supporting it. */
|
|
310 if ((off_t)offset == offset)
|
|
311 return fseek (stream, (off_t) offset, origin);
|
|
312 else
|
|
313 return -1;
|
|
314 }
|
|
315 #endif
|
|
316
|
|
317 /* Returns true if the path names a fifo (i.e. a named pipe). */
|
|
318 int
|
|
319 __gnat_is_fifo (const char* path)
|
|
320 {
|
|
321 /* Posix defines S_ISFIFO as a macro. If the macro doesn't exist, we return
|
|
322 false. */
|
|
323 #ifdef S_ISFIFO
|
|
324 struct stat buf;
|
|
325 const int status = stat(path, &buf);
|
|
326 if (status == 0)
|
|
327 return S_ISFIFO(buf.st_mode);
|
|
328 #endif
|
|
329
|
|
330 /* S_ISFIFO is not available, or stat got an error (probably
|
|
331 file not found). */
|
|
332 return 0;
|
|
333 }
|
|
334
|
|
335 #ifdef __cplusplus
|
|
336 }
|
|
337 #endif
|