OSDN Git Service

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