rust: Implement TARGET_RUST_OS_INFO for *-*-freebsd*
[official-gcc.git] / libgm2 / libm2pim / wrapc.cc
blobc24d214649edce8343f6e761592830def34018a3
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)
11 any later version.
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/>. */
27 #include <config.h>
28 #include <m2rts.h>
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)
35 #include <math.h>
36 #endif
38 #if defined(HAVE_STDLIB_H)
39 #include <stdlib.h>
40 #endif
42 #if defined(HAVE_UNISTD_H)
43 #include <unistd.h>
44 #endif
46 #if defined(HAVE_SYS_STAT_H)
47 #include <sys/stat.h>
48 #endif
50 #ifdef HAVE_STDIO_H
51 #include <stdio.h>
52 #endif
54 #if defined(HAVE_SYS_TYPES_H)
55 #include <sys/types.h>
56 #endif
58 #if defined(HAVE_TIME_H)
59 #include <time.h>
60 #endif
62 /* Define a generic NULL if one hasn't already been defined. */
64 #if !defined(NULL)
65 #define NULL 0
66 #endif
68 /* strtime returns the address of a string which describes the
69 local time. */
71 extern "C" char *
72 EXPORT(strtime) (void)
74 #if defined(HAVE_CTIME)
75 time_t clock = time (NULL);
76 char *string = ctime (&clock);
78 string[24] = (char)0;
80 return string;
81 #else
82 return "";
83 #endif
86 extern "C" int
87 EXPORT(filesize) (int f, unsigned int *low, unsigned int *high)
89 #if defined(HAVE_SYS_STAT_H) && defined(HAVE_STRUCT_STAT)
90 struct stat s;
91 int res = fstat (f, (struct stat *)&s);
93 if (res == 0)
95 *low = (unsigned int)s.st_size;
96 *high = (unsigned int)(s.st_size >> (sizeof (unsigned int) * 8));
98 return res;
99 #else
100 return -1;
101 #endif
104 /* filemtime returns the mtime of a file, f. */
106 extern "C" int
107 EXPORT(filemtime) (int f)
109 #if defined(HAVE_SYS_STAT_H) && defined(HAVE_STRUCT_STAT)
110 struct stat s;
112 if (fstat (f, (struct stat *)&s) == 0)
113 return s.st_mtime;
114 else
115 return -1;
116 #else
117 return -1;
118 #endif
121 /* fileinode returns the inode associated with a file, f. */
123 #if defined(HAVE_SYS_STAT_H) && defined(HAVE_STRUCT_STAT)
124 extern "C" ino_t
125 EXPORT(fileinode) (int f, unsigned int *low, unsigned int *high)
127 struct stat s;
129 if (fstat (f, (struct stat *)&s) == 0)
131 *low = (unsigned int)s.st_ino;
132 if ((sizeof (s.st_ino) == (sizeof (unsigned int))))
133 *high = 0;
134 else
135 *high = (unsigned int)(s.st_ino >> (sizeof (unsigned int) * 8));
136 return 0;
138 else
139 return -1;
141 #else
142 extern "C" int
143 EXPORT(fileinode) (int f, unsigned int *low, unsigned int *high)
145 *low = 0;
146 *high = 0;
147 return -1;
149 #endif
151 /* getrand returns a random number between 0..n-1. */
153 extern "C" int
154 EXPORT(getrand) (int n)
156 return rand () % n;
159 #if defined(HAVE_PWD_H)
160 #include <pwd.h>
162 extern "C" char *
163 EXPORT(getusername) (void)
165 return getpwuid (getuid ())->pw_gecos;
168 /* getnameuidgid fills in the, uid, and, gid, which represents
169 user, name. */
171 extern "C" void
172 EXPORT(getnameuidgid) (char *name, int *uid, int *gid)
174 struct passwd *p = getpwnam (name);
176 if (p == NULL)
178 *uid = -1;
179 *gid = -1;
181 else
183 *uid = p->pw_uid;
184 *gid = p->pw_gid;
187 #else
188 extern "C" char *
189 EXPORT(getusername) (void)
191 return "unknown";
194 extern "C" void
195 EXPORT(getnameuidgid) (char *name, int *uid, int *gid)
197 *uid = -1;
198 *gid = -1;
200 #endif
202 extern "C" int
203 EXPORT(signbit) (double r)
205 #if defined(HAVE_SIGNBIT)
207 /* signbit is a macro which tests its argument against sizeof(float),
208 sizeof(double). */
209 return signbit (r);
210 #else
211 return false;
212 #endif
215 extern "C" int
216 EXPORT(signbitl) (long double r)
218 #if defined(HAVE_SIGNBITL)
220 /* signbit is a macro which tests its argument against sizeof(float),
221 sizeof(double). */
222 return signbitl (r);
223 #else
224 return false;
225 #endif
228 extern "C" int
229 EXPORT(signbitf) (float r)
231 #if defined(HAVE_SIGNBITF)
233 /* signbit is a macro which tests its argument against sizeof(float),
234 sizeof(double). */
235 return signbitf (r);
236 #else
237 return false;
238 #endif
241 /* isfinite provide non builtin alternative to the gcc builtin
242 isfinite. Returns 1 if x is finite and 0 if it is not. */
244 extern "C" int
245 EXPORT(isfinite) (double x)
247 #if defined(FP_NAN) && defined(FP_INFINITE)
248 return (fpclassify (x) != FP_NAN && fpclassify (x) != FP_INFINITE);
249 #else
250 return false;
251 #endif
254 /* isfinitel provide non builtin alternative to the gcc builtin
255 isfinite. Returns 1 if x is finite and 0 if it is not. */
257 extern "C" int
258 EXPORT(isfinitel) (long double x)
260 #if defined(FP_NAN) && defined(FP_INFINITE)
261 return (fpclassify (x) != FP_NAN && fpclassify (x) != FP_INFINITE);
262 #else
263 return false;
264 #endif
267 /* isfinitef provide non builtin alternative to the gcc builtin
268 isfinite. Returns 1 if x is finite and 0 if it is not. */
270 extern "C" int
271 EXPORT(isfinitef) (float x)
273 #if defined(FP_NAN) && defined(FP_INFINITE)
274 return (fpclassify (x) != FP_NAN && fpclassify (x) != FP_INFINITE);
275 #else
276 return false;
277 #endif
280 /* GNU Modula-2 linking hooks. */
282 extern "C" void
283 M2EXPORT(init) (int, char **, char **)
287 extern "C" void
288 M2EXPORT(fini) (int, char **, char **)
292 extern "C" void
293 M2EXPORT(dep) (void)
297 extern "C" void __attribute__((__constructor__))
298 M2EXPORT(ctor) (void)
300 m2pim_M2RTS_RegisterModule ("wrapc", M2LIBNAME,
301 M2EXPORT(init), M2EXPORT(fini),
302 M2EXPORT(dep));