1 /* Selective.c provide access to timeval and select.
3 Copyright (C) 2009-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 ## _Selective_ ## FUNC
31 #define M2EXPORT(FUNC) m2pim ## _M2_Selective_ ## FUNC
32 #define M2LIBNAME "m2pim"
34 #if defined(HAVE_STDDEF_H)
35 /* Obtain a definition for NULL. */
39 #if defined(HAVE_STDIO_H)
40 /* Obtain a definition for NULL. */
44 #if defined(HAVE_SYS_TIME_H)
48 #if defined(HAVE_TIME_H)
49 /* Obtain a definition for NULL. */
53 #if defined(HAVE_STRING_H)
54 /* Obtain a definition for NULL. */
58 #if defined(HAVE_WCHAR_H)
59 /* Obtain a definition for NULL. */
63 #if defined(HAVE_STDLIB_H)
64 /* Obtain a prototype for free and malloc. */
68 #if defined(HAVE_SYS_TYPES_H)
69 #include <sys/types.h>
72 #if defined(HAVE_UNISTD_H)
77 #define NULL (void *)0
80 #if defined(HAVE_SELECT)
81 #define FDSET_T fd_set
86 /* Select wrap a call to the C select. */
88 #if defined(HAVE_STRUCT_TIMEVAL)
90 EXPORT(Select
) (int nooffds
, fd_set
*readfds
, fd_set
*writefds
,
91 fd_set
*exceptfds
, struct timeval
*timeout
)
93 return select (nooffds
, readfds
, writefds
, exceptfds
, timeout
);
97 EXPORT(Select
) (int nooffds
, void *readfds
, void *writefds
, void *exceptfds
,
104 /* InitTime initializes a timeval structure and returns a pointer to it. */
106 #if defined(HAVE_STRUCT_TIMEVAL)
107 extern "C" struct timeval
*
108 EXPORT(InitTime
) (unsigned int sec
, unsigned int usec
)
110 struct timeval
*t
= (struct timeval
*)malloc (sizeof (struct timeval
));
112 t
->tv_sec
= (long int)sec
;
113 t
->tv_usec
= (long int)usec
;
118 EXPORT(GetTime
) (struct timeval
*t
, unsigned int *sec
, unsigned int *usec
)
120 *sec
= (unsigned int)t
->tv_sec
;
121 *usec
= (unsigned int)t
->tv_usec
;
125 EXPORT(SetTime
) (struct timeval
*t
, unsigned int sec
, unsigned int usec
)
131 /* KillTime frees the timeval structure and returns NULL. */
133 extern "C" struct timeval
*
134 EXPORT(KillTime
) (struct timeval
*t
)
136 #if defined(HAVE_STDLIB_H)
142 /* InitSet returns a pointer to a FD_SET. */
145 EXPORT(InitSet
) (void)
147 #if defined(HAVE_STDLIB_H)
148 FDSET_T
*s
= (FDSET_T
*)malloc (sizeof (FDSET_T
));
156 /* KillSet frees the FD_SET and returns NULL. */
159 EXPORT(KillSet
) (FDSET_T
*s
)
161 #if defined(HAVE_STDLIB_H)
167 /* FdZero generate an empty set. */
170 EXPORT(FdZero
) (FDSET_T
*s
)
175 /* FS_Set include an element, fd, into set, s. */
178 EXPORT(FdSet
) (int fd
, FDSET_T
*s
)
183 /* FdClr exclude an element, fd, from the set, s. */
186 EXPORT(FdClr
) (int fd
, FDSET_T
*s
)
191 /* FdIsSet return TRUE if, fd, is present in set, s. */
194 EXPORT(FdIsSet
) (int fd
, FDSET_T
*s
)
196 return FD_ISSET (fd
, s
);
199 /* GetTimeOfDay fills in a record, Timeval, filled in with the
200 current system time in seconds and microseconds.
201 It returns zero (see man 3p gettimeofday). */
204 EXPORT(GetTimeOfDay
) (struct timeval
*t
)
206 return gettimeofday (t
, NULL
);
211 EXPORT(InitTime
) (unsigned int sec
, unsigned int usec
)
217 EXPORT(KillTime
) (void *t
)
223 EXPORT(GetTime
) (void *t
, unsigned int *sec
, unsigned int *usec
)
228 EXPORT(SetTime
) (void *t
, unsigned int sec
, unsigned int usec
)
233 EXPORT(InitSet
) (void)
239 EXPORT(KillSet
) (void)
245 EXPORT(FdZero
) (void *s
)
250 EXPORT(FdSet
) (int fd
, void *s
)
255 EXPORT(FdClr
) (int fd
, void *s
)
260 EXPORT(FdIsSet
) (int fd
, void *s
)
266 EXPORT(GetTimeOfDay
) (void *t
)
272 /* MaxFdsPlusOne returns max (a + 1, b + 1). */
275 EXPORT(MaxFdsPlusOne
) (int a
, int b
)
283 /* WriteCharRaw writes a single character to the file descriptor. */
286 EXPORT(WriteCharRaw
) (int fd
, char ch
)
291 /* ReadCharRaw read and return a single char from file descriptor, fd. */
294 EXPORT(ReadCharRaw
) (int fd
)
302 /* GNU Modula-2 linking hooks. */
305 M2EXPORT(init
) (int, char **, char **)
310 M2EXPORT(fini
) (int, char **, char **)
319 extern "C" void __attribute__((__constructor__
))
320 M2EXPORT(ctor
) (void)
322 m2pim_M2RTS_RegisterModule ("Selective", M2LIBNAME
,
323 M2EXPORT(init
), M2EXPORT(fini
),