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