* Make some portability fixes.
[make.git] / signame.c
blobdd73723e9a35ababb880b327e942ee189bcb9d15
1 /* Convert between signal names and numbers.
2 Copyright (C) 1990, 1992, 1993, 1995, 1996 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
24 #include <stdio.h>
25 #include <sys/types.h> /* Some systems need this for <signal.h>. */
26 #include <signal.h>
28 #ifdef HAVE_STRING_H
29 #include <string.h>
30 #endif
32 /* Some systems declare `sys_siglist in <unistd.h>; if
33 configure defined SYS_SIGLIST_DECLARED, it may expect
34 to find the declaration there. */
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
40 /* Some systems do not define NSIG in <signal.h>. */
41 #ifndef NSIG
42 #ifdef _NSIG
43 #define NSIG _NSIG
44 #else
45 #define NSIG 32
46 #endif
47 #endif
49 #if !__STDC__
50 #define const
51 #endif
53 #include "signame.h"
55 #ifndef HAVE_SYS_SIGLIST
56 /* There is too much variation in Sys V signal numbers and names, so
57 we must initialize them at runtime. */
59 static const char undoc[] = "unknown signal";
61 const char *sys_siglist[NSIG];
63 #else /* HAVE_SYS_SIGLIST. */
65 #ifndef SYS_SIGLIST_DECLARED
66 extern char *sys_siglist[];
67 #endif /* Not SYS_SIGLIST_DECLARED. */
69 #endif /* Not HAVE_SYS_SIGLIST. */
71 /* Table of abbreviations for signals. Note: A given number can
72 appear more than once with different abbreviations. */
73 #define SIG_TABLE_SIZE (NSIG*2)
75 typedef struct
77 int number;
78 const char *abbrev;
79 } num_abbrev;
80 static num_abbrev sig_table[SIG_TABLE_SIZE];
81 /* Number of elements of sig_table used. */
82 static int sig_table_nelts = 0;
84 /* Enter signal number NUMBER into the tables with ABBREV and NAME. */
86 static void
87 init_sig (number, abbrev, name)
88 int number;
89 const char *abbrev;
90 const char *name;
92 #ifndef HAVE_SYS_SIGLIST
93 /* If this value is ever greater than NSIG it seems like it'd be a bug in
94 the system headers, but... better safe than sorry. We know, for
95 example, that this isn't always true on VMS. */
97 if (number >= 0 && number < NSIG)
98 sys_siglist[number] = name;
99 #endif
100 if (sig_table_nelts < SIG_TABLE_SIZE)
102 sig_table[sig_table_nelts].number = number;
103 sig_table[sig_table_nelts++].abbrev = abbrev;
107 void
108 signame_init ()
110 #ifndef HAVE_SYS_SIGLIST
111 int i;
112 /* Initialize signal names. */
113 for (i = 0; i < NSIG; i++)
114 sys_siglist[i] = undoc;
115 #endif /* !HAVE_SYS_SIGLIST */
117 /* Initialize signal names. */
118 #if defined (SIGHUP)
119 init_sig (SIGHUP, "HUP", "Hangup");
120 #endif
121 #if defined (SIGINT)
122 init_sig (SIGINT, "INT", "Interrupt");
123 #endif
124 #if defined (SIGQUIT)
125 init_sig (SIGQUIT, "QUIT", "Quit");
126 #endif
127 #if defined (SIGILL)
128 init_sig (SIGILL, "ILL", "Illegal Instruction");
129 #endif
130 #if defined (SIGTRAP)
131 init_sig (SIGTRAP, "TRAP", "Trace/breakpoint trap");
132 #endif
133 /* If SIGIOT == SIGABRT, we want to print it as SIGABRT because
134 SIGABRT is in ANSI and POSIX.1 and SIGIOT isn't. */
135 #if defined (SIGABRT)
136 init_sig (SIGABRT, "ABRT", "Aborted");
137 #endif
138 #if defined (SIGIOT)
139 init_sig (SIGIOT, "IOT", "IOT trap");
140 #endif
141 #if defined (SIGEMT)
142 init_sig (SIGEMT, "EMT", "EMT trap");
143 #endif
144 #if defined (SIGFPE)
145 init_sig (SIGFPE, "FPE", "Floating point exception");
146 #endif
147 #if defined (SIGKILL)
148 init_sig (SIGKILL, "KILL", "Killed");
149 #endif
150 #if defined (SIGBUS)
151 init_sig (SIGBUS, "BUS", "Bus error");
152 #endif
153 #if defined (SIGSEGV)
154 init_sig (SIGSEGV, "SEGV", "Segmentation fault");
155 #endif
156 #if defined (SIGSYS)
157 init_sig (SIGSYS, "SYS", "Bad system call");
158 #endif
159 #if defined (SIGPIPE)
160 init_sig (SIGPIPE, "PIPE", "Broken pipe");
161 #endif
162 #if defined (SIGALRM)
163 init_sig (SIGALRM, "ALRM", "Alarm clock");
164 #endif
165 #if defined (SIGTERM)
166 init_sig (SIGTERM, "TERM", "Terminated");
167 #endif
168 #if defined (SIGUSR1)
169 init_sig (SIGUSR1, "USR1", "User defined signal 1");
170 #endif
171 #if defined (SIGUSR2)
172 init_sig (SIGUSR2, "USR2", "User defined signal 2");
173 #endif
174 /* If SIGCLD == SIGCHLD, we want to print it as SIGCHLD because that
175 is what is in POSIX.1. */
176 #if defined (SIGCHLD)
177 init_sig (SIGCHLD, "CHLD", "Child exited");
178 #endif
179 #if defined (SIGCLD)
180 init_sig (SIGCLD, "CLD", "Child exited");
181 #endif
182 #if defined (SIGPWR)
183 init_sig (SIGPWR, "PWR", "Power failure");
184 #endif
185 #if defined (SIGTSTP)
186 init_sig (SIGTSTP, "TSTP", "Stopped");
187 #endif
188 #if defined (SIGTTIN)
189 init_sig (SIGTTIN, "TTIN", "Stopped (tty input)");
190 #endif
191 #if defined (SIGTTOU)
192 init_sig (SIGTTOU, "TTOU", "Stopped (tty output)");
193 #endif
194 #if defined (SIGSTOP)
195 init_sig (SIGSTOP, "STOP", "Stopped (signal)");
196 #endif
197 #if defined (SIGXCPU)
198 init_sig (SIGXCPU, "XCPU", "CPU time limit exceeded");
199 #endif
200 #if defined (SIGXFSZ)
201 init_sig (SIGXFSZ, "XFSZ", "File size limit exceeded");
202 #endif
203 #if defined (SIGVTALRM)
204 init_sig (SIGVTALRM, "VTALRM", "Virtual timer expired");
205 #endif
206 #if defined (SIGPROF)
207 init_sig (SIGPROF, "PROF", "Profiling timer expired");
208 #endif
209 #if defined (SIGWINCH)
210 /* "Window size changed" might be more accurate, but even if that
211 is all that it means now, perhaps in the future it will be
212 extended to cover other kinds of window changes. */
213 init_sig (SIGWINCH, "WINCH", "Window changed");
214 #endif
215 #if defined (SIGCONT)
216 init_sig (SIGCONT, "CONT", "Continued");
217 #endif
218 #if defined (SIGURG)
219 init_sig (SIGURG, "URG", "Urgent I/O condition");
220 #endif
221 #if defined (SIGIO)
222 /* "I/O pending" has also been suggested. A disadvantage is
223 that signal only happens when the process has
224 asked for it, not everytime I/O is pending. Another disadvantage
225 is the confusion from giving it a different name than under Unix. */
226 init_sig (SIGIO, "IO", "I/O possible");
227 #endif
228 #if defined (SIGWIND)
229 init_sig (SIGWIND, "WIND", "SIGWIND");
230 #endif
231 #if defined (SIGPHONE)
232 init_sig (SIGPHONE, "PHONE", "SIGPHONE");
233 #endif
234 #if defined (SIGPOLL)
235 init_sig (SIGPOLL, "POLL", "I/O possible");
236 #endif
237 #if defined (SIGLOST)
238 init_sig (SIGLOST, "LOST", "Resource lost");
239 #endif
240 #if defined (SIGDANGER)
241 init_sig (SIGDANGER, "DANGER", "Danger signal");
242 #endif
243 #if defined (SIGINFO)
244 init_sig (SIGINFO, "INFO", "Information request");
245 #endif
246 #if defined (SIGNOFP)
247 init_sig (SIGNOFP, "NOFP", "Floating point co-processor not available");
248 #endif
251 /* Return the abbreviation for signal NUMBER. */
253 char *
254 sig_abbrev (number)
255 int number;
257 int i;
259 if (sig_table_nelts == 0)
260 signame_init ();
262 for (i = 0; i < sig_table_nelts; i++)
263 if (sig_table[i].number == number)
264 return (char *)sig_table[i].abbrev;
265 return NULL;
268 /* Return the signal number for an ABBREV, or -1 if there is no
269 signal by that name. */
272 sig_number (abbrev)
273 const char *abbrev;
275 int i;
277 if (sig_table_nelts == 0)
278 signame_init ();
280 /* Skip over "SIG" if present. */
281 if (abbrev[0] == 'S' && abbrev[1] == 'I' && abbrev[2] == 'G')
282 abbrev += 3;
284 for (i = 0; i < sig_table_nelts; i++)
285 if (abbrev[0] == sig_table[i].abbrev[0]
286 && strcmp (abbrev, sig_table[i].abbrev) == 0)
287 return sig_table[i].number;
288 return -1;
291 #ifndef HAVE_PSIGNAL
292 /* Print to standard error the name of SIGNAL, preceded by MESSAGE and
293 a colon, and followed by a newline. */
295 void
296 psignal (signal, message)
297 int signal;
298 const char *message;
300 if (signal <= 0 || signal >= NSIG)
301 fprintf (stderr, "%s: unknown signal", message);
302 else
303 fprintf (stderr, "%s: %s\n", message, sys_siglist[signal]);
305 #endif
307 #ifndef HAVE_STRSIGNAL
308 /* Return the string associated with the signal number. */
310 char *
311 strsignal (signal)
312 int signal;
314 static char buf[] = "Signal 12345678901234567890";
316 if (signal > 0 || signal < NSIG)
317 return (char *) sys_siglist[signal];
319 sprintf (buf, "Signal %d", signal);
320 return buf;
322 #endif