c: UX improvements to 'too {few,many} arguments' errors (v5) [PR118112]
[gcc.git] / libgm2 / libm2pim / wrapc.cc
blob5c31f1e268705c453607a387696912a6a1a72b2d
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 #ifdef HAVE_FCNTL_H
63 #include <fcntl.h>
64 #endif
66 /* Define a generic NULL if one hasn't already been defined. */
68 #if !defined(NULL)
69 #define NULL 0
70 #endif
72 /* strtime returns the address of a string which describes the
73 local time. */
75 extern "C" char *
76 EXPORT(strtime) (void)
78 #if defined(HAVE_CTIME)
79 time_t clock = time (NULL);
80 char *string = ctime (&clock);
82 string[24] = (char)0;
84 return string;
85 #else
86 return "";
87 #endif
90 extern "C" int
91 EXPORT(filesize) (int f, unsigned int *low, unsigned int *high)
93 #if defined(HAVE_SYS_STAT_H) && defined(HAVE_STRUCT_STAT)
94 struct stat s;
95 int res = fstat (f, (struct stat *)&s);
97 if (res == 0)
99 *low = (unsigned int)s.st_size;
100 *high = (unsigned int)(s.st_size >> (sizeof (unsigned int) * 8));
102 return res;
103 #else
104 return -1;
105 #endif
108 /* filemtime returns the mtime of a file, f. */
110 extern "C" int
111 EXPORT(filemtime) (int f)
113 #if defined(HAVE_SYS_STAT_H) && defined(HAVE_STRUCT_STAT)
114 struct stat s;
116 if (fstat (f, (struct stat *)&s) == 0)
117 return s.st_mtime;
118 else
119 return -1;
120 #else
121 return -1;
122 #endif
125 /* fileinode returns the inode associated with a file, f. */
127 #if defined(HAVE_SYS_STAT_H) && defined(HAVE_STRUCT_STAT)
128 extern "C" ino_t
129 EXPORT(fileinode) (int f, unsigned int *low, unsigned int *high)
131 struct stat s;
133 if (fstat (f, (struct stat *)&s) == 0)
135 *low = (unsigned int)s.st_ino;
136 if ((sizeof (s.st_ino) == (sizeof (unsigned int))))
137 *high = 0;
138 else
139 *high = (unsigned int)(s.st_ino >> (sizeof (unsigned int) * 8));
140 return 0;
142 else
143 return -1;
145 #else
146 extern "C" int
147 EXPORT(fileinode) (int f, unsigned int *low, unsigned int *high)
149 *low = 0;
150 *high = 0;
151 return -1;
153 #endif
155 /* getrand returns a random number between 0..n-1. */
157 extern "C" int
158 EXPORT(getrand) (int n)
160 return rand () % n;
163 #if defined(HAVE_PWD_H)
164 #include <pwd.h>
166 extern "C" char *
167 EXPORT(getusername) (void)
169 return getpwuid (getuid ())->pw_gecos;
172 /* getnameuidgid fills in the, uid, and, gid, which represents
173 user, name. */
175 extern "C" void
176 EXPORT(getnameuidgid) (char *name, int *uid, int *gid)
178 struct passwd *p = getpwnam (name);
180 if (p == NULL)
182 *uid = -1;
183 *gid = -1;
185 else
187 *uid = p->pw_uid;
188 *gid = p->pw_gid;
191 #else
192 extern "C" char *
193 EXPORT(getusername) (void)
195 return "unknown";
198 extern "C" void
199 EXPORT(getnameuidgid) (char *name, int *uid, int *gid)
201 *uid = -1;
202 *gid = -1;
204 #endif
206 extern "C" int
207 EXPORT(signbit) (double r)
209 #if defined(HAVE_SIGNBIT)
211 /* signbit is a macro which tests its argument against sizeof(float),
212 sizeof(double). */
213 return signbit (r);
214 #else
215 return false;
216 #endif
219 extern "C" int
220 EXPORT(signbitl) (long double r)
222 #if defined(HAVE_SIGNBITL)
224 /* signbit is a macro which tests its argument against sizeof(float),
225 sizeof(double). */
226 return signbitl (r);
227 #else
228 return false;
229 #endif
232 extern "C" int
233 EXPORT(signbitf) (float r)
235 #if defined(HAVE_SIGNBITF)
237 /* signbit is a macro which tests its argument against sizeof(float),
238 sizeof(double). */
239 return signbitf (r);
240 #else
241 return false;
242 #endif
245 /* isfinite provide non builtin alternative to the gcc builtin
246 isfinite. Returns 1 if x is finite and 0 if it is not. */
248 extern "C" int
249 EXPORT(isfinite) (double x)
251 #if defined(FP_NAN) && defined(FP_INFINITE)
252 return (fpclassify (x) != FP_NAN && fpclassify (x) != FP_INFINITE);
253 #else
254 return false;
255 #endif
258 /* isfinitel provide non builtin alternative to the gcc builtin
259 isfinite. Returns 1 if x is finite and 0 if it is not. */
261 extern "C" int
262 EXPORT(isfinitel) (long double x)
264 #if defined(FP_NAN) && defined(FP_INFINITE)
265 return (fpclassify (x) != FP_NAN && fpclassify (x) != FP_INFINITE);
266 #else
267 return false;
268 #endif
271 /* isfinitef provide non builtin alternative to the gcc builtin
272 isfinite. Returns 1 if x is finite and 0 if it is not. */
274 extern "C" int
275 EXPORT(isfinitef) (float x)
277 #if defined(FP_NAN) && defined(FP_INFINITE)
278 return (fpclassify (x) != FP_NAN && fpclassify (x) != FP_INFINITE);
279 #else
280 return false;
281 #endif
284 /* isnan - provide non builtin alternative to the gcc builtin isnan.
285 Returns 1 if x is a NaN otherwise return 0. */
287 extern "C" int
288 EXPORT(isnan) (double x)
290 #if defined(FP_NAN)
291 return fpclassify (x) == FP_NAN;
292 #else
293 return x != x;
294 #endif
297 /* isnanf - provide non builtin alternative to the gcc builtin isnanf.
298 Returns 1 if x is a NaN otherwise return 0. */
300 extern "C" int
301 EXPORT(isnanf) (float x)
303 #if defined(FP_NAN)
304 return fpclassify (x) == FP_NAN;
305 #else
306 return x != x;
307 #endif
310 /* isnanl - provide non builtin alternative to the gcc builtin isnanl.
311 Returns 1 if x is a NaN otherwise return 0. */
313 extern "C" int
314 EXPORT(isnanl) (long double x)
316 #if defined(FP_NAN)
317 return fpclassify (x) == FP_NAN;
318 #else
319 return x != x;
320 #endif
323 /* SeekSet return the system libc SEEK_SET value. */
325 extern "C" int
326 EXPORT(SeekSet) (void)
328 return SEEK_SET;
331 /* SeekEnd return the system libc SEEK_END value. */
333 extern "C" int
334 EXPORT(SeekEnd) (void)
336 return SEEK_END;
339 /* ReadOnly return the system value of O_RDONLY. */
341 extern "C" int
342 EXPORT(ReadOnly) (void)
344 return O_RDONLY;
347 /* WriteOnly return the system value of O_WRONLY. */
349 extern "C" int
350 EXPORT(WriteOnly) (void)
352 return O_WRONLY;
356 /* GNU Modula-2 linking hooks. */
358 extern "C" void
359 M2EXPORT(init) (int, char **, char **)
363 extern "C" void
364 M2EXPORT(fini) (int, char **, char **)
368 extern "C" void
369 M2EXPORT(dep) (void)
373 extern "C" void __attribute__((__constructor__))
374 M2EXPORT(ctor) (void)
376 m2pim_M2RTS_RegisterModule ("wrapc", M2LIBNAME,
377 M2EXPORT(init), M2EXPORT(fini),
378 M2EXPORT(dep));