Skip gnat.dg/prot7.adb on hppa.
[official-gcc.git] / libgm2 / libm2pim / wrapc.cc
blob0412cccb70bb497194423518b9bc23a9b5636151
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 FALSE if one hasn't already been defined. */
64 #if !defined(FALSE)
65 #define FALSE (1 == 0)
66 #endif
68 /* Define a generic NULL if one hasn't already been defined. */
70 #if !defined(NULL)
71 #define NULL 0
72 #endif
74 /* strtime returns the address of a string which describes the
75 local time. */
77 extern "C" char *
78 EXPORT(strtime) (void)
80 #if defined(HAVE_CTIME)
81 time_t clock = time (NULL);
82 char *string = ctime (&clock);
84 string[24] = (char)0;
86 return string;
87 #else
88 return "";
89 #endif
92 extern "C" int
93 EXPORT(filesize) (int f, unsigned int *low, unsigned int *high)
95 #if defined(HAVE_SYS_STAT_H) && defined(HAVE_STRUCT_STAT)
96 struct stat s;
97 int res = fstat (f, (struct stat *)&s);
99 if (res == 0)
101 *low = (unsigned int)s.st_size;
102 *high = (unsigned int)(s.st_size >> (sizeof (unsigned int) * 8));
104 return res;
105 #else
106 return -1;
107 #endif
110 /* filemtime returns the mtime of a file, f. */
112 extern "C" int
113 EXPORT(filemtime) (int f)
115 #if defined(HAVE_SYS_STAT_H) && defined(HAVE_STRUCT_STAT)
116 struct stat s;
118 if (fstat (f, (struct stat *)&s) == 0)
119 return s.st_mtime;
120 else
121 return -1;
122 #else
123 return -1;
124 #endif
127 /* fileinode returns the inode associated with a file, f. */
129 #if defined(HAVE_SYS_STAT_H) && defined(HAVE_STRUCT_STAT)
130 extern "C" ino_t
131 EXPORT(fileinode) (int f, unsigned int *low, unsigned int *high)
133 struct stat s;
135 if (fstat (f, (struct stat *)&s) == 0)
137 *low = (unsigned int)s.st_ino;
138 if ((sizeof (s.st_ino) == (sizeof (unsigned int))))
139 *high = 0;
140 else
141 *high = (unsigned int)(s.st_ino >> (sizeof (unsigned int) * 8));
142 return 0;
144 else
145 return -1;
147 #else
148 extern "C" int
149 EXPORT(fileinode) (int f, unsigned int *low, unsigned int *high)
151 *low = 0;
152 *high = 0;
153 return -1;
155 #endif
157 /* getrand returns a random number between 0..n-1. */
159 extern "C" int
160 EXPORT(getrand) (int n)
162 return rand () % n;
165 #if defined(HAVE_PWD_H)
166 #include <pwd.h>
168 extern "C" char *
169 EXPORT(getusername) (void)
171 return getpwuid (getuid ())->pw_gecos;
174 /* getnameuidgid fills in the, uid, and, gid, which represents
175 user, name. */
177 extern "C" void
178 EXPORT(getnameuidgid) (char *name, int *uid, int *gid)
180 struct passwd *p = getpwnam (name);
182 if (p == NULL)
184 *uid = -1;
185 *gid = -1;
187 else
189 *uid = p->pw_uid;
190 *gid = p->pw_gid;
193 #else
194 extern "C" char *
195 EXPORT(getusername) (void)
197 return "unknown";
200 extern "C" void
201 EXPORT(getnameuidgid) (char *name, int *uid, int *gid)
203 *uid = -1;
204 *gid = -1;
206 #endif
208 extern "C" int
209 EXPORT(signbit) (double r)
211 #if defined(HAVE_SIGNBIT)
213 /* signbit is a macro which tests its argument against sizeof(float),
214 sizeof(double). */
215 return signbit (r);
216 #else
217 return FALSE;
218 #endif
221 extern "C" int
222 EXPORT(signbitl) (long double r)
224 #if defined(HAVE_SIGNBITL)
226 /* signbit is a macro which tests its argument against sizeof(float),
227 sizeof(double). */
228 return signbitl (r);
229 #else
230 return FALSE;
231 #endif
234 extern "C" int
235 EXPORT(signbitf) (float r)
237 #if defined(HAVE_SIGNBITF)
239 /* signbit is a macro which tests its argument against sizeof(float),
240 sizeof(double). */
241 return signbitf (r);
242 #else
243 return FALSE;
244 #endif
247 /* isfinite provide non builtin alternative to the gcc builtin
248 isfinite. Returns 1 if x is finite and 0 if it is not. */
250 extern "C" int
251 EXPORT(isfinite) (double x)
253 #if defined(FP_NAN) && defined(FP_INFINITE)
254 return (fpclassify (x) != FP_NAN && fpclassify (x) != FP_INFINITE);
255 #else
256 return FALSE;
257 #endif
260 /* isfinitel provide non builtin alternative to the gcc builtin
261 isfinite. Returns 1 if x is finite and 0 if it is not. */
263 extern "C" int
264 EXPORT(isfinitel) (long double x)
266 #if defined(FP_NAN) && defined(FP_INFINITE)
267 return (fpclassify (x) != FP_NAN && fpclassify (x) != FP_INFINITE);
268 #else
269 return FALSE;
270 #endif
273 /* isfinitef provide non builtin alternative to the gcc builtin
274 isfinite. Returns 1 if x is finite and 0 if it is not. */
276 extern "C" int
277 EXPORT(isfinitef) (float x)
279 #if defined(FP_NAN) && defined(FP_INFINITE)
280 return (fpclassify (x) != FP_NAN && fpclassify (x) != FP_INFINITE);
281 #else
282 return FALSE;
283 #endif
286 /* GNU Modula-2 linking hooks. */
288 extern "C" void
289 M2EXPORT(init) (int, char **, char **)
293 extern "C" void
294 M2EXPORT(fini) (int, char **, char **)
298 extern "C" void
299 M2EXPORT(dep) (void)
303 extern "C" void __attribute__((__constructor__))
304 M2EXPORT(ctor) (void)
306 m2pim_M2RTS_RegisterModule ("wrapc", M2LIBNAME,
307 M2EXPORT(init), M2EXPORT(fini),
308 M2EXPORT(dep));