1 /* wrapc.c provide access to miscellaneous C library functions.
3 Copyright (C) 2005-2022 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
6 This file is part of GNU Modula-2.
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)
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.
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.
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/>. */
30 #define EXPORT(FUNC) m2pim ## _wrapc_ ## FUNC
31 #define M2EXPORT(FUNC) m2pim ## _M2_wrapc_ ## FUNC
32 #define M2LIBNAME "m2pim"
34 #if defined(HAVE_MATH_H)
38 #if defined(HAVE_STDLIB_H)
42 #if defined(HAVE_UNISTD_H)
46 #if defined(HAVE_SYS_STAT_H)
54 #if defined(HAVE_SYS_TYPES_H)
55 #include <sys/types.h>
58 #if defined(HAVE_TIME_H)
66 /* Define a generic NULL if one hasn't already been defined. */
72 /* strtime returns the address of a string which describes the
76 EXPORT(strtime
) (void)
78 #if defined(HAVE_CTIME)
79 time_t clock
= time (NULL
);
80 char *string
= ctime (&clock
);
91 EXPORT(filesize
) (int f
, unsigned int *low
, unsigned int *high
)
93 #if defined(HAVE_SYS_STAT_H) && defined(HAVE_STRUCT_STAT)
95 int res
= fstat (f
, (struct stat
*)&s
);
99 *low
= (unsigned int)s
.st_size
;
100 *high
= (unsigned int)(s
.st_size
>> (sizeof (unsigned int) * 8));
108 /* filemtime returns the mtime of a file, f. */
111 EXPORT(filemtime
) (int f
)
113 #if defined(HAVE_SYS_STAT_H) && defined(HAVE_STRUCT_STAT)
116 if (fstat (f
, (struct stat
*)&s
) == 0)
125 /* fileinode returns the inode associated with a file, f. */
127 #if defined(HAVE_SYS_STAT_H) && defined(HAVE_STRUCT_STAT)
129 EXPORT(fileinode
) (int f
, unsigned int *low
, unsigned int *high
)
133 if (fstat (f
, (struct stat
*)&s
) == 0)
135 *low
= (unsigned int)s
.st_ino
;
136 if ((sizeof (s
.st_ino
) == (sizeof (unsigned int))))
139 *high
= (unsigned int)(s
.st_ino
>> (sizeof (unsigned int) * 8));
147 EXPORT(fileinode
) (int f
, unsigned int *low
, unsigned int *high
)
155 /* getrand returns a random number between 0..n-1. */
158 EXPORT(getrand
) (int n
)
163 #if defined(HAVE_PWD_H)
167 EXPORT(getusername
) (void)
169 return getpwuid (getuid ())->pw_gecos
;
172 /* getnameuidgid fills in the, uid, and, gid, which represents
176 EXPORT(getnameuidgid
) (char *name
, int *uid
, int *gid
)
178 struct passwd
*p
= getpwnam (name
);
193 EXPORT(getusername
) (void)
199 EXPORT(getnameuidgid
) (char *name
, int *uid
, int *gid
)
207 EXPORT(signbit
) (double r
)
209 #if defined(HAVE_SIGNBIT)
211 /* signbit is a macro which tests its argument against sizeof(float),
220 EXPORT(signbitl
) (long double r
)
222 #if defined(HAVE_SIGNBITL)
224 /* signbit is a macro which tests its argument against sizeof(float),
233 EXPORT(signbitf
) (float r
)
235 #if defined(HAVE_SIGNBITF)
237 /* signbit is a macro which tests its argument against sizeof(float),
245 /* isfinite provide non builtin alternative to the gcc builtin
246 isfinite. Returns 1 if x is finite and 0 if it is not. */
249 EXPORT(isfinite
) (double x
)
251 #if defined(FP_NAN) && defined(FP_INFINITE)
252 return (fpclassify (x
) != FP_NAN
&& fpclassify (x
) != FP_INFINITE
);
258 /* isfinitel provide non builtin alternative to the gcc builtin
259 isfinite. Returns 1 if x is finite and 0 if it is not. */
262 EXPORT(isfinitel
) (long double x
)
264 #if defined(FP_NAN) && defined(FP_INFINITE)
265 return (fpclassify (x
) != FP_NAN
&& fpclassify (x
) != FP_INFINITE
);
271 /* isfinitef provide non builtin alternative to the gcc builtin
272 isfinite. Returns 1 if x is finite and 0 if it is not. */
275 EXPORT(isfinitef
) (float x
)
277 #if defined(FP_NAN) && defined(FP_INFINITE)
278 return (fpclassify (x
) != FP_NAN
&& fpclassify (x
) != FP_INFINITE
);
284 /* isnan - provide non builtin alternative to the gcc builtin isnan.
285 Returns 1 if x is a NaN otherwise return 0. */
288 EXPORT(isnan
) (double x
)
291 return fpclassify (x
) == FP_NAN
;
297 /* isnanf - provide non builtin alternative to the gcc builtin isnanf.
298 Returns 1 if x is a NaN otherwise return 0. */
301 EXPORT(isnanf
) (float x
)
304 return fpclassify (x
) == FP_NAN
;
310 /* isnanl - provide non builtin alternative to the gcc builtin isnanl.
311 Returns 1 if x is a NaN otherwise return 0. */
314 EXPORT(isnanl
) (long double x
)
317 return fpclassify (x
) == FP_NAN
;
323 /* SeekSet return the system libc SEEK_SET value. */
326 EXPORT(SeekSet
) (void)
331 /* SeekEnd return the system libc SEEK_END value. */
334 EXPORT(SeekEnd
) (void)
339 /* ReadOnly return the system value of O_RDONLY. */
342 EXPORT(ReadOnly
) (void)
347 /* WriteOnly return the system value of O_WRONLY. */
350 EXPORT(WriteOnly
) (void)
356 /* GNU Modula-2 linking hooks. */
359 M2EXPORT(init
) (int, char **, char **)
364 M2EXPORT(fini
) (int, char **, char **)
373 extern "C" void __attribute__((__constructor__
))
374 M2EXPORT(ctor
) (void)
376 m2pim_M2RTS_RegisterModule ("wrapc", M2LIBNAME
,
377 M2EXPORT(init
), M2EXPORT(fini
),