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)
62 /* Define a generic NULL if one hasn't already been defined. */
68 /* strtime returns the address of a string which describes the
72 EXPORT(strtime
) (void)
74 #if defined(HAVE_CTIME)
75 time_t clock
= time (NULL
);
76 char *string
= ctime (&clock
);
87 EXPORT(filesize
) (int f
, unsigned int *low
, unsigned int *high
)
89 #if defined(HAVE_SYS_STAT_H) && defined(HAVE_STRUCT_STAT)
91 int res
= fstat (f
, (struct stat
*)&s
);
95 *low
= (unsigned int)s
.st_size
;
96 *high
= (unsigned int)(s
.st_size
>> (sizeof (unsigned int) * 8));
104 /* filemtime returns the mtime of a file, f. */
107 EXPORT(filemtime
) (int f
)
109 #if defined(HAVE_SYS_STAT_H) && defined(HAVE_STRUCT_STAT)
112 if (fstat (f
, (struct stat
*)&s
) == 0)
121 /* fileinode returns the inode associated with a file, f. */
123 #if defined(HAVE_SYS_STAT_H) && defined(HAVE_STRUCT_STAT)
125 EXPORT(fileinode
) (int f
, unsigned int *low
, unsigned int *high
)
129 if (fstat (f
, (struct stat
*)&s
) == 0)
131 *low
= (unsigned int)s
.st_ino
;
132 if ((sizeof (s
.st_ino
) == (sizeof (unsigned int))))
135 *high
= (unsigned int)(s
.st_ino
>> (sizeof (unsigned int) * 8));
143 EXPORT(fileinode
) (int f
, unsigned int *low
, unsigned int *high
)
151 /* getrand returns a random number between 0..n-1. */
154 EXPORT(getrand
) (int n
)
159 #if defined(HAVE_PWD_H)
163 EXPORT(getusername
) (void)
165 return getpwuid (getuid ())->pw_gecos
;
168 /* getnameuidgid fills in the, uid, and, gid, which represents
172 EXPORT(getnameuidgid
) (char *name
, int *uid
, int *gid
)
174 struct passwd
*p
= getpwnam (name
);
189 EXPORT(getusername
) (void)
195 EXPORT(getnameuidgid
) (char *name
, int *uid
, int *gid
)
203 EXPORT(signbit
) (double r
)
205 #if defined(HAVE_SIGNBIT)
207 /* signbit is a macro which tests its argument against sizeof(float),
216 EXPORT(signbitl
) (long double r
)
218 #if defined(HAVE_SIGNBITL)
220 /* signbit is a macro which tests its argument against sizeof(float),
229 EXPORT(signbitf
) (float r
)
231 #if defined(HAVE_SIGNBITF)
233 /* signbit is a macro which tests its argument against sizeof(float),
241 /* isfinite provide non builtin alternative to the gcc builtin
242 isfinite. Returns 1 if x is finite and 0 if it is not. */
245 EXPORT(isfinite
) (double x
)
247 #if defined(FP_NAN) && defined(FP_INFINITE)
248 return (fpclassify (x
) != FP_NAN
&& fpclassify (x
) != FP_INFINITE
);
254 /* isfinitel provide non builtin alternative to the gcc builtin
255 isfinite. Returns 1 if x is finite and 0 if it is not. */
258 EXPORT(isfinitel
) (long double x
)
260 #if defined(FP_NAN) && defined(FP_INFINITE)
261 return (fpclassify (x
) != FP_NAN
&& fpclassify (x
) != FP_INFINITE
);
267 /* isfinitef provide non builtin alternative to the gcc builtin
268 isfinite. Returns 1 if x is finite and 0 if it is not. */
271 EXPORT(isfinitef
) (float x
)
273 #if defined(FP_NAN) && defined(FP_INFINITE)
274 return (fpclassify (x
) != FP_NAN
&& fpclassify (x
) != FP_INFINITE
);
280 /* isnan - provide non builtin alternative to the gcc builtin isnan.
281 Returns 1 if x is a NaN otherwise return 0. */
284 EXPORT(isnan
) (double x
)
287 return fpclassify (x
) == FP_NAN
;
293 /* isnanf - provide non builtin alternative to the gcc builtin isnanf.
294 Returns 1 if x is a NaN otherwise return 0. */
297 EXPORT(isnanf
) (float x
)
300 return fpclassify (x
) == FP_NAN
;
306 /* isnanl - provide non builtin alternative to the gcc builtin isnanl.
307 Returns 1 if x is a NaN otherwise return 0. */
310 EXPORT(isnanl
) (long double x
)
313 return fpclassify (x
) == FP_NAN
;
319 /* GNU Modula-2 linking hooks. */
322 M2EXPORT(init
) (int, char **, char **)
327 M2EXPORT(fini
) (int, char **, char **)
336 extern "C" void __attribute__((__constructor__
))
337 M2EXPORT(ctor
) (void)
339 m2pim_M2RTS_RegisterModule ("wrapc", M2LIBNAME
,
340 M2EXPORT(init
), M2EXPORT(fini
),