| 1 |
/* Glibc.c provides access to some libc functions. |
| 2 |
|
| 3 |
Copyright (C) 2016-2025 Free Software Foundation, Inc. |
| 4 |
Contributed by Gaius Mulley <[email protected]>. |
| 5 |
|
| 6 |
This file is part of GNU Modula-2. |
| 7 |
|
| 8 |
GNU Modula-2 is free software; you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation; either version 3, or (at your option) |
| 11 |
any later version. |
| 12 |
|
| 13 |
GNU Modula-2 is distributed in the hope that it will be useful, but |
| 14 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
along with GNU Modula-2; see the file COPYING3. If not see |
| 20 |
<http://www.gnu.org/licenses/>. */ |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
#include "system.h" |
| 24 |
|
| 25 |
#if defined(__cplusplus) |
| 26 |
#define EXTERN extern "C" |
| 27 |
#else |
| 28 |
#define EXTERN |
| 29 |
#endif |
| 30 |
|
| 31 |
#undef BUILD_MC_LIBC_TRACE |
| 32 |
|
| 33 |
#if defined(BUILD_MC_LIBC_TRACE) |
| 34 |
static bool initialzed_trace = false; |
| 35 |
static bool trace_on = false; |
| 36 |
|
| 37 |
static |
| 38 |
void |
| 39 |
check_init (void) |
| 40 |
{ |
| 41 |
if (! initialzed_trace) |
| 42 |
{ |
| 43 |
initialzed_trace = true; |
| 44 |
trace_on = ((getenv ("MC_LIBC_TRACE") != NULL)); |
| 45 |
} |
| 46 |
} |
| 47 |
#endif |
| 48 |
|
| 49 |
static |
| 50 |
void |
| 51 |
tracedb (const char *format, ...) |
| 52 |
{ |
| 53 |
#if defined(BUILD_MC_LIBC_TRACE) |
| 54 |
check_init (); |
| 55 |
if (trace_on) |
| 56 |
{ |
| 57 |
va_list arg; |
| 58 |
va_start (arg, format); |
| 59 |
{ |
| 60 |
vfprintf (stdout, format, arg); |
| 61 |
fflush (stdout); |
| 62 |
} |
| 63 |
va_end (arg); |
| 64 |
} |
| 65 |
#endif |
| 66 |
} |
| 67 |
|
| 68 |
static |
| 69 |
void |
| 70 |
tracedb_open (const void *p, int flags, int mode) |
| 71 |
{ |
| 72 |
#if defined(BUILD_MC_LIBC_TRACE) |
| 73 |
bool item_written = false; |
| 74 |
tracedb ("libc_open (%s, flags = 0x%x = ", p, flags); |
| 75 |
|
| 76 |
int bits = (flags & O_ACCMODE); |
| 77 |
tracedb ("bits = 0x%x", bits); |
| 78 |
if (bits == O_RDONLY) |
| 79 |
{ |
| 80 |
tracedb ("O_RDONLY"); |
| 81 |
item_written = true; |
| 82 |
} |
| 83 |
if ((flags & O_WRONLY) != 0) |
| 84 |
{ |
| 85 |
if (item_written) |
| 86 |
tracedb (" | "); |
| 87 |
tracedb ("O_WRONLY"); |
| 88 |
item_written = true; |
| 89 |
} |
| 90 |
if ((flags & O_RDWR) != 0) |
| 91 |
{ |
| 92 |
if (item_written) |
| 93 |
tracedb (" | "); |
| 94 |
tracedb ("O_RDWR"); |
| 95 |
item_written = true; |
| 96 |
} |
| 97 |
tracedb (", 0x%x)\n", mode); |
| 98 |
#endif |
| 99 |
} |
| 100 |
|
| 101 |
static |
| 102 |
void |
| 103 |
tracedb_result (int result) |
| 104 |
{ |
| 105 |
#if defined(BUILD_MC_LIBC_TRACE) |
| 106 |
tracedb (" result = %d", result); |
| 107 |
if (result == -1) |
| 108 |
tracedb (", errno = %s", strerror (errno)); |
| 109 |
tracedb ("\n"); |
| 110 |
#endif |
| 111 |
} |
| 112 |
|
| 113 |
static |
| 114 |
void |
| 115 |
tracedb_zresult (size_t result) |
| 116 |
{ |
| 117 |
#if defined(BUILD_MC_LIBC_TRACE) |
| 118 |
tracedb (" result = %zd", result); |
| 119 |
if (result == -1) |
| 120 |
tracedb (", errno = %s", strerror (errno)); |
| 121 |
tracedb ("\n"); |
| 122 |
#endif |
| 123 |
} |
| 124 |
|
| 125 |
EXTERN |
| 126 |
size_t |
| 127 |
libc_read (int fd, void *a, size_t nbytes) |
| 128 |
{ |
| 129 |
tracedb ("libc_read (%d, %p, %zd)\n", fd, a, nbytes); |
| 130 |
size_t result = read (fd, a, nbytes); |
| 131 |
tracedb_zresult (result); |
| 132 |
return result; |
| 133 |
} |
| 134 |
|
| 135 |
EXTERN |
| 136 |
size_t |
| 137 |
libc_write (int fd, void *a, size_t nbytes) |
| 138 |
{ |
| 139 |
tracedb ("libc_write (%d, %p, %zd)\n", fd, a, nbytes); |
| 140 |
size_t result = write (fd, a, nbytes); |
| 141 |
tracedb_zresult (result); |
| 142 |
return result; |
| 143 |
} |
| 144 |
|
| 145 |
EXTERN |
| 146 |
int |
| 147 |
libc_close (int fd) |
| 148 |
{ |
| 149 |
tracedb ("libc_close (%d)\n", fd); |
| 150 |
int result = close (fd); |
| 151 |
tracedb_result (result); |
| 152 |
return result; |
| 153 |
} |
| 154 |
|
| 155 |
EXTERN |
| 156 |
int |
| 157 |
libc_exit (int code) |
| 158 |
{ |
| 159 |
exit (code); |
| 160 |
} |
| 161 |
|
| 162 |
EXTERN |
| 163 |
void |
| 164 |
libc_perror (const char *s, unsigned int length) |
| 165 |
{ |
| 166 |
perror (s); |
| 167 |
} |
| 168 |
|
| 169 |
EXTERN |
| 170 |
int |
| 171 |
libc_abort () |
| 172 |
{ |
| 173 |
abort (); |
| 174 |
} |
| 175 |
|
| 176 |
EXTERN |
| 177 |
size_t |
| 178 |
libc_strlen (char *s) |
| 179 |
{ |
| 180 |
return strlen (s); |
| 181 |
} |
| 182 |
|
| 183 |
EXTERN |
| 184 |
time_t |
| 185 |
libc_time (time_t *buf) |
| 186 |
{ |
| 187 |
return time (buf); |
| 188 |
} |
| 189 |
|
| 190 |
EXTERN |
| 191 |
void * |
| 192 |
libc_localtime (time_t *epochtime) |
| 193 |
{ |
| 194 |
return localtime (epochtime); |
| 195 |
} |
| 196 |
|
| 197 |
EXTERN |
| 198 |
int |
| 199 |
libc_printf (const char *_format, unsigned int _format_high, ...) |
| 200 |
{ |
| 201 |
va_list arg; |
| 202 |
int done; |
| 203 |
char format[_format_high + 1]; |
| 204 |
unsigned int i = 0; |
| 205 |
unsigned int j = 0; |
| 206 |
const char *c; |
| 207 |
|
| 208 |
do |
| 209 |
{ |
| 210 |
c = index (&const_cast <char *> (_format)[i], '\\'); |
| 211 |
if (c == NULL) |
| 212 |
strcpy (&format[j], &_format[i]); |
| 213 |
else |
| 214 |
{ |
| 215 |
memcpy (&format[j], &_format[i], (c - _format) - i); |
| 216 |
i = c - _format; |
| 217 |
j += c - _format; |
| 218 |
if (_format[i + 1] == 'n') |
| 219 |
format[j] = '\n'; |
| 220 |
else |
| 221 |
format[j] = _format[i + 1]; |
| 222 |
j++; |
| 223 |
i += 2; |
| 224 |
} |
| 225 |
} |
| 226 |
while (c != NULL); |
| 227 |
|
| 228 |
va_start (arg, _format_high); |
| 229 |
done = vfprintf (stdout, format, arg); |
| 230 |
va_end (arg); |
| 231 |
return done; |
| 232 |
} |
| 233 |
|
| 234 |
EXTERN |
| 235 |
int |
| 236 |
libc_snprintf (void *dest, size_t length, const char *_format, unsigned int _format_high, ...) |
| 237 |
{ |
| 238 |
va_list arg; |
| 239 |
int done; |
| 240 |
char format[_format_high + 1]; |
| 241 |
unsigned int i = 0; |
| 242 |
unsigned int j = 0; |
| 243 |
const char *c; |
| 244 |
|
| 245 |
do |
| 246 |
{ |
| 247 |
c = index (&const_cast <char *> (_format)[i], '\\'); |
| 248 |
if (c == NULL) |
| 249 |
strcpy (&format[j], &_format[i]); |
| 250 |
else |
| 251 |
{ |
| 252 |
memcpy (&format[j], &_format[i], (c - _format) - i); |
| 253 |
i = c - _format; |
| 254 |
j += c - _format; |
| 255 |
if (_format[i + 1] == 'n') |
| 256 |
format[j] = '\n'; |
| 257 |
else |
| 258 |
format[j] = _format[i + 1]; |
| 259 |
j++; |
| 260 |
i += 2; |
| 261 |
} |
| 262 |
} |
| 263 |
while (c != NULL); |
| 264 |
|
| 265 |
va_start (arg, _format_high); |
| 266 |
done = vsnprintf (reinterpret_cast<char *> (dest), length, format, arg); |
| 267 |
va_end (arg); |
| 268 |
return done; |
| 269 |
} |
| 270 |
|
| 271 |
EXTERN |
| 272 |
void * |
| 273 |
libc_malloc (size_t size) |
| 274 |
{ |
| 275 |
return malloc (size); |
| 276 |
} |
| 277 |
|
| 278 |
EXTERN |
| 279 |
void |
| 280 |
libc_free (void *p) |
| 281 |
{ |
| 282 |
free (p); |
| 283 |
} |
| 284 |
|
| 285 |
EXTERN |
| 286 |
char * |
| 287 |
libc_strcpy (char *dest, char *src) |
| 288 |
{ |
| 289 |
return strcpy (dest, src); |
| 290 |
} |
| 291 |
|
| 292 |
EXTERN |
| 293 |
char * |
| 294 |
libc_strncpy (char *dest, char *src, int n) |
| 295 |
{ |
| 296 |
return strncpy (dest, src, n); |
| 297 |
} |
| 298 |
|
| 299 |
EXTERN |
| 300 |
int |
| 301 |
libc_unlink (char *p) |
| 302 |
{ |
| 303 |
return unlink (p); |
| 304 |
} |
| 305 |
|
| 306 |
EXTERN |
| 307 |
int |
| 308 |
libc_system (char *command) |
| 309 |
{ |
| 310 |
return system (command); |
| 311 |
} |
| 312 |
|
| 313 |
EXTERN |
| 314 |
void * |
| 315 |
libc_memcpy (void *dest, void *src, size_t n) |
| 316 |
{ |
| 317 |
return memcpy (dest, src, n); |
| 318 |
} |
| 319 |
|
| 320 |
EXTERN |
| 321 |
char * |
| 322 |
libc_getenv (char *name) |
| 323 |
{ |
| 324 |
return getenv (name); |
| 325 |
} |
| 326 |
|
| 327 |
EXTERN |
| 328 |
int |
| 329 |
libc_putenv (char *name) |
| 330 |
{ |
| 331 |
return putenv (name); |
| 332 |
} |
| 333 |
|
| 334 |
EXTERN |
| 335 |
int |
| 336 |
libc_creat (char *p, mode_t mode) |
| 337 |
{ |
| 338 |
tracedb ("libc_creat (%s, 0x%x)\n", p, mode); |
| 339 |
int result = creat (p, mode); |
| 340 |
tracedb_result (result); |
| 341 |
return result; |
| 342 |
} |
| 343 |
|
| 344 |
EXTERN |
| 345 |
int |
| 346 |
libc_open (void *p, int flags, int mode) |
| 347 |
{ |
| 348 |
tracedb_open (p, flags, mode); |
| 349 |
int result = open (reinterpret_cast <char *> (p), flags, mode); |
| 350 |
tracedb_result (result); |
| 351 |
return result; |
| 352 |
} |
| 353 |
|
| 354 |
EXTERN |
| 355 |
off_t |
| 356 |
libc_lseek (int fd, off_t offset, int whence) |
| 357 |
{ |
| 358 |
tracedb ("libc_lseek (%s, %p, %d)\n", fd, offset, whence); |
| 359 |
off_t result = lseek (fd, offset, whence); |
| 360 |
tracedb_result (result); |
| 361 |
return result; |
| 362 |
} |
| 363 |
|
| 364 |
EXTERN |
| 365 |
void * |
| 366 |
libc_realloc (void *ptr, size_t size) |
| 367 |
{ |
| 368 |
return realloc (ptr, size); |
| 369 |
} |
| 370 |
|
| 371 |
EXTERN |
| 372 |
void * |
| 373 |
libc_memset (void *s, int c, size_t n) |
| 374 |
{ |
| 375 |
return memset (s, c, n); |
| 376 |
} |
| 377 |
|
| 378 |
EXTERN |
| 379 |
void * |
| 380 |
libc_memmove (void *dest, void *src, size_t n) |
| 381 |
{ |
| 382 |
return memmove (dest, src, n); |
| 383 |
} |
| 384 |
|
| 385 |
EXTERN |
| 386 |
int |
| 387 |
libc_getpid (void) |
| 388 |
{ |
| 389 |
return getpid (); |
| 390 |
} |
| 391 |
|
| 392 |
EXTERN |
| 393 |
unsigned int |
| 394 |
libc_sleep (unsigned int s) |
| 395 |
{ |
| 396 |
return sleep (s); |
| 397 |
} |
| 398 |
|
| 399 |
EXTERN |
| 400 |
int |
| 401 |
libc_atexit (void (*function) (void)) |
| 402 |
{ |
| 403 |
return atexit (function); |
| 404 |
} |