1 /****************************************************************************
3 * GNAT RUN-TIME COMPONENTS *
7 * Auxiliary C functions for Interfaces.C.Streams *
9 * Copyright (C) 1992-2010, Free Software Foundation, Inc. *
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. *
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. *
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/>. *
27 * GNAT was originally developed by the GNAT team at New York University. *
28 * Extensive contributions were provided by Ada Core Technologies Inc. *
30 ****************************************************************************/
32 /* Routines required for implementing routines in Interfaces.C.Streams */
54 /* Don't use macros on GNU/Linux since they cause incompatible changes between
69 /* The _IONBF value in MINGW32 stdio.h is wrong. */
70 #if defined (WINNT) || defined (_WINNT)
78 __gnat_feof (FILE *stream
)
80 return (feof (stream
));
84 __gnat_ferror (FILE *stream
)
86 return (ferror (stream
));
90 __gnat_fileno (FILE *stream
)
92 return (fileno (stream
));
96 __gnat_is_regular_file_fd (int fd
)
99 GNAT_STRUCT_STAT statbuf
;
101 ret
= GNAT_FSTAT (fd
, &statbuf
);
102 return (!ret
&& S_ISREG (statbuf
.st_mode
));
105 /* on some systems, the constants for seek are not defined, if so, then
106 provide the conventional definitions */
109 #define SEEK_SET 0 /* Set file pointer to offset */
110 #define SEEK_CUR 1 /* Set file pointer to its current value plus offset */
111 #define SEEK_END 2 /* Set file pointer to the size of the file plus offset */
114 /* if L_tmpnam is not set, use a large number that should be safe */
119 int __gnat_constant_eof
= EOF
;
120 int __gnat_constant_iofbf
= _IOFBF
;
121 int __gnat_constant_iolbf
= _IOLBF
;
122 int __gnat_constant_ionbf
= _IONBF
;
123 int __gnat_constant_l_tmpnam
= L_tmpnam
;
124 int __gnat_constant_seek_cur
= SEEK_CUR
;
125 int __gnat_constant_seek_end
= SEEK_END
;
126 int __gnat_constant_seek_set
= SEEK_SET
;
129 __gnat_constant_stderr (void)
135 __gnat_constant_stdin (void)
141 __gnat_constant_stdout (void)
147 __gnat_full_name (char *nam
, char *buffer
)
150 /* RTSS applications have no current-directory notion, so RTSS file I/O
151 requests must use fully qualified path names, such as:
152 c:\temp\MyFile.txt (for a file system object)
153 \\.\MyDevice0 (for a device object)
155 if (nam
[1] == ':' || nam
[0] == '\\')
156 strcpy (buffer
, nam
);
160 #elif defined (__MINGW32__)
161 /* If this is a device file return it as is;
162 under Windows NT a device file ends with ":". */
163 if (nam
[strlen (nam
) - 1] == ':')
164 strcpy (buffer
, nam
);
169 _fullpath (buffer
, nam
, __gnat_max_path_len
);
171 for (p
= buffer
; *p
; p
++)
176 #elif defined (sgi) || defined (__FreeBSD__)
178 /* Use realpath function which resolves links and references to . and ..
179 on those Unix systems that support it. Note that GNU/Linux provides it but
180 cannot handle more than 5 symbolic links in a full name, so we use the
181 getcwd approach instead. */
182 realpath (nam
, buffer
);
185 strncpy (buffer
, __gnat_to_canonical_file_spec (nam
), __gnat_max_path_len
);
187 if (buffer
[0] == '/' || strchr (buffer
, '!')) /* '!' means decnet node */
188 strncpy (buffer
, __gnat_to_host_file_spec (buffer
), __gnat_max_path_len
);
191 char *nambuffer
= alloca (__gnat_max_path_len
);
193 strncpy (nambuffer
, buffer
, __gnat_max_path_len
);
195 (buffer
, getcwd (buffer
, __gnat_max_path_len
, 0), __gnat_max_path_len
);
196 strncat (buffer
, "/", __gnat_max_path_len
);
197 strncat (buffer
, nambuffer
, __gnat_max_path_len
);
198 strncpy (buffer
, __gnat_to_host_file_spec (buffer
), __gnat_max_path_len
);
201 #elif defined (__vxworks)
203 /* On VxWorks systems, an absolute path can be represented (depending on
204 the host platform) as either /dir/file, or device:/dir/file, or
205 device:drive_letter:/dir/file. Use the __gnat_is_absolute_path
210 if (__gnat_is_absolute_path (nam
, strlen (nam
)))
211 strcpy (buffer
, nam
);
215 length
= __gnat_max_path_len
;
216 __gnat_get_current_dir (buffer
, &length
);
217 strncat (buffer
, nam
, __gnat_max_path_len
- length
- 1);
223 char *p
= getcwd (buffer
, __gnat_max_path_len
);
232 /* If the name returned is an absolute path, it is safe to append '/'
233 to the path and concatenate the name of the file. */
234 if (buffer
[0] == '/')
235 strcat (buffer
, "/");
237 strcat (buffer
, nam
);
240 strcpy (buffer
, nam
);