| 1 |
(* libc.def provides an interface to the C library functions. |
| 2 |
|
| 3 |
Copyright (C) 2001-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 |
Under Section 7 of GPL version 3, you are granted additional |
| 19 |
permissions described in the GCC Runtime Library Exception, version |
| 20 |
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 |
DEFINITION MODULE FOR "C" libc ; |
| 28 |
|
| 29 |
FROM SYSTEM IMPORT ADDRESS, CSIZE_T, CSSIZE_T, COFF_T ; |
| 30 |
|
| 31 |
EXPORT UNQUALIFIED time_t, timeb, tm, ptrToTM, |
| 32 |
atof, atoi, atol, atoll, |
| 33 |
strtod, strtof, strtold, strtol, strtoll, strtoul, strtoull, |
| 34 |
write, read, |
| 35 |
system, abort, |
| 36 |
malloc, free, |
| 37 |
exit, isatty, |
| 38 |
getenv, putenv, getpid, |
| 39 |
dup, close, open, lseek, |
| 40 |
readv, writev, |
| 41 |
perror, creat, |
| 42 |
getcwd, chown, strlen, strcpy, strncpy, |
| 43 |
unlink, setenv, |
| 44 |
memcpy, memset, memmove, printf, realloc, |
| 45 |
rand, srand, |
| 46 |
time, localtime, ftime, |
| 47 |
shutdown, snprintf, |
| 48 |
rename, setjmp, longjmp, atexit, |
| 49 |
ttyname, sleep, execv ; |
| 50 |
|
| 51 |
|
| 52 |
TYPE |
| 53 |
time_t = LONGINT ; |
| 54 |
|
| 55 |
ptrToTM = POINTER TO tm ; |
| 56 |
tm = RECORD |
| 57 |
tm_sec: INTEGER ; (* Seconds. [0-60] (1 leap second) *) |
| 58 |
tm_min: INTEGER ; (* Minutes. [0-59] *) |
| 59 |
tm_hour: INTEGER ; (* Hours. [0-23] *) |
| 60 |
tm_mday: INTEGER ; (* Day. [1-31] *) |
| 61 |
tm_mon: INTEGER ; (* Month. [0-11] *) |
| 62 |
tm_year: INTEGER ; (* Year - 1900. *) |
| 63 |
tm_wday: INTEGER ; (* Day of week. [0-6] *) |
| 64 |
tm_yday: INTEGER ; (* Days in year.[0-365] *) |
| 65 |
tm_isdst: INTEGER ; (* DST. [-1/0/1] *) |
| 66 |
tm_gmtoff: LONGINT ; (* Seconds east of UTC. *) |
| 67 |
tm_zone: ADDRESS ; (* char * zone name *) |
| 68 |
END ; |
| 69 |
|
| 70 |
timeb = RECORD |
| 71 |
time : time_t ; |
| 72 |
millitm : SHORTCARD ; |
| 73 |
timezone: SHORTCARD ; |
| 74 |
dstflag : SHORTCARD ; |
| 75 |
END ; |
| 76 |
|
| 77 |
exitP = PROCEDURE () : INTEGER ; |
| 78 |
|
| 79 |
|
| 80 |
(* |
| 81 |
double atof(const char *nptr) |
| 82 |
*) |
| 83 |
|
| 84 |
PROCEDURE atof (nptr: ADDRESS) : REAL ; |
| 85 |
|
| 86 |
|
| 87 |
(* |
| 88 |
int atoi(const char *nptr) |
| 89 |
*) |
| 90 |
|
| 91 |
PROCEDURE atoi (nptr: ADDRESS) : INTEGER ; |
| 92 |
|
| 93 |
|
| 94 |
(* |
| 95 |
long atol(const char *nptr); |
| 96 |
*) |
| 97 |
|
| 98 |
PROCEDURE atol (nptr: ADDRESS) : CSSIZE_T ; |
| 99 |
|
| 100 |
|
| 101 |
(* |
| 102 |
long long atoll(const char *nptr); |
| 103 |
*) |
| 104 |
|
| 105 |
PROCEDURE atoll (nptr: ADDRESS) : LONGINT ; |
| 106 |
|
| 107 |
|
| 108 |
(* |
| 109 |
double strtod(const char *restrict nptr, char **_Nullable restrict endptr) |
| 110 |
*) |
| 111 |
|
| 112 |
PROCEDURE strtod (nptr, endptr: ADDRESS) : REAL ; |
| 113 |
|
| 114 |
|
| 115 |
(* |
| 116 |
float strtof(const char *restrict nptr, char **_Nullable restrict endptr) |
| 117 |
*) |
| 118 |
|
| 119 |
PROCEDURE strtof (nptr, endptr: ADDRESS) : SHORTREAL ; |
| 120 |
|
| 121 |
|
| 122 |
(* |
| 123 |
long double strtold(const char *restrict nptr, |
| 124 |
char **_Nullable restrict endptr) |
| 125 |
*) |
| 126 |
|
| 127 |
PROCEDURE strtold (nptr, endptr: ADDRESS) : LONGREAL ; |
| 128 |
|
| 129 |
|
| 130 |
(* |
| 131 |
long strtol(const char *restrict nptr, char **_Nullable restrict endptr, |
| 132 |
int base) |
| 133 |
*) |
| 134 |
|
| 135 |
PROCEDURE strtol (nptr, endptr: ADDRESS; base: INTEGER) : CSSIZE_T ; |
| 136 |
|
| 137 |
|
| 138 |
(* |
| 139 |
long long strtoll(const char *restrict nptr, |
| 140 |
char **_Nullable restrict endptr, int base) |
| 141 |
*) |
| 142 |
|
| 143 |
PROCEDURE strtoll (nptr, endptr: ADDRESS; base: INTEGER) : LONGINT ; |
| 144 |
|
| 145 |
|
| 146 |
(* |
| 147 |
unsigned long strtoul(const char *restrict nptr, |
| 148 |
char **_Nullable restrict endptr, int base) |
| 149 |
*) |
| 150 |
|
| 151 |
PROCEDURE strtoul (nptr, endptr: ADDRESS; base: INTEGER) : CSIZE_T ; |
| 152 |
|
| 153 |
|
| 154 |
(* |
| 155 |
unsigned long long strtoull(const char *restrict nptr, |
| 156 |
char **_Nullable restrict endptr, int base) |
| 157 |
*) |
| 158 |
|
| 159 |
PROCEDURE strtoull (nptr, endptr: ADDRESS; base: INTEGER) : LONGCARD ; |
| 160 |
|
| 161 |
|
| 162 |
(* |
| 163 |
ssize_t write (int d, void *buf, size_t nbytes) |
| 164 |
*) |
| 165 |
|
| 166 |
PROCEDURE write (d: INTEGER; buf: ADDRESS; nbytes: CSIZE_T) : [ CSSIZE_T ] ; |
| 167 |
|
| 168 |
|
| 169 |
(* |
| 170 |
ssize_t read (int d, void *buf, size_t nbytes) |
| 171 |
*) |
| 172 |
|
| 173 |
PROCEDURE read (d: INTEGER; buf: ADDRESS; nbytes: CSIZE_T) : [ CSSIZE_T ] ; |
| 174 |
|
| 175 |
|
| 176 |
(* |
| 177 |
int system(string) |
| 178 |
char *string; |
| 179 |
*) |
| 180 |
|
| 181 |
PROCEDURE system (a: ADDRESS) : [ INTEGER ] ; |
| 182 |
|
| 183 |
|
| 184 |
(* |
| 185 |
abort - generate a fault |
| 186 |
|
| 187 |
abort() first closes all open files if possible, then sends |
| 188 |
an IOT signal to the process. This signal usually results |
| 189 |
in termination with a core dump, which may be used for |
| 190 |
debugging. |
| 191 |
|
| 192 |
It is possible for abort() to return control if is caught or |
| 193 |
ignored, in which case the value returned is that of the |
| 194 |
kill(2V) system call. |
| 195 |
*) |
| 196 |
|
| 197 |
PROCEDURE abort <* noreturn *> ; |
| 198 |
|
| 199 |
|
| 200 |
(* |
| 201 |
malloc - memory allocator. |
| 202 |
|
| 203 |
void *malloc(size_t size); |
| 204 |
|
| 205 |
malloc() returns a pointer to a block of at least size |
| 206 |
bytes, which is appropriately aligned. If size is zero, |
| 207 |
malloc() returns a non-NULL pointer, but this pointer should |
| 208 |
not be dereferenced. |
| 209 |
*) |
| 210 |
|
| 211 |
PROCEDURE malloc (size: CSIZE_T) : ADDRESS ; |
| 212 |
|
| 213 |
|
| 214 |
(* |
| 215 |
free - memory deallocator. |
| 216 |
|
| 217 |
free (void *ptr); |
| 218 |
|
| 219 |
free() releases a previously allocated block. Its argument |
| 220 |
is a pointer to a block previously allocated by malloc, |
| 221 |
calloc, realloc, malloc, or memalign. |
| 222 |
*) |
| 223 |
|
| 224 |
PROCEDURE free (ptr: ADDRESS) ; |
| 225 |
|
| 226 |
|
| 227 |
(* |
| 228 |
void *realloc (void *ptr, size_t size); |
| 229 |
|
| 230 |
realloc changes the size of the memory block pointed to |
| 231 |
by ptr to size bytes. The contents will be unchanged to |
| 232 |
the minimum of the old and new sizes; newly allocated memory |
| 233 |
will be uninitialized. If ptr is NIL, the call is |
| 234 |
equivalent to malloc(size); if size is equal to zero, the |
| 235 |
call is equivalent to free(ptr). Unless ptr is NIL, it |
| 236 |
must have been returned by an earlier call to malloc(), |
| 237 |
realloc. |
| 238 |
*) |
| 239 |
|
| 240 |
PROCEDURE realloc (ptr: ADDRESS; size: CSIZE_T) : ADDRESS ; |
| 241 |
|
| 242 |
|
| 243 |
(* |
| 244 |
isatty - does this descriptor refer to a terminal. |
| 245 |
*) |
| 246 |
|
| 247 |
PROCEDURE isatty (fd: INTEGER) : INTEGER ; |
| 248 |
|
| 249 |
|
| 250 |
(* |
| 251 |
exit - returns control to the invoking process. Result, r, is |
| 252 |
returned. |
| 253 |
*) |
| 254 |
|
| 255 |
PROCEDURE exit (r: INTEGER) <* noreturn *> ; |
| 256 |
|
| 257 |
|
| 258 |
(* |
| 259 |
getenv - returns the C string for the equivalent C environment |
| 260 |
variable. |
| 261 |
*) |
| 262 |
|
| 263 |
PROCEDURE getenv (s: ADDRESS) : ADDRESS ; |
| 264 |
|
| 265 |
|
| 266 |
(* |
| 267 |
putenv - change or add an environment variable. |
| 268 |
*) |
| 269 |
|
| 270 |
PROCEDURE putenv (s: ADDRESS) : INTEGER ; |
| 271 |
|
| 272 |
|
| 273 |
(* |
| 274 |
getpid - returns the UNIX process identification number. |
| 275 |
*) |
| 276 |
|
| 277 |
PROCEDURE getpid () : INTEGER ; |
| 278 |
|
| 279 |
|
| 280 |
(* |
| 281 |
dup - duplicates the file descriptor, d. |
| 282 |
*) |
| 283 |
|
| 284 |
PROCEDURE dup (d: INTEGER) : INTEGER ; |
| 285 |
|
| 286 |
|
| 287 |
(* |
| 288 |
close - closes the file descriptor, d. |
| 289 |
*) |
| 290 |
|
| 291 |
PROCEDURE close (d: INTEGER) : [ INTEGER ] ; |
| 292 |
|
| 293 |
|
| 294 |
(* |
| 295 |
open - open the file, filename with flag and mode. |
| 296 |
*) |
| 297 |
|
| 298 |
PROCEDURE open (filename: ADDRESS; oflag: INTEGER; mode: INTEGER) : INTEGER ; |
| 299 |
|
| 300 |
|
| 301 |
(* |
| 302 |
creat - creates a new file |
| 303 |
*) |
| 304 |
|
| 305 |
PROCEDURE creat (filename: ADDRESS; mode: CARDINAL) : INTEGER; |
| 306 |
|
| 307 |
|
| 308 |
(* |
| 309 |
lseek - calls unix lseek: |
| 310 |
|
| 311 |
off_t lseek(int fildes, off_t offset, int whence); |
| 312 |
*) |
| 313 |
|
| 314 |
PROCEDURE lseek (fd: INTEGER; offset: COFF_T; whence: INTEGER) : [ COFF_T ] ; |
| 315 |
|
| 316 |
|
| 317 |
(* |
| 318 |
perror - writes errno and string. (ARRAY OF CHAR is translated onto ADDRESS). |
| 319 |
*) |
| 320 |
|
| 321 |
PROCEDURE perror (string: ARRAY OF CHAR); |
| 322 |
|
| 323 |
|
| 324 |
(* |
| 325 |
readv - reads an io vector of bytes. |
| 326 |
*) |
| 327 |
|
| 328 |
PROCEDURE readv (fd: INTEGER; v: ADDRESS; n: INTEGER) : [ INTEGER ] ; |
| 329 |
|
| 330 |
|
| 331 |
(* |
| 332 |
writev - writes an io vector of bytes. |
| 333 |
*) |
| 334 |
|
| 335 |
PROCEDURE writev (fd: INTEGER; v: ADDRESS; n: INTEGER) : [ INTEGER ] ; |
| 336 |
|
| 337 |
|
| 338 |
(* |
| 339 |
getcwd - copies the absolute pathname of the |
| 340 |
current working directory to the array pointed to by buf, |
| 341 |
which is of length size. |
| 342 |
|
| 343 |
If the current absolute path name would require a buffer |
| 344 |
longer than size elements, NULL is returned, and errno is |
| 345 |
set to ERANGE; an application should check for this error, |
| 346 |
and allocate a larger buffer if necessary. |
| 347 |
*) |
| 348 |
|
| 349 |
PROCEDURE getcwd (buf: ADDRESS; size: CSIZE_T) : ADDRESS ; |
| 350 |
|
| 351 |
|
| 352 |
(* |
| 353 |
chown - The owner of the file specified by path or by fd is |
| 354 |
changed. Only the super-user may change the owner of a |
| 355 |
file. The owner of a file may change the group of the |
| 356 |
file to any group of which that owner is a member. The |
| 357 |
super-user may change the group arbitrarily. |
| 358 |
|
| 359 |
If the owner or group is specified as -1, then that ID is |
| 360 |
not changed. |
| 361 |
|
| 362 |
On success, zero is returned. On error, -1 is returned, |
| 363 |
and errno is set appropriately. |
| 364 |
*) |
| 365 |
|
| 366 |
PROCEDURE chown (filename: ADDRESS; uid, gid: INTEGER) : [ INTEGER ] ; |
| 367 |
|
| 368 |
|
| 369 |
(* |
| 370 |
strlen - returns the length of string, a. |
| 371 |
*) |
| 372 |
|
| 373 |
PROCEDURE strlen (a: ADDRESS) : CSIZE_T ; |
| 374 |
|
| 375 |
|
| 376 |
(* |
| 377 |
strcpy - copies string, src, into, dest. |
| 378 |
It returns dest. |
| 379 |
*) |
| 380 |
|
| 381 |
PROCEDURE strcpy (dest, src: ADDRESS) : [ ADDRESS ] ; |
| 382 |
|
| 383 |
|
| 384 |
(* |
| 385 |
strncpy - copies string, src, into, dest, copying at most, n, bytes. |
| 386 |
It returns dest. |
| 387 |
*) |
| 388 |
|
| 389 |
PROCEDURE strncpy (dest, src: ADDRESS; n: CARDINAL) : [ ADDRESS ] ; |
| 390 |
|
| 391 |
|
| 392 |
(* |
| 393 |
unlink - removes file and returns 0 if successful. |
| 394 |
*) |
| 395 |
|
| 396 |
PROCEDURE unlink (file: ADDRESS) : [ INTEGER ] ; |
| 397 |
|
| 398 |
|
| 399 |
(* |
| 400 |
memcpy - copy memory area |
| 401 |
|
| 402 |
SYNOPSIS |
| 403 |
|
| 404 |
#include <string.h> |
| 405 |
|
| 406 |
void *memcpy(void *dest, const void *src, size_t n); |
| 407 |
It returns dest. |
| 408 |
*) |
| 409 |
|
| 410 |
PROCEDURE memcpy (dest, src: ADDRESS; size: CSIZE_T) : [ ADDRESS ] ; |
| 411 |
|
| 412 |
|
| 413 |
(* |
| 414 |
memset - fill memory with a constant byte |
| 415 |
|
| 416 |
SYNOPSIS |
| 417 |
|
| 418 |
#include <string.h> |
| 419 |
|
| 420 |
void *memset(void *s, int c, size_t n); |
| 421 |
It returns s. |
| 422 |
*) |
| 423 |
|
| 424 |
PROCEDURE memset (s: ADDRESS; c: INTEGER; size: CSIZE_T) : [ ADDRESS ] ; |
| 425 |
|
| 426 |
|
| 427 |
(* |
| 428 |
memmove - copy memory areas which may overlap |
| 429 |
|
| 430 |
SYNOPSIS |
| 431 |
|
| 432 |
#include <string.h> |
| 433 |
|
| 434 |
void *memmove(void *dest, const void *src, size_t n); |
| 435 |
It returns dest. |
| 436 |
*) |
| 437 |
|
| 438 |
PROCEDURE memmove (dest, src: ADDRESS; size: CSIZE_T) : [ ADDRESS ] ; |
| 439 |
|
| 440 |
|
| 441 |
(* |
| 442 |
int printf(const char *format, ...); |
| 443 |
*) |
| 444 |
|
| 445 |
PROCEDURE printf (format: ARRAY OF CHAR; ...) : [ INTEGER ] ; |
| 446 |
|
| 447 |
|
| 448 |
(* |
| 449 |
int snprintf(char *str, size_t size, const char *format, ...); |
| 450 |
*) |
| 451 |
|
| 452 |
PROCEDURE snprintf (dest: ADDRESS; size: CSIZE_T; |
| 453 |
format: ARRAY OF CHAR; ...) : [ INTEGER ] ; |
| 454 |
|
| 455 |
(* |
| 456 |
setenv - sets environment variable, name, to value. |
| 457 |
It will overwrite an existing value if, overwrite, |
| 458 |
is true. It returns 0 on success and -1 for an error. |
| 459 |
*) |
| 460 |
|
| 461 |
PROCEDURE setenv (name: ADDRESS; value: ADDRESS; overwrite: INTEGER) : [ INTEGER ] ; |
| 462 |
|
| 463 |
|
| 464 |
(* |
| 465 |
srand - initialize the random number seed. |
| 466 |
*) |
| 467 |
|
| 468 |
PROCEDURE srand (seed: INTEGER) ; |
| 469 |
|
| 470 |
|
| 471 |
(* |
| 472 |
rand - return a random integer. |
| 473 |
*) |
| 474 |
|
| 475 |
PROCEDURE rand () : INTEGER ; |
| 476 |
|
| 477 |
|
| 478 |
(* |
| 479 |
time - returns a pointer to the time_t value. If, a, |
| 480 |
is not NIL then the libc value is copied into |
| 481 |
memory at address, a. |
| 482 |
*) |
| 483 |
|
| 484 |
PROCEDURE time (a: ADDRESS) : time_t ; |
| 485 |
|
| 486 |
|
| 487 |
(* |
| 488 |
localtime - returns a pointer to the libc copy of the tm |
| 489 |
structure. |
| 490 |
*) |
| 491 |
|
| 492 |
PROCEDURE localtime (VAR t: time_t) : ADDRESS ; |
| 493 |
|
| 494 |
|
| 495 |
(* |
| 496 |
ftime - return date and time. |
| 497 |
*) |
| 498 |
|
| 499 |
PROCEDURE ftime (VAR t: timeb) : [ INTEGER ] ; |
| 500 |
|
| 501 |
|
| 502 |
(* |
| 503 |
shutdown - shutdown a socket, s. |
| 504 |
if how = 0, then no more reads are allowed. |
| 505 |
if how = 1, then no more writes are allowed. |
| 506 |
if how = 2, then mo more reads or writes are allowed. |
| 507 |
*) |
| 508 |
|
| 509 |
PROCEDURE shutdown (s: INTEGER; how: INTEGER) : [ INTEGER ] ; |
| 510 |
|
| 511 |
|
| 512 |
(* |
| 513 |
rename - change the name or location of a file |
| 514 |
*) |
| 515 |
|
| 516 |
PROCEDURE rename (oldpath, newpath: ADDRESS) : [ INTEGER ] ; |
| 517 |
|
| 518 |
|
| 519 |
(* |
| 520 |
setjmp - returns 0 if returning directly, and non-zero |
| 521 |
when returning from longjmp using the saved |
| 522 |
context. |
| 523 |
*) |
| 524 |
|
| 525 |
PROCEDURE setjmp (env: ADDRESS) : INTEGER ; |
| 526 |
|
| 527 |
|
| 528 |
(* |
| 529 |
longjmp - restores the environment saved by the last call |
| 530 |
of setjmp with the corresponding env argument. |
| 531 |
After longjmp is completed, program execution |
| 532 |
continues as if the corresponding call of setjmp |
| 533 |
had just returned the value val. The value of |
| 534 |
val must not be zero. |
| 535 |
*) |
| 536 |
|
| 537 |
PROCEDURE longjmp (env: ADDRESS; val: INTEGER) ; |
| 538 |
|
| 539 |
|
| 540 |
(* |
| 541 |
atexit - execute, proc, when the function exit is called. |
| 542 |
*) |
| 543 |
|
| 544 |
PROCEDURE atexit (proc: exitP) : [ INTEGER ] ; |
| 545 |
|
| 546 |
|
| 547 |
(* |
| 548 |
ttyname - returns a pointer to a string determining the ttyname. |
| 549 |
*) |
| 550 |
|
| 551 |
PROCEDURE ttyname (filedes: INTEGER) : ADDRESS ; |
| 552 |
|
| 553 |
|
| 554 |
(* |
| 555 |
sleep - calling thread sleeps for seconds. |
| 556 |
*) |
| 557 |
|
| 558 |
PROCEDURE sleep (seconds: CARDINAL) : [ CARDINAL ] ; |
| 559 |
|
| 560 |
|
| 561 |
(* |
| 562 |
execv - execute a file. |
| 563 |
*) |
| 564 |
|
| 565 |
PROCEDURE execv (pathname: ADDRESS; argv: ADDRESS) : [ INTEGER ] ; |
| 566 |
|
| 567 |
|
| 568 |
END libc. |