]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libgm2/libm2pim/Selective.cc
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 #if defined(HAVE_STDDEF_H)
31 /* Obtain a definition for NULL. */
35 #if defined(HAVE_STDIO_H)
36 /* Obtain a definition for NULL. */
40 #if defined(HAVE_SYS_TIME_H)
44 #if defined(HAVE_TIME_H)
45 /* Obtain a definition for NULL. */
49 #if defined(HAVE_STRING_H)
50 /* Obtain a definition for NULL. */
54 #if defined(HAVE_WCHAR_H)
55 /* Obtain a definition for NULL. */
59 #if defined(HAVE_STDLIB_H)
60 /* Obtain a prototype for free and malloc. */
64 #if defined(HAVE_SYS_TYPES_H)
65 #include <sys/types.h>
68 #if defined(HAVE_UNISTD_H)
73 #define NULL (void *)0
76 #if defined(HAVE_SELECT)
77 #define FDSET_T fd_set
82 /* Select wrap a call to the C select. */
84 #if defined(HAVE_STRUCT_TIMEVAL)
86 Selective_Select (int nooffds
, fd_set
*readfds
, fd_set
*writefds
,
87 fd_set
*exceptfds
, struct timeval
*timeout
)
89 return select (nooffds
, readfds
, writefds
, exceptfds
, timeout
);
93 Selective_Select (int nooffds
, void *readfds
, void *writefds
, void *exceptfds
,
100 /* InitTime initializes a timeval structure and returns a pointer to it. */
102 #if defined(HAVE_STRUCT_TIMEVAL)
103 extern "C" struct timeval
*
104 Selective_InitTime (unsigned int sec
, unsigned int usec
)
106 struct timeval
*t
= (struct timeval
*)malloc (sizeof (struct timeval
));
108 t
->tv_sec
= (long int)sec
;
109 t
->tv_usec
= (long int)usec
;
114 Selective_GetTime (struct timeval
*t
, unsigned int *sec
, unsigned int *usec
)
116 *sec
= (unsigned int)t
->tv_sec
;
117 *usec
= (unsigned int)t
->tv_usec
;
121 Selective_SetTime (struct timeval
*t
, unsigned int sec
, unsigned int usec
)
127 /* KillTime frees the timeval structure and returns NULL. */
129 extern "C" struct timeval
*
130 Selective_KillTime (struct timeval
*t
)
132 #if defined(HAVE_STDLIB_H)
138 /* InitSet returns a pointer to a FD_SET. */
141 Selective_InitSet (void)
143 #if defined(HAVE_STDLIB_H)
144 FDSET_T
*s
= (FDSET_T
*)malloc (sizeof (FDSET_T
));
152 /* KillSet frees the FD_SET and returns NULL. */
155 Selective_KillSet (FDSET_T
*s
)
157 #if defined(HAVE_STDLIB_H)
163 /* FdZero generate an empty set. */
166 Selective_FdZero (FDSET_T
*s
)
171 /* FS_Set include an element, fd, into set, s. */
174 Selective_FdSet (int fd
, FDSET_T
*s
)
179 /* FdClr exclude an element, fd, from the set, s. */
182 Selective_FdClr (int fd
, FDSET_T
*s
)
187 /* FdIsSet return TRUE if, fd, is present in set, s. */
190 Selective_FdIsSet (int fd
, FDSET_T
*s
)
192 return FD_ISSET (fd
, s
);
195 /* GetTimeOfDay fills in a record, Timeval, filled in with the
196 current system time in seconds and microseconds.
197 It returns zero (see man 3p gettimeofday). */
200 Selective_GetTimeOfDay (struct timeval
*t
)
202 return gettimeofday (t
, NULL
);
207 Selective_InitTime (unsigned int sec
, unsigned int usec
)
213 Selective_KillTime (void *t
)
219 Selective_GetTime (void *t
, unsigned int *sec
, unsigned int *usec
)
224 Selective_SetTime (void *t
, unsigned int sec
, unsigned int usec
)
229 Selective_InitSet (void)
235 Selective_KillSet (void)
241 Selective_FdZero (void *s
)
246 Selective_FdSet (int fd
, void *s
)
251 Selective_FdClr (int fd
, void *s
)
256 Selective_FdIsSet (int fd
, void *s
)
262 Selective_GetTimeOfDay (void *t
)
268 /* MaxFdsPlusOne returns max (a + 1, b + 1). */
271 Selective_MaxFdsPlusOne (int a
, int b
)
279 /* WriteCharRaw writes a single character to the file descriptor. */
282 Selective_WriteCharRaw (int fd
, char ch
)
287 /* ReadCharRaw read and return a single char from file descriptor, fd. */
290 Selective_ReadCharRaw (int fd
)
299 _M2_Selective_init (int argc
, char *argv
[], char *envp
[])
304 _M2_Selective_fini (int argc
, char *argv
[], char *envp
[])
309 _M2_Selective_dep (void)
313 struct _M2_Selective_ctor
{ _M2_Selective_ctor (); } _M2_Selective_ctor
;
315 _M2_Selective_ctor::_M2_Selective_ctor (void)
317 M2RTS_RegisterModule ("Selective", _M2_Selective_init
, _M2_Selective_fini
,