| 1 |
/* wrapc.c provide access to miscellaneous C library functions. |
| 2 |
|
| 3 |
Copyright (C) 2005-2022 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 |
#include <config.h> |
| 28 |
#include <m2rts.h> |
| 29 |
|
| 30 |
#define EXPORT(FUNC) m2pim ## _wrapc_ ## FUNC |
| 31 |
#define M2EXPORT(FUNC) m2pim ## _M2_wrapc_ ## FUNC |
| 32 |
#define M2LIBNAME "m2pim" |
| 33 |
|
| 34 |
#if defined(HAVE_MATH_H) |
| 35 |
#include <math.h> |
| 36 |
#endif |
| 37 |
|
| 38 |
#if defined(HAVE_STDLIB_H) |
| 39 |
#include <stdlib.h> |
| 40 |
#endif |
| 41 |
|
| 42 |
#if defined(HAVE_UNISTD_H) |
| 43 |
#include <unistd.h> |
| 44 |
#endif |
| 45 |
|
| 46 |
#if defined(HAVE_SYS_STAT_H) |
| 47 |
#include <sys/stat.h> |
| 48 |
#endif |
| 49 |
|
| 50 |
#ifdef HAVE_STDIO_H |
| 51 |
#include <stdio.h> |
| 52 |
#endif |
| 53 |
|
| 54 |
#if defined(HAVE_SYS_TYPES_H) |
| 55 |
#include <sys/types.h> |
| 56 |
#endif |
| 57 |
|
| 58 |
#if defined(HAVE_TIME_H) |
| 59 |
#include <time.h> |
| 60 |
#endif |
| 61 |
|
| 62 |
#ifdef HAVE_FCNTL_H |
| 63 |
#include <fcntl.h> |
| 64 |
#endif |
| 65 |
|
| 66 |
/* Define a generic NULL if one hasn't already been defined. */ |
| 67 |
|
| 68 |
#if !defined(NULL) |
| 69 |
#define NULL 0 |
| 70 |
#endif |
| 71 |
|
| 72 |
/* strtime returns the address of a string which describes the |
| 73 |
local time. */ |
| 74 |
|
| 75 |
extern "C" char * |
| 76 |
EXPORT(strtime) (void) |
| 77 |
{ |
| 78 |
#if defined(HAVE_CTIME) |
| 79 |
time_t clock = time (NULL); |
| 80 |
char *string = ctime (&clock); |
| 81 |
|
| 82 |
string[24] = (char)0; |
| 83 |
|
| 84 |
return string; |
| 85 |
#else |
| 86 |
return ""; |
| 87 |
#endif |
| 88 |
} |
| 89 |
|
| 90 |
extern "C" int |
| 91 |
EXPORT(filesize) (int f, unsigned int *low, unsigned int *high) |
| 92 |
{ |
| 93 |
#if defined(HAVE_SYS_STAT_H) && defined(HAVE_STRUCT_STAT) |
| 94 |
struct stat s; |
| 95 |
int res = fstat (f, (struct stat *)&s); |
| 96 |
|
| 97 |
if (res == 0) |
| 98 |
{ |
| 99 |
*low = (unsigned int)s.st_size; |
| 100 |
*high = (unsigned int)(s.st_size >> (sizeof (unsigned int) * 8)); |
| 101 |
} |
| 102 |
return res; |
| 103 |
#else |
| 104 |
return -1; |
| 105 |
#endif |
| 106 |
} |
| 107 |
|
| 108 |
/* filemtime returns the mtime of a file, f. */ |
| 109 |
|
| 110 |
extern "C" int |
| 111 |
EXPORT(filemtime) (int f) |
| 112 |
{ |
| 113 |
#if defined(HAVE_SYS_STAT_H) && defined(HAVE_STRUCT_STAT) |
| 114 |
struct stat s; |
| 115 |
|
| 116 |
if (fstat (f, (struct stat *)&s) == 0) |
| 117 |
return s.st_mtime; |
| 118 |
else |
| 119 |
return -1; |
| 120 |
#else |
| 121 |
return -1; |
| 122 |
#endif |
| 123 |
} |
| 124 |
|
| 125 |
/* fileinode returns the inode associated with a file, f. */ |
| 126 |
|
| 127 |
#if defined(HAVE_SYS_STAT_H) && defined(HAVE_STRUCT_STAT) |
| 128 |
extern "C" ino_t |
| 129 |
EXPORT(fileinode) (int f, unsigned int *low, unsigned int *high) |
| 130 |
{ |
| 131 |
struct stat s; |
| 132 |
|
| 133 |
if (fstat (f, (struct stat *)&s) == 0) |
| 134 |
{ |
| 135 |
*low = (unsigned int)s.st_ino; |
| 136 |
if ((sizeof (s.st_ino) == (sizeof (unsigned int)))) |
| 137 |
*high = 0; |
| 138 |
else |
| 139 |
*high = (unsigned int)(s.st_ino >> (sizeof (unsigned int) * 8)); |
| 140 |
return 0; |
| 141 |
} |
| 142 |
else |
| 143 |
return -1; |
| 144 |
} |
| 145 |
#else |
| 146 |
extern "C" int |
| 147 |
EXPORT(fileinode) (int f, unsigned int *low, unsigned int *high) |
| 148 |
{ |
| 149 |
*low = 0; |
| 150 |
*high = 0; |
| 151 |
return -1; |
| 152 |
} |
| 153 |
#endif |
| 154 |
|
| 155 |
/* getrand returns a random number between 0..n-1. */ |
| 156 |
|
| 157 |
extern "C" int |
| 158 |
EXPORT(getrand) (int n) |
| 159 |
{ |
| 160 |
return rand () % n; |
| 161 |
} |
| 162 |
|
| 163 |
#if defined(HAVE_PWD_H) |
| 164 |
#include <pwd.h> |
| 165 |
|
| 166 |
extern "C" char * |
| 167 |
EXPORT(getusername) (void) |
| 168 |
{ |
| 169 |
return getpwuid (getuid ())->pw_gecos; |
| 170 |
} |
| 171 |
|
| 172 |
/* getnameuidgid fills in the, uid, and, gid, which represents |
| 173 |
user, name. */ |
| 174 |
|
| 175 |
extern "C" void |
| 176 |
EXPORT(getnameuidgid) (char *name, int *uid, int *gid) |
| 177 |
{ |
| 178 |
struct passwd *p = getpwnam (name); |
| 179 |
|
| 180 |
if (p == NULL) |
| 181 |
{ |
| 182 |
*uid = -1; |
| 183 |
*gid = -1; |
| 184 |
} |
| 185 |
else |
| 186 |
{ |
| 187 |
*uid = p->pw_uid; |
| 188 |
*gid = p->pw_gid; |
| 189 |
} |
| 190 |
} |
| 191 |
#else |
| 192 |
extern "C" char * |
| 193 |
EXPORT(getusername) (void) |
| 194 |
{ |
| 195 |
return "unknown"; |
| 196 |
} |
| 197 |
|
| 198 |
extern "C" void |
| 199 |
EXPORT(getnameuidgid) (char *name, int *uid, int *gid) |
| 200 |
{ |
| 201 |
*uid = -1; |
| 202 |
*gid = -1; |
| 203 |
} |
| 204 |
#endif |
| 205 |
|
| 206 |
extern "C" int |
| 207 |
EXPORT(signbit) (double r) |
| 208 |
{ |
| 209 |
#if defined(HAVE_SIGNBIT) |
| 210 |
|
| 211 |
/* signbit is a macro which tests its argument against sizeof(float), |
| 212 |
sizeof(double). */ |
| 213 |
return signbit (r); |
| 214 |
#else |
| 215 |
return false; |
| 216 |
#endif |
| 217 |
} |
| 218 |
|
| 219 |
extern "C" int |
| 220 |
EXPORT(signbitl) (long double r) |
| 221 |
{ |
| 222 |
#if defined(HAVE_SIGNBITL) |
| 223 |
|
| 224 |
/* signbit is a macro which tests its argument against sizeof(float), |
| 225 |
sizeof(double). */ |
| 226 |
return signbitl (r); |
| 227 |
#else |
| 228 |
return false; |
| 229 |
#endif |
| 230 |
} |
| 231 |
|
| 232 |
extern "C" int |
| 233 |
EXPORT(signbitf) (float r) |
| 234 |
{ |
| 235 |
#if defined(HAVE_SIGNBITF) |
| 236 |
|
| 237 |
/* signbit is a macro which tests its argument against sizeof(float), |
| 238 |
sizeof(double). */ |
| 239 |
return signbitf (r); |
| 240 |
#else |
| 241 |
return false; |
| 242 |
#endif |
| 243 |
} |
| 244 |
|
| 245 |
/* isfinite provide non builtin alternative to the gcc builtin |
| 246 |
isfinite. Returns 1 if x is finite and 0 if it is not. */ |
| 247 |
|
| 248 |
extern "C" int |
| 249 |
EXPORT(isfinite) (double x) |
| 250 |
{ |
| 251 |
#if defined(FP_NAN) && defined(FP_INFINITE) |
| 252 |
return (fpclassify (x) != FP_NAN && fpclassify (x) != FP_INFINITE); |
| 253 |
#else |
| 254 |
return false; |
| 255 |
#endif |
| 256 |
} |
| 257 |
|
| 258 |
/* isfinitel provide non builtin alternative to the gcc builtin |
| 259 |
isfinite. Returns 1 if x is finite and 0 if it is not. */ |
| 260 |
|
| 261 |
extern "C" int |
| 262 |
EXPORT(isfinitel) (long double x) |
| 263 |
{ |
| 264 |
#if defined(FP_NAN) && defined(FP_INFINITE) |
| 265 |
return (fpclassify (x) != FP_NAN && fpclassify (x) != FP_INFINITE); |
| 266 |
#else |
| 267 |
return false; |
| 268 |
#endif |
| 269 |
} |
| 270 |
|
| 271 |
/* isfinitef provide non builtin alternative to the gcc builtin |
| 272 |
isfinite. Returns 1 if x is finite and 0 if it is not. */ |
| 273 |
|
| 274 |
extern "C" int |
| 275 |
EXPORT(isfinitef) (float x) |
| 276 |
{ |
| 277 |
#if defined(FP_NAN) && defined(FP_INFINITE) |
| 278 |
return (fpclassify (x) != FP_NAN && fpclassify (x) != FP_INFINITE); |
| 279 |
#else |
| 280 |
return false; |
| 281 |
#endif |
| 282 |
} |
| 283 |
|
| 284 |
/* isnan - provide non builtin alternative to the gcc builtin isnan. |
| 285 |
Returns 1 if x is a NaN otherwise return 0. */ |
| 286 |
|
| 287 |
extern "C" int |
| 288 |
EXPORT(isnan) (double x) |
| 289 |
{ |
| 290 |
#if defined(FP_NAN) |
| 291 |
return fpclassify (x) == FP_NAN; |
| 292 |
#else |
| 293 |
return x != x; |
| 294 |
#endif |
| 295 |
} |
| 296 |
|
| 297 |
/* isnanf - provide non builtin alternative to the gcc builtin isnanf. |
| 298 |
Returns 1 if x is a NaN otherwise return 0. */ |
| 299 |
|
| 300 |
extern "C" int |
| 301 |
EXPORT(isnanf) (float x) |
| 302 |
{ |
| 303 |
#if defined(FP_NAN) |
| 304 |
return fpclassify (x) == FP_NAN; |
| 305 |
#else |
| 306 |
return x != x; |
| 307 |
#endif |
| 308 |
} |
| 309 |
|
| 310 |
/* isnanl - provide non builtin alternative to the gcc builtin isnanl. |
| 311 |
Returns 1 if x is a NaN otherwise return 0. */ |
| 312 |
|
| 313 |
extern "C" int |
| 314 |
EXPORT(isnanl) (long double x) |
| 315 |
{ |
| 316 |
#if defined(FP_NAN) |
| 317 |
return fpclassify (x) == FP_NAN; |
| 318 |
#else |
| 319 |
return x != x; |
| 320 |
#endif |
| 321 |
} |
| 322 |
|
| 323 |
/* SeekSet return the system libc SEEK_SET value. */ |
| 324 |
|
| 325 |
extern "C" int |
| 326 |
EXPORT(SeekSet) (void) |
| 327 |
{ |
| 328 |
return SEEK_SET; |
| 329 |
} |
| 330 |
|
| 331 |
/* SeekEnd return the system libc SEEK_END value. */ |
| 332 |
|
| 333 |
extern "C" int |
| 334 |
EXPORT(SeekEnd) (void) |
| 335 |
{ |
| 336 |
return SEEK_END; |
| 337 |
} |
| 338 |
|
| 339 |
/* ReadOnly return the system value of O_RDONLY. */ |
| 340 |
|
| 341 |
extern "C" int |
| 342 |
EXPORT(ReadOnly) (void) |
| 343 |
{ |
| 344 |
return O_RDONLY; |
| 345 |
} |
| 346 |
|
| 347 |
/* WriteOnly return the system value of O_WRONLY. */ |
| 348 |
|
| 349 |
extern "C" int |
| 350 |
EXPORT(WriteOnly) (void) |
| 351 |
{ |
| 352 |
return O_WRONLY; |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
/* GNU Modula-2 linking hooks. */ |
| 357 |
|
| 358 |
extern "C" void |
| 359 |
M2EXPORT(init) (int, char **, char **) |
| 360 |
{ |
| 361 |
} |
| 362 |
|
| 363 |
extern "C" void |
| 364 |
M2EXPORT(fini) (int, char **, char **) |
| 365 |
{ |
| 366 |
} |
| 367 |
|
| 368 |
extern "C" void |
| 369 |
M2EXPORT(dep) (void) |
| 370 |
{ |
| 371 |
} |
| 372 |
|
| 373 |
extern "C" void __attribute__((__constructor__)) |
| 374 |
M2EXPORT(ctor) (void) |
| 375 |
{ |
| 376 |
m2pim_M2RTS_RegisterModule ("wrapc", M2LIBNAME, |
| 377 |
M2EXPORT(init), M2EXPORT(fini), |
| 378 |
M2EXPORT(dep)); |
| 379 |
} |