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-2012, 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 #ifdef __vxworks
35 #include "vxWorks.h"
36 #endif
37 
38 #ifdef IN_RTS
39 #include "tconfig.h"
40 #include "tsystem.h"
41 #include <sys/stat.h>
42 #else
43 #include "config.h"
44 #include "system.h"
45 #endif
46 
47 #include "adaint.h"
48 
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52 
53 #ifdef VMS
54 #include <unixlib.h>
55 #endif
56 
57 #ifdef linux
58 /* Don't use macros on GNU/Linux since they cause incompatible changes between
59    glibc 2.0 and 2.1 */
60 
61 #ifdef stderr
62 #  undef stderr
63 #endif
64 #ifdef stdin
65 #  undef stdin
66 #endif
67 #ifdef stdout
68 #  undef stdout
69 #endif
70 
71 #endif
72 
73 /* Don't use macros versions of this functions on VxWorks since they cause
74    imcompatible changes in some VxWorks versions */
75 #ifdef __vxworks
76 #undef getchar
77 #undef putchar
78 #undef feof
79 #undef ferror
80 #undef fileno
81 #endif
82 
83 /* The _IONBF value in MINGW32 stdio.h is wrong.  */
84 #if defined (WINNT) || defined (_WINNT)
85 #if OLD_MINGW
86 #undef _IONBF
87 #define _IONBF 0004
88 #endif
89 #endif
90 
91 int
__gnat_feof(FILE * stream)92 __gnat_feof (FILE *stream)
93 {
94   return (feof (stream));
95 }
96 
97 int
__gnat_ferror(FILE * stream)98 __gnat_ferror (FILE *stream)
99 {
100    return (ferror (stream));
101 }
102 
103 int
__gnat_fileno(FILE * stream)104 __gnat_fileno (FILE *stream)
105 {
106    return (fileno (stream));
107 }
108 
109 int
__gnat_is_regular_file_fd(int fd)110 __gnat_is_regular_file_fd (int fd)
111 {
112   int ret;
113   GNAT_STRUCT_STAT statbuf;
114 
115   ret = GNAT_FSTAT (fd, &statbuf);
116   return (!ret && S_ISREG (statbuf.st_mode));
117 }
118 
119 /* on some systems, the constants for seek are not defined, if so, then
120    provide the conventional definitions */
121 
122 #ifndef SEEK_SET
123 #define SEEK_SET 0  /* Set file pointer to offset                           */
124 #define SEEK_CUR 1  /* Set file pointer to its current value plus offset    */
125 #define SEEK_END 2  /* Set file pointer to the size of the file plus offset */
126 #endif
127 
128 /* if L_tmpnam is not set, use a large number that should be safe */
129 #ifndef L_tmpnam
130 #define L_tmpnam 256
131 #endif
132 
133 int    __gnat_constant_eof      = EOF;
134 int    __gnat_constant_iofbf    = _IOFBF;
135 int    __gnat_constant_iolbf    = _IOLBF;
136 int    __gnat_constant_ionbf    = _IONBF;
137 int    __gnat_constant_l_tmpnam = L_tmpnam;
138 int    __gnat_constant_seek_cur = SEEK_CUR;
139 int    __gnat_constant_seek_end = SEEK_END;
140 int    __gnat_constant_seek_set = SEEK_SET;
141 
142 FILE *
__gnat_constant_stderr(void)143 __gnat_constant_stderr (void)
144 {
145   return stderr;
146 }
147 
148 FILE *
__gnat_constant_stdin(void)149 __gnat_constant_stdin (void)
150 {
151   return stdin;
152 }
153 
154 FILE *
__gnat_constant_stdout(void)155 __gnat_constant_stdout (void)
156 {
157   return stdout;
158 }
159 
160 char *
__gnat_full_name(char * nam,char * buffer)161 __gnat_full_name (char *nam, char *buffer)
162 {
163 #ifdef RTSS
164   /* RTSS applications have no current-directory notion, so RTSS file I/O
165      requests must use fully qualified path names, such as:
166        c:\temp\MyFile.txt (for a file system object)
167        \\.\MyDevice0 (for a device object)
168    */
169   if (nam[1] == ':' || nam[0] == '\\')
170     strcpy (buffer, nam);
171   else
172     buffer[0] = '\0';
173 
174 #elif defined (__MINGW32__)
175   /* If this is a device file return it as is;
176      under Windows NT a device file ends with ":".  */
177   if (nam[strlen (nam) - 1] == ':')
178     strcpy (buffer, nam);
179   else
180     {
181       char *p;
182 
183       _fullpath (buffer, nam, __gnat_max_path_len);
184 
185       for (p = buffer; *p; p++)
186 	if (*p == '/')
187 	  *p = '\\';
188     }
189 
190 #elif defined (__FreeBSD__)
191 
192   /* Use realpath function which resolves links and references to . and ..
193      on those Unix systems that support it. Note that GNU/Linux provides it but
194      cannot handle more than 5 symbolic links in a full name, so we use the
195      getcwd approach instead. */
196   realpath (nam, buffer);
197 
198 #elif defined (VMS)
199   strncpy (buffer, __gnat_to_canonical_file_spec (nam), __gnat_max_path_len);
200 
201   if (buffer[0] == '/' || strchr (buffer, '!'))  /* '!' means decnet node */
202     strncpy (buffer, __gnat_to_host_file_spec (buffer), __gnat_max_path_len);
203   else
204     {
205       char *nambuffer = alloca (__gnat_max_path_len);
206 
207       strncpy (nambuffer, buffer, __gnat_max_path_len);
208       strncpy
209 	(buffer, getcwd (buffer, __gnat_max_path_len, 0), __gnat_max_path_len);
210       strncat (buffer, "/", __gnat_max_path_len);
211       strncat (buffer, nambuffer, __gnat_max_path_len);
212       strncpy (buffer, __gnat_to_host_file_spec (buffer), __gnat_max_path_len);
213     }
214 
215 #elif defined (__vxworks)
216 
217   /* On VxWorks systems, an absolute path can be represented (depending on
218      the host platform) as either /dir/file, or device:/dir/file, or
219      device:drive_letter:/dir/file. Use the __gnat_is_absolute_path
220      to verify it. */
221 
222   int length;
223 
224   if (__gnat_is_absolute_path (nam, strlen (nam)))
225     strcpy (buffer, nam);
226 
227   else
228     {
229       length = __gnat_max_path_len;
230       __gnat_get_current_dir (buffer, &length);
231       strncat (buffer, nam, __gnat_max_path_len - length - 1);
232     }
233 
234 #else
235   if (nam[0] != '/')
236     {
237       char *p = getcwd (buffer, __gnat_max_path_len);
238 
239       if (p == 0)
240 	{
241 	  buffer[0] = '\0';
242 	  return 0;
243 	}
244 
245 
246       /* If the name returned is an absolute path, it is safe to append '/'
247 	 to the path and concatenate the name of the file. */
248       if (buffer[0] == '/')
249 	strcat (buffer, "/");
250 
251       strcat (buffer, nam);
252     }
253   else
254     strcpy (buffer, nam);
255 #endif
256 
257   return buffer;
258 }
259 
260 #ifdef _WIN64
261   /* On Windows 64 we want to use the fseek/fteel supporting large files. This
262      issue is due to the fact that a long on Win64 is still a 32 bits value */
263 __int64
__gnat_ftell64(FILE * stream)264 __gnat_ftell64 (FILE *stream)
265 {
266   return _ftelli64 (stream);
267 }
268 
269 int
__gnat_fseek64(FILE * stream,__int64 offset,int origin)270 __gnat_fseek64 (FILE *stream, __int64 offset, int origin)
271 {
272   return _fseeki64 (stream, offset, origin);
273 }
274 
275 #else
276 long
__gnat_ftell64(FILE * stream)277 __gnat_ftell64 (FILE *stream)
278 {
279   return ftell (stream);
280 }
281 
282 int
__gnat_fseek64(FILE * stream,long offset,int origin)283 __gnat_fseek64 (FILE *stream, long offset, int origin)
284 {
285   return fseek (stream, offset, origin);
286 }
287 #endif
288 
289 #ifdef __cplusplus
290 }
291 #endif
292