OSDN Git Service

2011-08-05 Hristian Kirtchev <kirtchev@adacore.com>
[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-2011, 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 __cplusplus
35 extern "C" {
36 #endif
37
38 #ifdef __vxworks
39 #include "vxWorks.h"
40 #endif
41
42 #ifdef IN_RTS
43 #include "tconfig.h"
44 #include "tsystem.h"
45 #include <sys/stat.h>
46 #else
47 #include "config.h"
48 #include "system.h"
49 #endif
50
51 #include "adaint.h"
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 /* The _IONBF value in MINGW32 stdio.h is wrong.  */
74 #if defined (WINNT) || defined (_WINNT)
75 #if OLD_MINGW
76 #undef _IONBF
77 #define _IONBF 0004
78 #endif
79 #endif
80
81 int
82 __gnat_feof (FILE *stream)
83 {
84   return (feof (stream));
85 }
86
87 int
88 __gnat_ferror (FILE *stream)
89 {
90    return (ferror (stream));
91 }
92
93 int
94 __gnat_fileno (FILE *stream)
95 {
96    return (fileno (stream));
97 }
98
99 int
100 __gnat_is_regular_file_fd (int fd)
101 {
102   int ret;
103   GNAT_STRUCT_STAT statbuf;
104
105   ret = GNAT_FSTAT (fd, &statbuf);
106   return (!ret && S_ISREG (statbuf.st_mode));
107 }
108
109 /* on some systems, the constants for seek are not defined, if so, then
110    provide the conventional definitions */
111
112 #ifndef SEEK_SET
113 #define SEEK_SET 0  /* Set file pointer to offset                           */
114 #define SEEK_CUR 1  /* Set file pointer to its current value plus offset    */
115 #define SEEK_END 2  /* Set file pointer to the size of the file plus offset */
116 #endif
117
118 /* if L_tmpnam is not set, use a large number that should be safe */
119 #ifndef L_tmpnam
120 #define L_tmpnam 256
121 #endif
122
123 int    __gnat_constant_eof      = EOF;
124 int    __gnat_constant_iofbf    = _IOFBF;
125 int    __gnat_constant_iolbf    = _IOLBF;
126 int    __gnat_constant_ionbf    = _IONBF;
127 int    __gnat_constant_l_tmpnam = L_tmpnam;
128 int    __gnat_constant_seek_cur = SEEK_CUR;
129 int    __gnat_constant_seek_end = SEEK_END;
130 int    __gnat_constant_seek_set = SEEK_SET;
131
132 FILE *
133 __gnat_constant_stderr (void)
134 {
135   return stderr;
136 }
137
138 FILE *
139 __gnat_constant_stdin (void)
140 {
141   return stdin;
142 }
143
144 FILE *
145 __gnat_constant_stdout (void)
146 {
147   return stdout;
148 }
149
150 char *
151 __gnat_full_name (char *nam, char *buffer)
152 {
153 #ifdef RTSS
154   /* RTSS applications have no current-directory notion, so RTSS file I/O
155      requests must use fully qualified path names, such as:
156        c:\temp\MyFile.txt (for a file system object)
157        \\.\MyDevice0 (for a device object)
158    */
159   if (nam[1] == ':' || nam[0] == '\\')
160     strcpy (buffer, nam);
161   else
162     buffer[0] = '\0';
163
164 #elif defined (__MINGW32__)
165   /* If this is a device file return it as is;
166      under Windows NT a device file ends with ":".  */
167   if (nam[strlen (nam) - 1] == ':')
168     strcpy (buffer, nam);
169   else
170     {
171       char *p;
172
173       _fullpath (buffer, nam, __gnat_max_path_len);
174
175       for (p = buffer; *p; p++)
176         if (*p == '/')
177           *p = '\\';
178     }
179
180 #elif defined (sgi) || defined (__FreeBSD__)
181
182   /* Use realpath function which resolves links and references to . and ..
183      on those Unix systems that support it. Note that GNU/Linux provides it but
184      cannot handle more than 5 symbolic links in a full name, so we use the
185      getcwd approach instead. */
186   realpath (nam, buffer);
187
188 #elif defined (VMS)
189   strncpy (buffer, __gnat_to_canonical_file_spec (nam), __gnat_max_path_len);
190
191   if (buffer[0] == '/' || strchr (buffer, '!'))  /* '!' means decnet node */
192     strncpy (buffer, __gnat_to_host_file_spec (buffer), __gnat_max_path_len);
193   else
194     {
195       char *nambuffer = alloca (__gnat_max_path_len);
196
197       strncpy (nambuffer, buffer, __gnat_max_path_len);
198       strncpy
199         (buffer, getcwd (buffer, __gnat_max_path_len, 0), __gnat_max_path_len);
200       strncat (buffer, "/", __gnat_max_path_len);
201       strncat (buffer, nambuffer, __gnat_max_path_len);
202       strncpy (buffer, __gnat_to_host_file_spec (buffer), __gnat_max_path_len);
203     }
204
205 #elif defined (__vxworks)
206
207   /* On VxWorks systems, an absolute path can be represented (depending on
208      the host platform) as either /dir/file, or device:/dir/file, or
209      device:drive_letter:/dir/file. Use the __gnat_is_absolute_path
210      to verify it. */
211
212   int length;
213
214   if (__gnat_is_absolute_path (nam, strlen (nam)))
215     strcpy (buffer, nam);
216
217   else
218     {
219       length = __gnat_max_path_len;
220       __gnat_get_current_dir (buffer, &length);
221       strncat (buffer, nam, __gnat_max_path_len - length - 1);
222     }
223
224 #else
225   if (nam[0] != '/')
226     {
227       char *p = getcwd (buffer, __gnat_max_path_len);
228
229       if (p == 0)
230         {
231           buffer[0] = '\0';
232           return 0;
233         }
234
235
236       /* If the name returned is an absolute path, it is safe to append '/'
237          to the path and concatenate the name of the file. */
238       if (buffer[0] == '/')
239         strcat (buffer, "/");
240
241       strcat (buffer, nam);
242     }
243   else
244     strcpy (buffer, nam);
245 #endif
246
247   return buffer;
248 }
249
250 #ifdef __cplusplus
251 }
252 #endif