200x-xx-xx Jakub Jelinek <jakub@redhat.com> Richard Sandiford <rsandifo@nildram.co.uk>
[official-gcc.git] / libgfortran / intrinsics / signal.c
blob27d6222cf4e752d93a3569e728c006c7d5bbc450
1 /* Implementation of the SIGNAL and ALARM g77 intrinsics
2 Copyright (C) 2005, 2007 Free Software Foundation, Inc.
3 Contributed by François-Xavier Coudert <coudert@clipper.ens.fr>
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
19 executable.)
21 Libgfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public
27 License along with libgfortran; see the file COPYING. If not,
28 write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA. */
31 #include "libgfortran.h"
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
37 #ifdef HAVE_SIGNAL_H
38 #include <signal.h>
39 #endif
41 #ifdef HAVE_INTTYPES_H
42 #include <inttypes.h>
43 #endif
45 #include <errno.h>
47 #ifdef HAVE_INTPTR_T
48 # define INTPTR_T intptr_t
49 #else
50 # define INTPTR_T int
51 #endif
53 /* SIGNAL subroutine with PROCEDURE as handler */
54 extern void signal_sub (int *, void (*)(int), int *);
55 iexport_proto(signal_sub);
57 void
58 signal_sub (int *number, void (*handler)(int), int *status)
60 #ifdef HAVE_SIGNAL
61 INTPTR_T ret;
63 if (status != NULL)
65 ret = (INTPTR_T) signal (*number, handler);
66 *status = (int) ret;
68 else
69 signal (*number, handler);
70 #else
71 errno = ENOSYS;
72 if (status != NULL)
73 *status = -1;
74 #endif
76 iexport(signal_sub);
79 /* SIGNAL subroutine with INTEGER as handler */
80 extern void signal_sub_int (int *, int *, int *);
81 iexport_proto(signal_sub_int);
83 void
84 signal_sub_int (int *number, int *handler, int *status)
86 #ifdef HAVE_SIGNAL
87 INTPTR_T ptr = *handler, ret;
89 if (status != NULL)
91 ret = (INTPTR_T) signal (*number, (void (*)(int)) ptr);
92 *status = (int) ret;
94 else
95 signal (*number, (void (*)(int)) ptr);
96 #else
97 errno = ENOSYS;
98 if (status != NULL)
99 *status = -1;
100 #endif
102 iexport(signal_sub_int);
105 /* SIGNAL function with PROCEDURE as handler */
106 extern int signal_func (int *, void (*)(int));
107 iexport_proto(signal_func);
110 signal_func (int *number, void (*handler)(int))
112 int status;
113 signal_sub (number, handler, &status);
114 return status;
116 iexport(signal_func);
119 /* SIGNAL function with INTEGER as handler */
120 extern int signal_func_int (int *, int *);
121 iexport_proto(signal_func_int);
124 signal_func_int (int *number, int *handler)
126 int status;
127 signal_sub_int (number, handler, &status);
128 return status;
130 iexport(signal_func_int);
134 /* ALARM intrinsic with PROCEDURE as handler */
135 extern void alarm_sub_i4 (int *, void (*)(int), GFC_INTEGER_4 *);
136 iexport_proto(alarm_sub_i4);
138 void
139 alarm_sub_i4 (int * seconds __attribute__ ((unused)),
140 void (*handler)(int) __attribute__ ((unused)),
141 GFC_INTEGER_4 *status)
143 #if defined (SIGALRM) && defined (HAVE_ALARM) && defined (HAVE_SIGNAL)
144 if (status != NULL)
146 if (signal (SIGALRM, handler) == SIG_ERR)
147 *status = -1;
148 else
149 *status = alarm (*seconds);
151 else
153 signal (SIGALRM, handler);
154 alarm (*seconds);
156 #else
157 errno = ENOSYS;
158 if (status != NULL)
159 *status = -1;
160 #endif
162 iexport(alarm_sub_i4);
165 extern void alarm_sub_i8 (int *, void (*)(int), GFC_INTEGER_8 *);
166 iexport_proto(alarm_sub_i8);
168 void
169 alarm_sub_i8 (int *seconds __attribute__ ((unused)),
170 void (*handler)(int) __attribute__ ((unused)),
171 GFC_INTEGER_8 *status)
173 #if defined (SIGALRM) && defined (HAVE_ALARM) && defined (HAVE_SIGNAL)
174 if (status != NULL)
176 if (signal (SIGALRM, handler) == SIG_ERR)
177 *status = -1;
178 else
179 *status = alarm (*seconds);
181 else
183 signal (SIGALRM, handler);
184 alarm (*seconds);
186 #else
187 errno = ENOSYS;
188 if (status != NULL)
189 *status = -1;
190 #endif
192 iexport(alarm_sub_i8);
195 /* ALARM intrinsic with INTEGER as handler */
196 extern void alarm_sub_int_i4 (int *, int *, GFC_INTEGER_4 *);
197 iexport_proto(alarm_sub_int_i4);
199 void
200 alarm_sub_int_i4 (int *seconds __attribute__ ((unused)),
201 int *handler __attribute__ ((unused)),
202 GFC_INTEGER_4 *status)
204 #if defined (SIGALRM) && defined (HAVE_ALARM) && defined (HAVE_SIGNAL)
205 if (status != NULL)
207 if (signal (SIGALRM, (void (*)(int)) (INTPTR_T) *handler) == SIG_ERR)
208 *status = -1;
209 else
210 *status = alarm (*seconds);
212 else
214 signal (SIGALRM, (void (*)(int)) (INTPTR_T) *handler);
215 alarm (*seconds);
217 #else
218 errno = ENOSYS;
219 if (status != NULL)
220 *status = -1;
221 #endif
223 iexport(alarm_sub_int_i4);
226 extern void alarm_sub_int_i8 (int *, int *, GFC_INTEGER_8 *);
227 iexport_proto(alarm_sub_int_i8);
229 void
230 alarm_sub_int_i8 (int *seconds __attribute__ ((unused)),
231 int *handler __attribute__ ((unused)),
232 GFC_INTEGER_8 *status)
234 #if defined (SIGALRM) && defined (HAVE_ALARM) && defined (HAVE_SIGNAL)
235 if (status != NULL)
237 if (signal (SIGALRM, (void (*)(int)) (INTPTR_T) *handler) == SIG_ERR)
238 *status = -1;
239 else
240 *status = alarm (*seconds);
242 else
244 signal (SIGALRM, (void (*)(int)) (INTPTR_T) *handler);
245 alarm (*seconds);
247 #else
248 errno = ENOSYS;
249 if (status != NULL)
250 *status = -1;
251 #endif
253 iexport(alarm_sub_int_i8);