Wed Feb 22 00:44:41 1995 Roland McGrath <roland@churchy.gnu.ai.mit.edu>
[glibc.git] / sysdeps / generic / signame.c
blob40e72e0e4cb85434d81131738f708c334f767197
1 /* Convert between signal names and numbers.
2 Copyright (C) 1990, 1992, 1993, 1995 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
18 #include <stdio.h>
19 #include <sys/types.h> /* Some systems need this for <signal.h>. */
20 #include <signal.h>
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 /* Some systems do not define NSIG in <signal.h>. */
27 #ifndef NSIG
28 #ifdef _NSIG
29 #define NSIG _NSIG
30 #else
31 #define NSIG 32
32 #endif
33 #endif
35 #if !__STDC__
36 #define const
37 #endif
39 #include "signame.h"
41 #ifndef HAVE_SYS_SIGLIST
42 /* There is too much variation in Sys V signal numbers and names, so
43 we must initialize them at runtime. */
45 static const char undoc[] = "unknown signal";
47 const char *sys_siglist[NSIG];
49 #else /* HAVE_SYS_SIGLIST. */
51 #ifndef SYS_SIGLIST_DECLARED
52 extern char *sys_siglist[];
53 #endif /* Not SYS_SIGLIST_DECLARED. */
55 #endif /* Not HAVE_SYS_SIGLIST. */
57 /* Table of abbreviations for signals. Note: A given number can
58 appear more than once with different abbreviations. */
59 typedef struct
61 int number;
62 const char *abbrev;
63 } num_abbrev;
64 static num_abbrev sig_table[NSIG*2];
65 /* Number of elements of sig_table used. */
66 static int sig_table_nelts = 0;
68 /* Enter signal number NUMBER into the tables with ABBREV and NAME. */
70 static void
71 init_sig (number, abbrev, name)
72 int number;
73 const char *abbrev;
74 const char *name;
76 #ifndef HAVE_SYS_SIGLIST
77 sys_siglist[number] = name;
78 #endif
79 sig_table[sig_table_nelts].number = number;
80 sig_table[sig_table_nelts++].abbrev = abbrev;
83 void
84 signame_init ()
86 #ifndef HAVE_SYS_SIGLIST
87 int i;
88 /* Initialize signal names. */
89 for (i = 0; i < NSIG; i++)
90 sys_siglist[i] = undoc;
91 #endif /* !HAVE_SYS_SIGLIST */
93 /* Initialize signal names. */
94 #if defined (SIGHUP)
95 init_sig (SIGHUP, "HUP", "Hangup");
96 #endif
97 #if defined (SIGINT)
98 init_sig (SIGINT, "INT", "Interrupt");
99 #endif
100 #if defined (SIGQUIT)
101 init_sig (SIGQUIT, "QUIT", "Quit");
102 #endif
103 #if defined (SIGILL)
104 init_sig (SIGILL, "ILL", "Illegal Instruction");
105 #endif
106 #if defined (SIGTRAP)
107 init_sig (SIGTRAP, "TRAP", "Trace/breakpoint trap");
108 #endif
109 /* If SIGIOT == SIGABRT, we want to print it as SIGABRT because
110 SIGABRT is in ANSI and POSIX.1 and SIGIOT isn't. */
111 #if defined (SIGABRT)
112 init_sig (SIGABRT, "ABRT", "Aborted");
113 #endif
114 #if defined (SIGIOT)
115 init_sig (SIGIOT, "IOT", "IOT trap");
116 #endif
117 #if defined (SIGEMT)
118 init_sig (SIGEMT, "EMT", "EMT trap");
119 #endif
120 #if defined (SIGFPE)
121 init_sig (SIGFPE, "FPE", "Floating point exception");
122 #endif
123 #if defined (SIGKILL)
124 init_sig (SIGKILL, "KILL", "Killed");
125 #endif
126 #if defined (SIGBUS)
127 init_sig (SIGBUS, "BUS", "Bus error");
128 #endif
129 #if defined (SIGSEGV)
130 init_sig (SIGSEGV, "SEGV", "Segmentation fault");
131 #endif
132 #if defined (SIGSYS)
133 init_sig (SIGSYS, "SYS", "Bad system call");
134 #endif
135 #if defined (SIGPIPE)
136 init_sig (SIGPIPE, "PIPE", "Broken pipe");
137 #endif
138 #if defined (SIGALRM)
139 init_sig (SIGALRM, "ALRM", "Alarm clock");
140 #endif
141 #if defined (SIGTERM)
142 init_sig (SIGTERM, "TERM", "Terminated");
143 #endif
144 #if defined (SIGUSR1)
145 init_sig (SIGUSR1, "USR1", "User defined signal 1");
146 #endif
147 #if defined (SIGUSR2)
148 init_sig (SIGUSR2, "USR2", "User defined signal 2");
149 #endif
150 /* If SIGCLD == SIGCHLD, we want to print it as SIGCHLD because that
151 is what is in POSIX.1. */
152 #if defined (SIGCHLD)
153 init_sig (SIGCHLD, "CHLD", "Child exited");
154 #endif
155 #if defined (SIGCLD)
156 init_sig (SIGCLD, "CLD", "Child exited");
157 #endif
158 #if defined (SIGPWR)
159 init_sig (SIGPWR, "PWR", "Power failure");
160 #endif
161 #if defined (SIGTSTP)
162 init_sig (SIGTSTP, "TSTP", "Stopped");
163 #endif
164 #if defined (SIGTTIN)
165 init_sig (SIGTTIN, "TTIN", "Stopped (tty input)");
166 #endif
167 #if defined (SIGTTOU)
168 init_sig (SIGTTOU, "TTOU", "Stopped (tty output)");
169 #endif
170 #if defined (SIGSTOP)
171 init_sig (SIGSTOP, "STOP", "Stopped (signal)");
172 #endif
173 #if defined (SIGXCPU)
174 init_sig (SIGXCPU, "XCPU", "CPU time limit exceeded");
175 #endif
176 #if defined (SIGXFSZ)
177 init_sig (SIGXFSZ, "XFSZ", "File size limit exceeded");
178 #endif
179 #if defined (SIGVTALRM)
180 init_sig (SIGVTALRM, "VTALRM", "Virtual timer expired");
181 #endif
182 #if defined (SIGPROF)
183 init_sig (SIGPROF, "PROF", "Profiling timer expired");
184 #endif
185 #if defined (SIGWINCH)
186 /* "Window size changed" might be more accurate, but even if that
187 is all that it means now, perhaps in the future it will be
188 extended to cover other kinds of window changes. */
189 init_sig (SIGWINCH, "WINCH", "Window changed");
190 #endif
191 #if defined (SIGCONT)
192 init_sig (SIGCONT, "CONT", "Continued");
193 #endif
194 #if defined (SIGURG)
195 init_sig (SIGURG, "URG", "Urgent I/O condition");
196 #endif
197 #if defined (SIGIO)
198 /* "I/O pending" has also been suggested. A disadvantage is
199 that signal only happens when the process has
200 asked for it, not everytime I/O is pending. Another disadvantage
201 is the confusion from giving it a different name than under Unix. */
202 init_sig (SIGIO, "IO", "I/O possible");
203 #endif
204 #if defined (SIGWIND)
205 init_sig (SIGWIND, "WIND", "SIGWIND");
206 #endif
207 #if defined (SIGPHONE)
208 init_sig (SIGPHONE, "PHONE", "SIGPHONE");
209 #endif
210 #if defined (SIGPOLL)
211 init_sig (SIGPOLL, "POLL", "I/O possible");
212 #endif
213 #if defined (SIGLOST)
214 init_sig (SIGLOST, "LOST", "Resource lost");
215 #endif
216 #if defined (SIGDANGER)
217 init_sig (SIGDANGER, "DANGER", "Danger signal");
218 #endif
221 /* Return the abbreviation for signal NUMBER. */
223 char *
224 sig_abbrev (number)
225 int number;
227 int i;
229 if (sig_table_nelts == 0)
230 signame_init ();
232 for (i = 0; i < sig_table_nelts; i++)
233 if (sig_table[i].number == number)
234 return (char *)sig_table[i].abbrev;
235 return NULL;
238 /* Return the signal number for an ABBREV, or -1 if there is no
239 signal by that name. */
242 sig_number (abbrev)
243 const char *abbrev;
245 int i;
247 if (sig_table_nelts == 0)
248 signame_init ();
250 /* Skip over "SIG" if present. */
251 if (abbrev[0] == 'S' && abbrev[1] == 'I' && abbrev[2] == 'G')
252 abbrev += 3;
254 for (i = 0; i < sig_table_nelts; i++)
255 if (abbrev[0] == sig_table[i].abbrev[0]
256 && strcmp (abbrev, sig_table[i].abbrev) == 0)
257 return sig_table[i].number;
258 return -1;
261 #ifndef HAVE_PSIGNAL
262 /* Print to standard error the name of SIGNAL, preceded by MESSAGE and
263 a colon, and followed by a newline. */
265 void
266 psignal (signal, message)
267 int signal;
268 const char *message;
270 if (signal <= 0 || signal >= NSIG)
271 fprintf (stderr, "%s: unknown signal", message);
272 else
273 fprintf (stderr, "%s: %s\n", message, sys_siglist[signal]);
275 #endif
277 #ifndef HAVE_STRSIGNAL
278 /* Return the string associated with the signal number. */
280 char *
281 strsignal (signal)
282 int signal;
284 static char buf[] = "Signal 12345678901234567890";
286 if (signal > 0 || signal < NSIG)
287 return sys_siglist[signal];
289 sprintf (buf, "Signal %d", signal);
290 return buf;
292 #endif