2018-03-08 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libgfortran / intrinsics / signal.c
blob365630d0e4532f7a2ad6d3f322eba0d1135451ca
1 /* Implementation of the SIGNAL and ALARM g77 intrinsics
2 Copyright (C) 2005-2018 Free Software Foundation, Inc.
3 Contributed by François-Xavier Coudert <coudert@clipper.ens.fr>
5 This file is part of the GNU Fortran 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 3 of the License, or (at your option) any later version.
12 Libgfortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 #include "libgfortran.h"
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
32 #include <signal.h>
34 #ifdef HAVE_INTTYPES_H
35 #include <inttypes.h>
36 #endif
38 #include <errno.h>
40 /* SIGNAL subroutine with PROCEDURE as handler */
41 extern void signal_sub (int *, void (*)(int), int *);
42 iexport_proto(signal_sub);
44 void
45 signal_sub (int *number, void (*handler)(int), int *status)
47 intptr_t ret;
49 if (status != NULL)
51 ret = (intptr_t) signal (*number, handler);
52 *status = (int) ret;
54 else
55 signal (*number, handler);
57 iexport(signal_sub);
60 /* SIGNAL subroutine with INTEGER as handler */
61 extern void signal_sub_int (int *, int *, int *);
62 iexport_proto(signal_sub_int);
64 void
65 signal_sub_int (int *number, int *handler, int *status)
67 intptr_t ptr = *handler, ret;
69 if (status != NULL)
71 ret = (intptr_t) signal (*number, (void (*)(int)) ptr);
72 *status = (int) ret;
74 else
75 signal (*number, (void (*)(int)) ptr);
77 iexport(signal_sub_int);
80 /* SIGNAL function with PROCEDURE as handler */
81 extern int signal_func (int *, void (*)(int));
82 iexport_proto(signal_func);
84 int
85 signal_func (int *number, void (*handler)(int))
87 int status;
88 signal_sub (number, handler, &status);
89 return status;
91 iexport(signal_func);
94 /* SIGNAL function with INTEGER as handler */
95 extern int signal_func_int (int *, int *);
96 iexport_proto(signal_func_int);
98 int
99 signal_func_int (int *number, int *handler)
101 int status;
102 signal_sub_int (number, handler, &status);
103 return status;
105 iexport(signal_func_int);
109 /* ALARM intrinsic with PROCEDURE as handler */
110 extern void alarm_sub_i4 (int *, void (*)(int), GFC_INTEGER_4 *);
111 iexport_proto(alarm_sub_i4);
113 void
114 alarm_sub_i4 (int * seconds __attribute__ ((unused)),
115 void (*handler)(int) __attribute__ ((unused)),
116 GFC_INTEGER_4 *status)
118 #if defined (SIGALRM) && defined (HAVE_ALARM)
119 if (status != NULL)
121 if (signal (SIGALRM, handler) == SIG_ERR)
122 *status = -1;
123 else
124 *status = alarm (*seconds);
126 else
128 signal (SIGALRM, handler);
129 alarm (*seconds);
131 #else
132 errno = ENOSYS;
133 if (status != NULL)
134 *status = -1;
135 #endif
137 iexport(alarm_sub_i4);
140 extern void alarm_sub_i8 (int *, void (*)(int), GFC_INTEGER_8 *);
141 iexport_proto(alarm_sub_i8);
143 void
144 alarm_sub_i8 (int *seconds __attribute__ ((unused)),
145 void (*handler)(int) __attribute__ ((unused)),
146 GFC_INTEGER_8 *status)
148 #if defined (SIGALRM) && defined (HAVE_ALARM)
149 if (status != NULL)
151 if (signal (SIGALRM, handler) == SIG_ERR)
152 *status = -1;
153 else
154 *status = alarm (*seconds);
156 else
158 signal (SIGALRM, handler);
159 alarm (*seconds);
161 #else
162 errno = ENOSYS;
163 if (status != NULL)
164 *status = -1;
165 #endif
167 iexport(alarm_sub_i8);
170 /* ALARM intrinsic with INTEGER as handler */
171 extern void alarm_sub_int_i4 (int *, int *, GFC_INTEGER_4 *);
172 iexport_proto(alarm_sub_int_i4);
174 void
175 alarm_sub_int_i4 (int *seconds __attribute__ ((unused)),
176 int *handler __attribute__ ((unused)),
177 GFC_INTEGER_4 *status)
179 #if defined (SIGALRM) && defined (HAVE_ALARM)
180 if (status != NULL)
182 if (signal (SIGALRM, (void (*)(int)) (intptr_t) *handler) == SIG_ERR)
183 *status = -1;
184 else
185 *status = alarm (*seconds);
187 else
189 signal (SIGALRM, (void (*)(int)) (intptr_t) *handler);
190 alarm (*seconds);
192 #else
193 errno = ENOSYS;
194 if (status != NULL)
195 *status = -1;
196 #endif
198 iexport(alarm_sub_int_i4);
201 extern void alarm_sub_int_i8 (int *, int *, GFC_INTEGER_8 *);
202 iexport_proto(alarm_sub_int_i8);
204 void
205 alarm_sub_int_i8 (int *seconds __attribute__ ((unused)),
206 int *handler __attribute__ ((unused)),
207 GFC_INTEGER_8 *status)
209 #if defined (SIGALRM) && defined (HAVE_ALARM)
210 if (status != NULL)
212 if (signal (SIGALRM, (void (*)(int)) (intptr_t) *handler) == SIG_ERR)
213 *status = -1;
214 else
215 *status = alarm (*seconds);
217 else
219 signal (SIGALRM, (void (*)(int)) (intptr_t) *handler);
220 alarm (*seconds);
222 #else
223 errno = ENOSYS;
224 if (status != NULL)
225 *status = -1;
226 #endif
228 iexport(alarm_sub_int_i8);