OSDN Git Service

2010-05-16 Manuel López-Ibáñez <manu@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / ada / cstreams.c
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-2009, 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 VMS
50 #include <unixlib.h>
51 #endif
52
53 #ifdef linux
54 /* Don't use macros on GNU/Linux since they cause incompatible changes between
55    glibc 2.0 and 2.1 */
56
57 #ifdef stderr
58 #  undef stderr
59 #endif
60 #ifdef stdin
61 #  undef stdin
62 #endif
63 #ifdef stdout
64 #  undef stdout
65 #endif
66
67 #endif
68
69 /* The _IONBF value in MINGW32 stdio.h is wrong.  */
70 #if defined (WINNT) || defined (_WINNT)
71 #if OLD_MINGW
72 #undef _IONBF
73 #define _IONBF 0004
74 #endif
75 #endif
76
77 int
78 __gnat_feof (FILE *stream)
79 {
80   return (feof (stream));
81 }
82
83 int
84 __gnat_ferror (FILE *stream)
85 {
86    return (ferror (stream));
87 }
88
89 int
90 __gnat_fileno (FILE *stream)
91 {
92    return (fileno (stream));
93 }
94
95 int
96 __gnat_is_regular_file_fd (int fd)
97 {
98   int ret;
99   GNAT_STRUCT_STAT statbuf;
100
101 #ifdef __EMX__
102   /* Programs using screen I/O may need to reset the FPU after
103      initialization of screen-handling related DLL's, so force
104      DLL initialization by doing a null-write and then reset the FPU */
105
106   DosWrite (0, &ret, 0, &ret);
107   __gnat_init_float();
108 #endif
109
110   ret = GNAT_FSTAT (fd, &statbuf);
111   return (!ret && S_ISREG (statbuf.st_mode));
112 }
113
114 /* on some systems, the constants for seek are not defined, if so, then
115    provide the conventional definitions */
116
117 #ifndef SEEK_SET
118 #define SEEK_SET 0  /* Set file pointer to offset                           */
119 #define SEEK_CUR 1  /* Set file pointer to its current value plus offset    */
120 #define SEEK_END 2  /* Set file pointer to the size of the file plus offset */
121 #endif
122
123 /* if L_tmpnam is not set, use a large number that should be safe */
124 #ifndef L_tmpnam
125 #define L_tmpnam 256
126 #endif
127
128 int    __gnat_constant_eof      = EOF;
129 int    __gnat_constant_iofbf    = _IOFBF;
130 int    __gnat_constant_iolbf    = _IOLBF;
131 int    __gnat_constant_ionbf    = _IONBF;
132 int    __gnat_constant_l_tmpnam = L_tmpnam;
133 int    __gnat_constant_seek_cur = SEEK_CUR;
134 int    __gnat_constant_seek_end = SEEK_END;
135 int    __gnat_constant_seek_set = SEEK_SET;
136
137 FILE *
138 __gnat_constant_stderr (void)
139 {
140   return stderr;
141 }
142
143 FILE *
144 __gnat_constant_stdin (void)
145 {
146   return stdin;
147 }
148
149 FILE *
150 __gnat_constant_stdout (void)
151 {
152   return stdout;
153 }
154
155 char *
156 __gnat_full_name (char *nam, char *buffer)
157 {
158 #ifdef RTSS
159   /* RTSS applications have no current-directory notion, so RTSS file I/O
160      requests must use fully qualified path names, such as:
161        c:\temp\MyFile.txt (for a file system object)
162        \\.\MyDevice0 (for a device object)
163    */
164   if (nam[1] == ':' || nam[0] == '\\')
165     strcpy (buffer, nam);
166   else
167     buffer[0] = '\0';
168
169 #elif defined(__EMX__) || defined (__MINGW32__)
170   /* If this is a device file return it as is; under Windows NT and
171      OS/2 a device file end with ":".  */
172   if (nam[strlen (nam) - 1] == ':')
173     strcpy (buffer, nam);
174   else
175     {
176       char *p;
177
178       _fullpath (buffer, nam, __gnat_max_path_len);
179
180       for (p = buffer; *p; p++)
181         if (*p == '/')
182           *p = '\\';
183     }
184
185 #elif defined (MSDOS)
186   _fixpath (nam, buffer);
187
188 #elif defined (sgi) || defined (__FreeBSD__)
189
190   /* Use realpath function which resolves links and references to . and ..
191      on those Unix systems that support it. Note that GNU/Linux provides it but
192      cannot handle more than 5 symbolic links in a full name, so we use the
193      getcwd approach instead. */
194   realpath (nam, buffer);
195
196 #elif defined (VMS)
197   strncpy (buffer, __gnat_to_canonical_file_spec (nam), __gnat_max_path_len);
198
199   if (buffer[0] == '/' || strchr (buffer, '!'))  /* '!' means decnet node */
200     strncpy (buffer, __gnat_to_host_file_spec (buffer), __gnat_max_path_len);
201   else
202     {
203       char *nambuffer = alloca (__gnat_max_path_len);
204
205       strncpy (nambuffer, buffer, __gnat_max_path_len);
206       strncpy
207         (buffer, getcwd (buffer, __gnat_max_path_len, 0), __gnat_max_path_len);
208       strncat (buffer, "/", __gnat_max_path_len);
209       strncat (buffer, nambuffer, __gnat_max_path_len);
210       strncpy (buffer, __gnat_to_host_file_spec (buffer), __gnat_max_path_len);
211     }
212
213 #elif defined (__vxworks)
214
215   /* On VxWorks systems, an absolute path can be represented (depending on
216      the host platform) as either /dir/file, or device:/dir/file, or
217      device:drive_letter:/dir/file. Use the __gnat_is_absolute_path
218      to verify it. */
219
220   int length;
221
222   if (__gnat_is_absolute_path (nam, strlen (nam)))
223     strcpy (buffer, nam);
224
225   else
226     {
227       length = __gnat_max_path_len;
228       __gnat_get_current_dir (buffer, &length);
229       strncat (buffer, nam, __gnat_max_path_len - length - 1);
230     }
231
232 #else
233   if (nam[0] != '/')
234     {
235       char *p = getcwd (buffer, __gnat_max_path_len);
236
237       if (p == 0)
238         {
239           buffer[0] = '\0';
240           return 0;
241         }
242
243
244       /* If the name returned is an absolute path, it is safe to append '/'
245          to the path and concatenate the name of the file. */
246       if (buffer[0] == '/')
247         strcat (buffer, "/");
248
249       strcat (buffer, nam);
250     }
251   else
252     strcpy (buffer, nam);
253 #endif
254
255   return buffer;
256 }