OSDN Git Service

2007-09-26 Thomas Quinot <quinot@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-2007, 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 2,  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.  See the GNU General Public License *
17  * for  more details.  You should have  received  a copy of the GNU General *
18  * Public License  distributed with GNAT;  see file COPYING.  If not, write *
19  * to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, *
20  * Boston, MA 02110-1301, USA.                                              *
21  *                                                                          *
22  * As a  special  exception,  if you  link  this file  with other  files to *
23  * produce an executable,  this file does not by itself cause the resulting *
24  * executable to be covered by the GNU General Public License. This except- *
25  * ion does not  however invalidate  any other reasons  why the  executable *
26  * file might be covered by the  GNU Public License.                        *
27  *                                                                          *
28  * GNAT was originally developed  by the GNAT team at  New York University. *
29  * Extensive contributions were provided by Ada Core Technologies Inc.      *
30  *                                                                          *
31  ****************************************************************************/
32
33 /* Routines required for implementing routines in Interfaces.C.Streams */
34
35 #ifdef __vxworks
36 #include "vxWorks.h"
37 #endif
38
39 #ifdef IN_RTS
40 #include "tconfig.h"
41 #include "tsystem.h"
42 #include <sys/stat.h>
43 #else
44 #include "config.h"
45 #include "system.h"
46 #endif
47
48 #include "adaint.h"
49
50 #ifdef VMS
51 #include <unixlib.h>
52 #endif
53
54 #ifdef linux
55 /* Don't use macros on GNU/Linux since they cause incompatible changes between
56    glibc 2.0 and 2.1 */
57
58 #ifdef stderr
59 #  undef stderr
60 #endif
61 #ifdef stdin
62 #  undef stdin
63 #endif
64 #ifdef stdout
65 #  undef stdout
66 #endif
67
68 #endif
69
70 /* The _IONBF value in MINGW32 stdio.h is wrong.  */
71 #if defined (WINNT) || defined (_WINNT)
72 #if OLD_MINGW
73 #undef _IONBF
74 #define _IONBF 0004
75 #endif
76 #endif
77
78 int
79 __gnat_feof (FILE *stream)
80 {
81   return (feof (stream));
82 }
83
84 int
85 __gnat_ferror (FILE *stream)
86 {
87    return (ferror (stream));
88 }
89
90 int
91 __gnat_fileno (FILE *stream)
92 {
93    return (fileno (stream));
94 }
95
96 int
97 __gnat_is_regular_file_fd (int fd)
98 {
99   int ret;
100   struct stat statbuf;
101
102 #ifdef __EMX__
103   /* Programs using screen I/O may need to reset the FPU after
104      initialization of screen-handling related DLL's, so force
105      DLL initialization by doing a null-write and then reset the FPU */
106
107   DosWrite (0, &ret, 0, &ret);
108   __gnat_init_float();
109 #endif
110
111   ret = fstat (fd, &statbuf);
112   return (!ret && S_ISREG (statbuf.st_mode));
113 }
114
115 /* on some systems, the constants for seek are not defined, if so, then
116    provide the conventional definitions */
117
118 #ifndef SEEK_SET
119 #define SEEK_SET 0  /* Set file pointer to offset                           */
120 #define SEEK_CUR 1  /* Set file pointer to its current value plus offset    */
121 #define SEEK_END 2  /* Set file pointer to the size of the file plus offset */
122 #endif
123
124 /* if L_tmpnam is not set, use a large number that should be safe */
125 #ifndef L_tmpnam
126 #define L_tmpnam 256
127 #endif
128
129 int    __gnat_constant_eof      = EOF;
130 int    __gnat_constant_iofbf    = _IOFBF;
131 int    __gnat_constant_iolbf    = _IOLBF;
132 int    __gnat_constant_ionbf    = _IONBF;
133 int    __gnat_constant_l_tmpnam = L_tmpnam;
134 int    __gnat_constant_seek_cur = SEEK_CUR;
135 int    __gnat_constant_seek_end = SEEK_END;
136 int    __gnat_constant_seek_set = SEEK_SET;
137
138 FILE *
139 __gnat_constant_stderr (void)
140 {
141   return stderr;
142 }
143
144 FILE *
145 __gnat_constant_stdin (void)
146 {
147   return stdin;
148 }
149
150 FILE *
151 __gnat_constant_stdout (void)
152 {
153   return stdout;
154 }
155
156 char *
157 __gnat_full_name (char *nam, char *buffer)
158 {
159 #if defined(__EMX__) || defined (__MINGW32__)
160   /* If this is a device file return it as is; under Windows NT and
161      OS/2 a device file end with ":".  */
162   if (nam[strlen (nam) - 1] == ':')
163     strcpy (buffer, nam);
164   else
165     {
166       char *p;
167
168       _fullpath (buffer, nam, __gnat_max_path_len);
169
170       for (p = buffer; *p; p++)
171         if (*p == '/')
172           *p = '\\';
173     }
174
175 #elif defined (MSDOS)
176   _fixpath (nam, buffer);
177
178 #elif defined (sgi) || defined (__FreeBSD__)
179
180   /* Use realpath function which resolves links and references to . and ..
181      on those Unix systems that support it. Note that GNU/Linux provides it but
182      cannot handle more than 5 symbolic links in a full name, so we use the
183      getcwd approach instead. */
184   realpath (nam, buffer);
185
186 #elif defined (VMS)
187   strncpy (buffer, __gnat_to_canonical_file_spec (nam), __gnat_max_path_len);
188
189   if (buffer[0] == '/' || strchr (buffer, '!'))  /* '!' means decnet node */
190     strncpy (buffer, __gnat_to_host_file_spec (buffer), __gnat_max_path_len);
191   else
192     {
193       char *nambuffer = alloca (__gnat_max_path_len);
194
195       strncpy (nambuffer, buffer, __gnat_max_path_len);
196       strncpy
197         (buffer, getcwd (buffer, __gnat_max_path_len, 0), __gnat_max_path_len);
198       strncat (buffer, "/", __gnat_max_path_len);
199       strncat (buffer, nambuffer, __gnat_max_path_len);
200       strncpy (buffer, __gnat_to_host_file_spec (buffer), __gnat_max_path_len);
201     }
202
203 #elif defined (__vxworks)
204
205   /* On VxWorks systems, an absolute path can be represented (depending on
206      the host platform) as either /dir/file, or device:/dir/file, or
207      device:drive_letter:/dir/file. Use the __gnat_is_absolute_path
208      to verify it. */
209
210   int length;
211
212   if (__gnat_is_absolute_path (nam, strlen (nam)))
213     strcpy (buffer, nam);
214
215   else
216     {
217       length = __gnat_max_path_len;
218       __gnat_get_current_dir (buffer, &length);
219       strncat (buffer, nam, __gnat_max_path_len - length - 1);
220     }
221
222 #else
223   if (nam[0] != '/')
224     {
225       char *p = getcwd (buffer, __gnat_max_path_len);
226
227       if (p == 0)
228         {
229           buffer[0] = '\0';
230           return 0;
231         }
232
233
234       /* If the name returned is an absolute path, it is safe to append '/'
235          to the path and concatenate the name of the file. */
236       if (buffer[0] == '/')
237         strcat (buffer, "/");
238
239       strcat (buffer, nam);
240     }
241   else
242     strcpy (buffer, nam);
243 #endif
244
245   return buffer;
246 }