1 /****************************************************************************
3 * GNAT RUN-TIME COMPONENTS *
7 * Auxiliary C functions for Interfaces.C.Streams *
9 * Copyright (C) 1992-2017, 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>
46 /* needed to avoid conflicting declarations */
71 /* Don't use macros on GNU/Linux since they cause incompatible changes between
86 /* Don't use macros versions of this functions on VxWorks since they cause
87 imcompatible changes in some VxWorks versions */
96 /* The _IONBF value in MINGW32 stdio.h is wrong. */
97 #if defined (WINNT) || defined (_WINNT)
105 __gnat_feof (FILE *stream
)
107 return (feof (stream
));
111 __gnat_ferror (FILE *stream
)
113 return (ferror (stream
));
117 __gnat_fileno (FILE *stream
)
119 return (fileno (stream
));
122 /* on some systems, the constants for seek are not defined, if so, then
123 provide the conventional definitions */
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 */
131 /* if L_tmpnam is not set, use a large number that should be safe */
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
;
146 __gnat_constant_stderr (void)
152 __gnat_constant_stdin (void)
158 __gnat_constant_stdout (void)
164 __gnat_full_name (char *nam
, char *buffer
)
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)
172 if (nam
[1] == ':' || nam
[0] == '\\')
173 strcpy (buffer
, nam
);
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
);
186 _fullpath (buffer
, nam
, __gnat_max_path_len
);
188 for (p
= buffer
; *p
; p
++)
193 #elif defined (__FreeBSD__) || defined (__DragonFly__) || defined (__OpenBSD__)
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
);
201 #elif defined (__vxworks)
203 /* On VxWorks systems, an absolute path can be represented (depending on
204 the host platform) as either /dir/file, or device:/dir/file, or
205 device:drive_letter:/dir/file. Use the __gnat_is_absolute_path
210 if (__gnat_is_absolute_path (nam
, strlen (nam
)))
211 strcpy (buffer
, nam
);
215 length
= __gnat_max_path_len
;
216 __gnat_get_current_dir (buffer
, &length
);
217 strncat (buffer
, nam
, __gnat_max_path_len
- length
- 1);
223 char *p
= getcwd (buffer
, __gnat_max_path_len
);
232 /* If the name returned is an absolute path, it is safe to append '/'
233 to the path and concatenate the name of the file. */
234 if (buffer
[0] == '/')
235 strcat (buffer
, "/");
237 strcat (buffer
, nam
);
240 strcpy (buffer
, nam
);
247 /* On Windows we want to use the fseek/fteel supporting large files. This
248 issue is due to the fact that a long on Win64 is still a 32 bits value */
250 __gnat_ftell64 (FILE *stream
)
252 return _ftelli64 (stream
);
256 __gnat_fseek64 (FILE *stream
, __int64 offset
, int origin
)
258 return _fseeki64 (stream
, offset
, origin
);
261 #elif defined (__linux__) || defined (__sun__) || defined (__FreeBSD__) \
262 || defined (__APPLE__)
263 /* section for platforms having ftello/fseeko */
266 __gnat_ftell64 (FILE *stream
)
268 return (__int64
)ftello (stream
);
272 __gnat_fseek64 (FILE *stream
, __int64 offset
, int origin
)
274 /* make sure that the offset is not bigger than the OS off_t, if so return
275 with error as this mean that we are trying to handle files larger than
276 2Gb on a patform not supporting it. */
277 if ((off_t
)offset
== offset
)
278 return fseeko (stream
, (off_t
) offset
, origin
);
286 __gnat_ftell64 (FILE *stream
)
288 return (__int64
)ftell (stream
);
292 __gnat_fseek64 (FILE *stream
, __int64 offset
, int origin
)
294 /* make sure that the offset is not bigger than the OS off_t, if so return
295 with error as this mean that we are trying to handle files larger than
296 2Gb on a patform not supporting it. */
297 if ((off_t
)offset
== offset
)
298 return fseek (stream
, (off_t
) offset
, origin
);
304 /* Returns true if the path names a fifo (i.e. a named pipe). */
306 __gnat_is_fifo (const char* path
)
308 /* Posix defines S_ISFIFO as a macro. If the macro doesn't exist, we return
312 const int status
= stat(path
, &buf
);
314 return S_ISFIFO(buf
.st_mode
);
317 /* S_ISFIFO is not available, or stat got an error (probably