OSDN Git Service

Nathanael Nerode <neroden@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  *                                                                          *
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  * Extensive contributions were provided by Ada Core Technologies Inc.      *
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 VMS
52 #include <unixlib.h>
53 #endif
54
55 #ifdef linux
56 /* Don't use macros on GNU/Linux since they cause incompatible changes between
57    glibc 2.0 and 2.1 */
58
59 #ifdef stderr
60 #  undef stderr
61 #endif
62 #ifdef stdin
63 #  undef stdin
64 #endif
65 #ifdef stdout
66 #  undef stdout
67 #endif
68 #endif
69
70 /* The _IONBF value in CYGNUS or MINGW32 stdio.h is wrong.  */
71 #if defined (WINNT) || defined (_WINNT)
72 #undef _IONBF
73 #define _IONBF 0004
74 #endif
75
76
77 int
78 __gnat_feof (stream)
79      FILE *stream;
80 {
81   return (feof (stream));
82 }
83
84 int
85 __gnat_ferror (stream)
86      FILE *stream;
87 {
88    return (ferror (stream));
89 }
90
91 int
92 __gnat_fileno (stream)
93      FILE *stream;
94 {
95    return (fileno (stream));
96 }
97
98 int
99 __gnat_is_regular_file_fd (fd)
100      int fd;
101 {
102   int ret;
103   struct stat statbuf;
104
105 #ifdef __EMX__
106   /* Programs using screen I/O may need to reset the FPU after
107      initialization of screen-handling related DLL's, so force
108      DLL initialization by doing a null-write and then reset the FPU */
109
110   DosWrite (0, &ret, 0, &ret);
111   __gnat_init_float();
112 #endif
113
114   ret = fstat (fd, &statbuf);
115   return (!ret && S_ISREG (statbuf.st_mode));
116 }
117
118 /* on some systems, the constants for seek are not defined, if so, then
119    provide the conventional definitions */
120
121 #ifndef SEEK_SET
122 #define SEEK_SET 0  /* Set file pointer to offset                           */
123 #define SEEK_CUR 1  /* Set file pointer to its current value plus offset    */
124 #define SEEK_END 2  /* Set file pointer to the size of the file plus offset */
125 #endif
126
127 /* if L_tmpnam is not set, use a large number that should be safe */
128 #ifndef L_tmpnam
129 #define L_tmpnam 256
130 #endif
131
132 int    __gnat_constant_eof      = EOF;
133 int    __gnat_constant_iofbf    = _IOFBF;
134 int    __gnat_constant_iolbf    = _IOLBF;
135 int    __gnat_constant_ionbf    = _IONBF;
136 int    __gnat_constant_l_tmpnam = L_tmpnam;
137 int    __gnat_constant_seek_cur = SEEK_CUR;
138 int    __gnat_constant_seek_end = SEEK_END;
139 int    __gnat_constant_seek_set = SEEK_SET;
140
141 FILE *
142 __gnat_constant_stderr ()
143 {
144   return stderr;
145 }
146
147 FILE *
148 __gnat_constant_stdin ()
149 {
150   return stdin;
151 }
152
153 FILE *
154 __gnat_constant_stdout ()
155 {
156   return stdout;
157 }
158
159 char *
160 __gnat_full_name (nam, buffer)
161      char *nam;
162      char *buffer;
163 {
164   char *p;
165
166 #if defined(__EMX__) || defined (__MINGW32__)
167   /* If this is a device file return it as is; under Windows NT and
168      OS/2 a device file end with ":".  */
169   if (nam[strlen (nam) - 1] == ':')
170     strcpy (buffer, nam);
171   else
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 (MSDOS)
181   _fixpath (nam, buffer);
182
183 #elif defined (sgi)
184
185   /* Use realpath function which resolves links and references to .. and ..
186      on those Unix systems that support it. Note that GNU/Linux provides it but
187      cannot handle more than 5 symbolic links in a full name, so we use the
188      getcwd approach instead. */
189   realpath (nam, buffer);
190
191 #elif defined (VMS)
192   strcpy (buffer, __gnat_to_canonical_file_spec (nam));
193
194   if (buffer[0] == '/')
195     strcpy (buffer, __gnat_to_host_file_spec (buffer));
196   else
197     {
198       char *nambuffer = alloca (__gnat_max_path_len);
199
200       strcpy (nambuffer, buffer);
201       strcpy (buffer, getcwd (buffer, __gnat_max_path_len, 0));
202       strcat (buffer, "/");
203       strcat (buffer, nambuffer);
204       strcpy (buffer, __gnat_to_host_file_spec (buffer));
205     }
206
207   return buffer;
208
209 #else
210   if (nam[0] != '/')
211     {
212       p = getcwd (buffer, __gnat_max_path_len);
213       if (p == 0)
214         {
215           buffer[0] = '\0';
216           return 0;
217         }
218
219       /* If the name returned is an absolute path, it is safe to append '/'
220          to the path and concatenate the name of the file. */
221       if (buffer[0] == '/')
222         strcat (buffer, "/");
223
224       strcat (buffer, nam);
225     }
226   else
227     strcpy (buffer, nam);
228
229   return buffer;
230 #endif
231 }