| 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 |
EXTERN |
| 32 |
size_t |
| 33 |
libc_read (int fd, void *a, size_t nbytes) |
| 34 |
{ |
| 35 |
return read (fd, a, nbytes); |
| 36 |
} |
| 37 |
|
| 38 |
EXTERN |
| 39 |
size_t |
| 40 |
libc_write (int fd, void *a, size_t nbytes) |
| 41 |
{ |
| 42 |
return write (fd, a, nbytes); |
| 43 |
} |
| 44 |
|
| 45 |
EXTERN |
| 46 |
int |
| 47 |
libc_close (int fd) |
| 48 |
{ |
| 49 |
return close (fd); |
| 50 |
} |
| 51 |
|
| 52 |
EXTERN |
| 53 |
int |
| 54 |
libc_exit (int code) |
| 55 |
{ |
| 56 |
exit (code); |
| 57 |
} |
| 58 |
|
| 59 |
EXTERN |
| 60 |
void |
| 61 |
libc_perror (const char *s) |
| 62 |
{ |
| 63 |
perror (s); |
| 64 |
} |
| 65 |
|
| 66 |
EXTERN |
| 67 |
int |
| 68 |
libc_abort () |
| 69 |
{ |
| 70 |
abort (); |
| 71 |
} |
| 72 |
|
| 73 |
EXTERN |
| 74 |
size_t |
| 75 |
libc_strlen (char *s) |
| 76 |
{ |
| 77 |
return strlen (s); |
| 78 |
} |
| 79 |
|
| 80 |
EXTERN |
| 81 |
int |
| 82 |
libc_printf (const char *_format, unsigned int _format_high, ...) |
| 83 |
{ |
| 84 |
va_list arg; |
| 85 |
int done; |
| 86 |
char format[_format_high + 1]; |
| 87 |
unsigned int i = 0; |
| 88 |
unsigned int j = 0; |
| 89 |
char *c; |
| 90 |
|
| 91 |
do |
| 92 |
{ |
| 93 |
c = index (&const_cast <char *> (_format)[i], '\\'); |
| 94 |
if (c == NULL) |
| 95 |
strcpy (&format[j], &_format[i]); |
| 96 |
else |
| 97 |
{ |
| 98 |
memcpy (&format[j], &_format[i], (c - _format) - i); |
| 99 |
i = c - _format; |
| 100 |
j += c - _format; |
| 101 |
if (_format[i + 1] == 'n') |
| 102 |
format[j] = '\n'; |
| 103 |
else |
| 104 |
format[j] = _format[i + 1]; |
| 105 |
j++; |
| 106 |
i += 2; |
| 107 |
} |
| 108 |
} |
| 109 |
while (c != NULL); |
| 110 |
|
| 111 |
va_start (arg, _format_high); |
| 112 |
done = vfprintf (stdout, format, arg); |
| 113 |
va_end (arg); |
| 114 |
|
| 115 |
return done; |
| 116 |
} |
| 117 |
|
| 118 |
EXTERN |
| 119 |
int |
| 120 |
libc_snprintf (void *dest, size_t length, const char *_format, unsigned int _format_high, ...) |
| 121 |
{ |
| 122 |
va_list arg; |
| 123 |
int done; |
| 124 |
char format[_format_high + 1]; |
| 125 |
unsigned int i = 0; |
| 126 |
unsigned int j = 0; |
| 127 |
char *c; |
| 128 |
|
| 129 |
do |
| 130 |
{ |
| 131 |
c = index (&const_cast <char *> (_format)[i], '\\'); |
| 132 |
if (c == NULL) |
| 133 |
strcpy (&format[j], &_format[i]); |
| 134 |
else |
| 135 |
{ |
| 136 |
memcpy (&format[j], &_format[i], (c - _format) - i); |
| 137 |
i = c - _format; |
| 138 |
j += c - _format; |
| 139 |
if (_format[i + 1] == 'n') |
| 140 |
format[j] = '\n'; |
| 141 |
else |
| 142 |
format[j] = _format[i + 1]; |
| 143 |
j++; |
| 144 |
i += 2; |
| 145 |
} |
| 146 |
} |
| 147 |
while (c != NULL); |
| 148 |
|
| 149 |
va_start (arg, _format_high); |
| 150 |
done = vsnprintf (reinterpret_cast<char *> (dest), length, format, arg); |
| 151 |
va_end (arg); |
| 152 |
return done; |
| 153 |
} |
| 154 |
|
| 155 |
EXTERN |
| 156 |
void * |
| 157 |
libc_malloc (size_t size) |
| 158 |
{ |
| 159 |
return malloc (size); |
| 160 |
} |
| 161 |
|
| 162 |
EXTERN |
| 163 |
void |
| 164 |
libc_free (void *p) |
| 165 |
{ |
| 166 |
free (p); |
| 167 |
} |
| 168 |
|
| 169 |
EXTERN |
| 170 |
char * |
| 171 |
libc_strcpy (char *dest, char *src) |
| 172 |
{ |
| 173 |
return strcpy (dest, src); |
| 174 |
} |
| 175 |
|
| 176 |
EXTERN |
| 177 |
char * |
| 178 |
libc_strncpy (char *dest, char *src, int n) |
| 179 |
{ |
| 180 |
return strncpy (dest, src, n); |
| 181 |
} |
| 182 |
|
| 183 |
EXTERN |
| 184 |
int |
| 185 |
libc_unlink (char *p) |
| 186 |
{ |
| 187 |
return unlink (p); |
| 188 |
} |
| 189 |
|
| 190 |
EXTERN |
| 191 |
int |
| 192 |
libc_system (char *command) |
| 193 |
{ |
| 194 |
return system (command); |
| 195 |
} |
| 196 |
|
| 197 |
EXTERN |
| 198 |
void * |
| 199 |
libc_memcpy (void *dest, void *src, size_t nbytes) |
| 200 |
{ |
| 201 |
return memcpy (dest, src, nbytes); |
| 202 |
} |
| 203 |
|
| 204 |
EXTERN |
| 205 |
char * |
| 206 |
libc_getenv (char *name) |
| 207 |
{ |
| 208 |
return getenv (name); |
| 209 |
} |
| 210 |
|
| 211 |
EXTERN |
| 212 |
int |
| 213 |
libc_putenv (char *name) |
| 214 |
{ |
| 215 |
return putenv (name); |
| 216 |
} |
| 217 |
|
| 218 |
EXTERN |
| 219 |
int |
| 220 |
libc_creat (char *p, mode_t mode) |
| 221 |
{ |
| 222 |
return creat (p, mode); |
| 223 |
} |
| 224 |
|
| 225 |
EXTERN |
| 226 |
int |
| 227 |
libc_open (void *p, int oflag, int mode) |
| 228 |
{ |
| 229 |
return open (reinterpret_cast <char *> (p), oflag, mode); |
| 230 |
} |
| 231 |
|
| 232 |
EXTERN |
| 233 |
off_t |
| 234 |
libc_lseek (int fd, off_t offset, int whence) |
| 235 |
{ |
| 236 |
return lseek (fd, offset, whence); |
| 237 |
} |
| 238 |
|
| 239 |
EXTERN |
| 240 |
void * |
| 241 |
libc_realloc (void *ptr, size_t size) |
| 242 |
{ |
| 243 |
return realloc (ptr, size); |
| 244 |
} |
| 245 |
|
| 246 |
EXTERN |
| 247 |
void * |
| 248 |
libc_memset (void *s, int c, size_t n) |
| 249 |
{ |
| 250 |
return memset (s, c, n); |
| 251 |
} |
| 252 |
|
| 253 |
EXTERN |
| 254 |
void * |
| 255 |
libc_memmove (void *dest, void *src, size_t n) |
| 256 |
{ |
| 257 |
return memmove (dest, src, n); |
| 258 |
} |
| 259 |
|
| 260 |
EXTERN |
| 261 |
int |
| 262 |
libc_getpid (void) |
| 263 |
{ |
| 264 |
return getpid (); |
| 265 |
} |
| 266 |
|
| 267 |
EXTERN |
| 268 |
unsigned int |
| 269 |
libc_sleep (unsigned int s) |
| 270 |
{ |
| 271 |
return sleep (s); |
| 272 |
} |
| 273 |
|
| 274 |
EXTERN |
| 275 |
int |
| 276 |
libc_atexit (void (*function) (void)) |
| 277 |
{ |
| 278 |
return atexit (function); |
| 279 |
} |