1 /****************************************************************************
3 * GNAT RUN-TIME COMPONENTS *
7 * Auxiliary C functions for Interfaces.C.Streams *
9 * Copyright (C) 1992-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 /* Routines required for implementing routines in Interfaces.C.Streams. */
34 #ifndef _LARGEFILE_SOURCE
35 #define _LARGEFILE_SOURCE
37 #define _FILE_OFFSET_BITS 64
38 /* the define above will make off_t a 64bit type on GNU/Linux */
41 #include <sys/types.h>
44 /* needed to avoid conflicting declarations */
73 /* Don't use macros on GNU/Linux since they cause incompatible changes between
88 /* Don't use macros versions of this functions on VxWorks since they cause
89 imcompatible changes in some VxWorks versions */
98 /* The _IONBF value in MINGW32 stdio.h is wrong. */
99 #if defined (WINNT) || defined (_WINNT)
107 __gnat_feof (FILE *stream
)
109 return (feof (stream
));
113 __gnat_ferror (FILE *stream
)
115 return (ferror (stream
));
119 __gnat_fileno (FILE *stream
)
121 return (fileno (stream
));
124 /* on some systems, the constants for seek are not defined, if so, then
125 provide the conventional definitions */
128 #define SEEK_SET 0 /* Set file pointer to offset */
129 #define SEEK_CUR 1 /* Set file pointer to its current value plus offset */
130 #define SEEK_END 2 /* Set file pointer to the size of the file plus offset */
133 /* if L_tmpnam is not set, use a large number that should be safe */
138 int __gnat_constant_eof
= EOF
;
139 int __gnat_constant_iofbf
= _IOFBF
;
140 int __gnat_constant_iolbf
= _IOLBF
;
141 int __gnat_constant_ionbf
= _IONBF
;
142 int __gnat_constant_l_tmpnam
= L_tmpnam
;
143 int __gnat_constant_seek_cur
= SEEK_CUR
;
144 int __gnat_constant_seek_end
= SEEK_END
;
145 int __gnat_constant_seek_set
= SEEK_SET
;
148 __gnat_constant_stderr (void)
154 __gnat_constant_stdin (void)
160 __gnat_constant_stdout (void)
166 __gnat_full_name (char *nam
, char *buffer
)
169 /* RTSS applications have no current-directory notion, so RTSS file I/O
170 requests must use fully qualified path names, such as:
171 c:\temp\MyFile.txt (for a file system object)
172 \\.\MyDevice0 (for a device object)
174 if (nam
[1] == ':' || nam
[0] == '\\')
175 strcpy (buffer
, nam
);
179 #elif defined (__MINGW32__)
180 /* If this is a device file return it as is;
181 under Windows NT a device file ends with ":". */
182 if (nam
[strlen (nam
) - 1] == ':')
183 strcpy (buffer
, nam
);
188 _fullpath (buffer
, nam
, __gnat_max_path_len
);
190 for (p
= buffer
; *p
; p
++)
195 #elif defined (__FreeBSD__)
197 /* Use realpath function which resolves links and references to . and ..
198 on those Unix systems that support it. Note that GNU/Linux provides it but
199 cannot handle more than 5 symbolic links in a full name, so we use the
200 getcwd approach instead. */
201 realpath (nam
, buffer
);
204 strncpy (buffer
, __gnat_to_canonical_file_spec (nam
), __gnat_max_path_len
);
206 if (buffer
[0] == '/' || strchr (buffer
, '!')) /* '!' means decnet node */
207 strncpy (buffer
, __gnat_to_host_file_spec (buffer
), __gnat_max_path_len
);
210 char *nambuffer
= alloca (__gnat_max_path_len
);
212 strncpy (nambuffer
, buffer
, __gnat_max_path_len
);
214 (buffer
, getcwd (buffer
, __gnat_max_path_len
, 0), __gnat_max_path_len
);
215 strncat (buffer
, "/", __gnat_max_path_len
);
216 strncat (buffer
, nambuffer
, __gnat_max_path_len
);
217 strncpy (buffer
, __gnat_to_host_file_spec (buffer
), __gnat_max_path_len
);
220 #elif defined (__vxworks)
222 /* On VxWorks systems, an absolute path can be represented (depending on
223 the host platform) as either /dir/file, or device:/dir/file, or
224 device:drive_letter:/dir/file. Use the __gnat_is_absolute_path
229 if (__gnat_is_absolute_path (nam
, strlen (nam
)))
230 strcpy (buffer
, nam
);
234 length
= __gnat_max_path_len
;
235 __gnat_get_current_dir (buffer
, &length
);
236 strncat (buffer
, nam
, __gnat_max_path_len
- length
- 1);
242 char *p
= getcwd (buffer
, __gnat_max_path_len
);
251 /* If the name returned is an absolute path, it is safe to append '/'
252 to the path and concatenate the name of the file. */
253 if (buffer
[0] == '/')
254 strcat (buffer
, "/");
256 strcat (buffer
, nam
);
259 strcpy (buffer
, nam
);
266 /* On Windows we want to use the fseek/fteel supporting large files. This
267 issue is due to the fact that a long on Win64 is still a 32 bits value */
269 __gnat_ftell64 (FILE *stream
)
271 return _ftelli64 (stream
);
275 __gnat_fseek64 (FILE *stream
, __int64 offset
, int origin
)
277 return _fseeki64 (stream
, offset
, origin
);
280 #elif defined(linux) || defined(sun) \
281 || defined (__FreeBSD__) || defined(__APPLE__)
282 /* section for platforms having ftello/fseeko */
285 __gnat_ftell64 (FILE *stream
)
287 return (__int64
)ftello (stream
);
291 __gnat_fseek64 (FILE *stream
, __int64 offset
, int origin
)
293 /* make sure that the offset is not bigger than the OS off_t, if so return
294 with error as this mean that we are trying to handle files larger than
295 2Gb on a patform not supporting it. */
296 if ((off_t
)offset
== offset
)
297 return fseeko (stream
, (off_t
) offset
, origin
);
305 __gnat_ftell64 (FILE *stream
)
307 return (__int64
)ftell (stream
);
311 __gnat_fseek64 (FILE *stream
, __int64 offset
, int origin
)
313 /* make sure that the offset is not bigger than the OS off_t, if so return
314 with error as this mean that we are trying to handle files larger than
315 2Gb on a patform not supporting it. */
316 if ((off_t
)offset
== offset
)
317 return fseek (stream
, (off_t
) offset
, origin
);