| 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-2025, 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 |
#ifndef _LARGEFILE_SOURCE |
| 35 |
#define _LARGEFILE_SOURCE |
| 36 |
#endif |
| 37 |
#define _FILE_OFFSET_BITS 64 |
| 38 |
/* the define above will make off_t a 64bit type on GNU/Linux */ |
| 39 |
|
| 40 |
/* Tell Cygwin's <stdio.h> to expose fileno_unlocked() */ |
| 41 |
#if defined(__CYGWIN__) && !defined(__CYGWIN32__) && !defined(_GNU_SOURCE) |
| 42 |
#define _GNU_SOURCE |
| 43 |
#endif |
| 44 |
|
| 45 |
#include <stdio.h> |
| 46 |
#include <sys/types.h> |
| 47 |
#include <sys/stat.h> |
| 48 |
#include <unistd.h> |
| 49 |
#include <stdlib.h> |
| 50 |
|
| 51 |
#ifdef _AIX |
| 52 |
/* needed to avoid conflicting declarations */ |
| 53 |
#include <unistd.h> |
| 54 |
#include <sys/mman.h> |
| 55 |
#endif |
| 56 |
|
| 57 |
#ifdef __vxworks |
| 58 |
#include "vxWorks.h" |
| 59 |
#endif |
| 60 |
|
| 61 |
#ifdef IN_RTS |
| 62 |
#include <string.h> |
| 63 |
#else |
| 64 |
#include "config.h" |
| 65 |
#include "system.h" |
| 66 |
#endif |
| 67 |
|
| 68 |
#include "adaint.h" |
| 69 |
|
| 70 |
#ifdef __cplusplus |
| 71 |
extern "C" { |
| 72 |
#endif |
| 73 |
|
| 74 |
#ifdef __linux__ |
| 75 |
/* Don't use macros on GNU/Linux since they cause incompatible changes between |
| 76 |
glibc 2.0 and 2.1 */ |
| 77 |
|
| 78 |
#ifdef stderr |
| 79 |
# undef stderr |
| 80 |
#endif |
| 81 |
#ifdef stdin |
| 82 |
# undef stdin |
| 83 |
#endif |
| 84 |
#ifdef stdout |
| 85 |
# undef stdout |
| 86 |
#endif |
| 87 |
|
| 88 |
#endif |
| 89 |
|
| 90 |
/* Don't use macros versions of this functions on VxWorks since they cause |
| 91 |
imcompatible changes in some VxWorks versions */ |
| 92 |
#ifdef __vxworks |
| 93 |
#undef getchar |
| 94 |
#undef putchar |
| 95 |
#undef feof |
| 96 |
#undef ferror |
| 97 |
#undef fileno |
| 98 |
#endif |
| 99 |
|
| 100 |
int |
| 101 |
__gnat_feof (FILE *stream) |
| 102 |
{ |
| 103 |
return (feof (stream)); |
| 104 |
} |
| 105 |
|
| 106 |
int |
| 107 |
__gnat_ferror (FILE *stream) |
| 108 |
{ |
| 109 |
return (ferror (stream)); |
| 110 |
} |
| 111 |
|
| 112 |
int |
| 113 |
__gnat_fileno (FILE *stream) |
| 114 |
{ |
| 115 |
return (fileno (stream)); |
| 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 (void) |
| 143 |
{ |
| 144 |
return stderr; |
| 145 |
} |
| 146 |
|
| 147 |
FILE * |
| 148 |
__gnat_constant_stdin (void) |
| 149 |
{ |
| 150 |
return stdin; |
| 151 |
} |
| 152 |
|
| 153 |
FILE * |
| 154 |
__gnat_constant_stdout (void) |
| 155 |
{ |
| 156 |
return stdout; |
| 157 |
} |
| 158 |
|
| 159 |
char * |
| 160 |
__gnat_full_name (char *nam, char *buffer) |
| 161 |
{ |
| 162 |
#ifdef RTSS |
| 163 |
/* RTSS applications have no current-directory notion, so RTSS file I/O |
| 164 |
requests must use fully qualified path names, such as: |
| 165 |
c:\temp\MyFile.txt (for a file system object) |
| 166 |
\\.\MyDevice0 (for a device object) |
| 167 |
*/ |
| 168 |
if (nam[1] == ':' || nam[0] == '\\') |
| 169 |
strcpy (buffer, nam); |
| 170 |
else |
| 171 |
buffer[0] = '\0'; |
| 172 |
|
| 173 |
#elif defined (__MINGW32__) |
| 174 |
/* If this is a device file return it as is; |
| 175 |
under Windows NT a device file ends with ":". */ |
| 176 |
if (nam[strlen (nam) - 1] == ':') |
| 177 |
strcpy (buffer, nam); |
| 178 |
else |
| 179 |
{ |
| 180 |
char *p; |
| 181 |
|
| 182 |
_fullpath (buffer, nam, __gnat_max_path_len); |
| 183 |
|
| 184 |
for (p = buffer; *p; p++) |
| 185 |
if (*p == '/') |
| 186 |
*p = '\\'; |
| 187 |
} |
| 188 |
|
| 189 |
#elif defined (__FreeBSD__) || defined (__DragonFly__) || defined (__OpenBSD__) |
| 190 |
|
| 191 |
/* Use realpath function which resolves links and references to . and .. |
| 192 |
on those Unix systems that support it. Note that GNU/Linux provides it but |
| 193 |
cannot handle more than 5 symbolic links in a full name, so we use the |
| 194 |
getcwd approach instead. */ |
| 195 |
realpath (nam, buffer); |
| 196 |
|
| 197 |
#elif defined (__vxworks) |
| 198 |
|
| 199 |
/* On VxWorks systems, an absolute path can be represented (depending on |
| 200 |
the host platform) as either /dir/file, or device:/dir/file, or |
| 201 |
device:drive_letter:/dir/file. Use the __gnat_is_absolute_path |
| 202 |
to verify it. */ |
| 203 |
|
| 204 |
int length; |
| 205 |
|
| 206 |
if (__gnat_is_absolute_path (nam, strlen (nam))) |
| 207 |
strcpy (buffer, nam); |
| 208 |
|
| 209 |
else |
| 210 |
{ |
| 211 |
length = __gnat_max_path_len; |
| 212 |
__gnat_get_current_dir (buffer, &length); |
| 213 |
strncat (buffer, nam, __gnat_max_path_len - length - 1); |
| 214 |
} |
| 215 |
|
| 216 |
#else |
| 217 |
if (nam[0] != '/') |
| 218 |
{ |
| 219 |
char *p = getcwd (buffer, __gnat_max_path_len); |
| 220 |
|
| 221 |
if (p == 0) |
| 222 |
{ |
| 223 |
buffer[0] = '\0'; |
| 224 |
return 0; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
/* If the name returned is an absolute path, it is safe to append '/' |
| 229 |
to the path and concatenate the name of the file. */ |
| 230 |
if (buffer[0] == '/') |
| 231 |
strcat (buffer, "/"); |
| 232 |
|
| 233 |
strcat (buffer, nam); |
| 234 |
} |
| 235 |
else |
| 236 |
strcpy (buffer, nam); |
| 237 |
#endif |
| 238 |
|
| 239 |
return buffer; |
| 240 |
} |
| 241 |
|
| 242 |
#ifdef _WIN32 |
| 243 |
/* On Windows we want to use the fseek/fteel supporting large files. This |
| 244 |
issue is due to the fact that a long on Win64 is still a 32 bits value */ |
| 245 |
__int64 |
| 246 |
__gnat_ftell64 (FILE *stream) |
| 247 |
{ |
| 248 |
return _ftelli64 (stream); |
| 249 |
} |
| 250 |
|
| 251 |
int |
| 252 |
__gnat_fseek64 (FILE *stream, __int64 offset, int origin) |
| 253 |
{ |
| 254 |
return _fseeki64 (stream, offset, origin); |
| 255 |
} |
| 256 |
|
| 257 |
#elif defined (__linux__) || defined (__sun__) || defined (__FreeBSD__) \ |
| 258 |
|| defined (__APPLE__) |
| 259 |
/* section for platforms having ftello/fseeko */ |
| 260 |
|
| 261 |
__int64 |
| 262 |
__gnat_ftell64 (FILE *stream) |
| 263 |
{ |
| 264 |
return (__int64)ftello (stream); |
| 265 |
} |
| 266 |
|
| 267 |
int |
| 268 |
__gnat_fseek64 (FILE *stream, __int64 offset, int origin) |
| 269 |
{ |
| 270 |
/* make sure that the offset is not bigger than the OS off_t, if so return |
| 271 |
with error as this mean that we are trying to handle files larger than |
| 272 |
2Gb on a patform not supporting it. */ |
| 273 |
if ((off_t)offset == offset) |
| 274 |
return fseeko (stream, (off_t) offset, origin); |
| 275 |
else |
| 276 |
return -1; |
| 277 |
} |
| 278 |
|
| 279 |
#else |
| 280 |
|
| 281 |
__int64 |
| 282 |
__gnat_ftell64 (FILE *stream) |
| 283 |
{ |
| 284 |
return (__int64)ftell (stream); |
| 285 |
} |
| 286 |
|
| 287 |
int |
| 288 |
__gnat_fseek64 (FILE *stream, __int64 offset, int origin) |
| 289 |
{ |
| 290 |
/* make sure that the offset is not bigger than the OS off_t, if so return |
| 291 |
with error as this mean that we are trying to handle files larger than |
| 292 |
2Gb on a patform not supporting it. */ |
| 293 |
if ((off_t)offset == offset) |
| 294 |
return fseek (stream, (off_t) offset, origin); |
| 295 |
else |
| 296 |
return -1; |
| 297 |
} |
| 298 |
#endif |
| 299 |
|
| 300 |
/* Returns true if the path names a fifo (i.e. a named pipe). */ |
| 301 |
int |
| 302 |
__gnat_is_fifo (const char* path) |
| 303 |
{ |
| 304 |
/* Posix defines S_ISFIFO as a macro. If the macro doesn't exist, we return |
| 305 |
false. */ |
| 306 |
#ifdef S_ISFIFO |
| 307 |
struct stat buf; |
| 308 |
const int status = stat(path, &buf); |
| 309 |
if (status == 0) |
| 310 |
return S_ISFIFO(buf.st_mode); |
| 311 |
#endif |
| 312 |
|
| 313 |
/* S_ISFIFO is not available, or stat got an error (probably |
| 314 |
file not found). */ |
| 315 |
return 0; |
| 316 |
} |
| 317 |
|
| 318 |
#ifdef __cplusplus |
| 319 |
} |
| 320 |
#endif |