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 FALSE if one hasn't already been defined. */
65 #define FALSE (1 == 0)
68 /* Define a generic NULL if one hasn't already been defined. */
74 /* strtime returns the address of a string which describes the
78 EXPORT(strtime
) (void)
80 #if defined(HAVE_CTIME)
81 time_t clock
= time (NULL
);
82 char *string
= ctime (&clock
);
93 EXPORT(filesize
) (int f
, unsigned int *low
, unsigned int *high
)
95 #if defined(HAVE_SYS_STAT_H) && defined(HAVE_STRUCT_STAT)
97 int res
= fstat (f
, (struct stat
*)&s
);
101 *low
= (unsigned int)s
.st_size
;
102 *high
= (unsigned int)(s
.st_size
>> (sizeof (unsigned int) * 8));
110 /* filemtime returns the mtime of a file, f. */
113 EXPORT(filemtime
) (int f
)
115 #if defined(HAVE_SYS_STAT_H) && defined(HAVE_STRUCT_STAT)
118 if (fstat (f
, (struct stat
*)&s
) == 0)
127 /* fileinode returns the inode associated with a file, f. */
129 #if defined(HAVE_SYS_STAT_H) && defined(HAVE_STRUCT_STAT)
131 EXPORT(fileinode
) (int f
, unsigned int *low
, unsigned int *high
)
135 if (fstat (f
, (struct stat
*)&s
) == 0)
137 *low
= (unsigned int)s
.st_ino
;
138 if ((sizeof (s
.st_ino
) == (sizeof (unsigned int))))
141 *high
= (unsigned int)(s
.st_ino
>> (sizeof (unsigned int) * 8));
149 EXPORT(fileinode
) (int f
, unsigned int *low
, unsigned int *high
)
157 /* getrand returns a random number between 0..n-1. */
160 EXPORT(getrand
) (int n
)
165 #if defined(HAVE_PWD_H)
169 EXPORT(getusername
) (void)
171 return getpwuid (getuid ())->pw_gecos
;
174 /* getnameuidgid fills in the, uid, and, gid, which represents
178 EXPORT(getnameuidgid
) (char *name
, int *uid
, int *gid
)
180 struct passwd
*p
= getpwnam (name
);
195 EXPORT(getusername
) (void)
201 EXPORT(getnameuidgid
) (char *name
, int *uid
, int *gid
)
209 EXPORT(signbit
) (double r
)
211 #if defined(HAVE_SIGNBIT)
213 /* signbit is a macro which tests its argument against sizeof(float),
222 EXPORT(signbitl
) (long double r
)
224 #if defined(HAVE_SIGNBITL)
226 /* signbit is a macro which tests its argument against sizeof(float),
235 EXPORT(signbitf
) (float r
)
237 #if defined(HAVE_SIGNBITF)
239 /* signbit is a macro which tests its argument against sizeof(float),
247 /* isfinite provide non builtin alternative to the gcc builtin
248 isfinite. Returns 1 if x is finite and 0 if it is not. */
251 EXPORT(isfinite
) (double x
)
253 #if defined(FP_NAN) && defined(FP_INFINITE)
254 return (fpclassify (x
) != FP_NAN
&& fpclassify (x
) != FP_INFINITE
);
260 /* isfinitel provide non builtin alternative to the gcc builtin
261 isfinite. Returns 1 if x is finite and 0 if it is not. */
264 EXPORT(isfinitel
) (long double x
)
266 #if defined(FP_NAN) && defined(FP_INFINITE)
267 return (fpclassify (x
) != FP_NAN
&& fpclassify (x
) != FP_INFINITE
);
273 /* isfinitef provide non builtin alternative to the gcc builtin
274 isfinite. Returns 1 if x is finite and 0 if it is not. */
277 EXPORT(isfinitef
) (float x
)
279 #if defined(FP_NAN) && defined(FP_INFINITE)
280 return (fpclassify (x
) != FP_NAN
&& fpclassify (x
) != FP_INFINITE
);
286 /* GNU Modula-2 linking hooks. */
289 M2EXPORT(init
) (int, char **, char **)
294 M2EXPORT(fini
) (int, char **, char **)
303 extern "C" void __attribute__((__constructor__
))
304 M2EXPORT(ctor
) (void)
306 m2pim_M2RTS_RegisterModule ("wrapc", M2LIBNAME
,
307 M2EXPORT(init
), M2EXPORT(fini
),