xref: /dflybsd-src/contrib/binutils-2.27/libiberty/strsignal.c (revision e656dc90e3d65d744d534af2f5ea88cf8101ebcf)
1*a9fa9459Szrj /* Extended support for using signal values.
2*a9fa9459Szrj    Written by Fred Fish.  fnf@cygnus.com
3*a9fa9459Szrj    This file is in the public domain.  */
4*a9fa9459Szrj 
5*a9fa9459Szrj #include "config.h"
6*a9fa9459Szrj #include "ansidecl.h"
7*a9fa9459Szrj #include "libiberty.h"
8*a9fa9459Szrj 
9*a9fa9459Szrj /* We need to declare sys_siglist, because even if the system provides
10*a9fa9459Szrj    it we can't assume that it is declared in <signal.h> (for example,
11*a9fa9459Szrj    SunOS provides sys_siglist, but it does not declare it in any
12*a9fa9459Szrj    header file).  However, we can't declare sys_siglist portably,
13*a9fa9459Szrj    because on some systems it is declared with const and on some
14*a9fa9459Szrj    systems it is declared without const.  If we were using autoconf,
15*a9fa9459Szrj    we could work out the right declaration.  Until, then we just
16*a9fa9459Szrj    ignore any declaration in the system header files, and always
17*a9fa9459Szrj    declare it ourselves.  With luck, this will always work.  */
18*a9fa9459Szrj #define sys_siglist no_such_symbol
19*a9fa9459Szrj #define sys_nsig sys_nsig__no_such_symbol
20*a9fa9459Szrj 
21*a9fa9459Szrj #include <stdio.h>
22*a9fa9459Szrj #include <signal.h>
23*a9fa9459Szrj 
24*a9fa9459Szrj /*  Routines imported from standard C runtime libraries. */
25*a9fa9459Szrj 
26*a9fa9459Szrj #ifdef HAVE_STDLIB_H
27*a9fa9459Szrj #include <stdlib.h>
28*a9fa9459Szrj #else
29*a9fa9459Szrj extern PTR malloc ();
30*a9fa9459Szrj #endif
31*a9fa9459Szrj 
32*a9fa9459Szrj #ifdef HAVE_STRING_H
33*a9fa9459Szrj #include <string.h>
34*a9fa9459Szrj #else
35*a9fa9459Szrj extern PTR memset ();
36*a9fa9459Szrj #endif
37*a9fa9459Szrj 
38*a9fa9459Szrj /* Undefine the macro we used to hide the definition of sys_siglist
39*a9fa9459Szrj    found in the system header files.  */
40*a9fa9459Szrj #undef sys_siglist
41*a9fa9459Szrj #undef sys_nsig
42*a9fa9459Szrj 
43*a9fa9459Szrj #ifndef NULL
44*a9fa9459Szrj #  define NULL (void *) 0
45*a9fa9459Szrj #endif
46*a9fa9459Szrj 
47*a9fa9459Szrj #ifndef MAX
48*a9fa9459Szrj #  define MAX(a,b) ((a) > (b) ? (a) : (b))
49*a9fa9459Szrj #endif
50*a9fa9459Szrj 
51*a9fa9459Szrj static void init_signal_tables (void);
52*a9fa9459Szrj 
53*a9fa9459Szrj /* Translation table for signal values.
54*a9fa9459Szrj 
55*a9fa9459Szrj    Note that this table is generally only accessed when it is used at runtime
56*a9fa9459Szrj    to initialize signal name and message tables that are indexed by signal
57*a9fa9459Szrj    value.
58*a9fa9459Szrj 
59*a9fa9459Szrj    Not all of these signals will exist on all systems.  This table is the only
60*a9fa9459Szrj    thing that should have to be updated as new signal numbers are introduced.
61*a9fa9459Szrj    It's sort of ugly, but at least its portable. */
62*a9fa9459Szrj 
63*a9fa9459Szrj struct signal_info
64*a9fa9459Szrj {
65*a9fa9459Szrj   const int value;		/* The numeric value from <signal.h> */
66*a9fa9459Szrj   const char *const name;	/* The equivalent symbolic value */
67*a9fa9459Szrj #ifndef HAVE_SYS_SIGLIST
68*a9fa9459Szrj   const char *const msg;	/* Short message about this value */
69*a9fa9459Szrj #endif
70*a9fa9459Szrj };
71*a9fa9459Szrj 
72*a9fa9459Szrj #ifndef HAVE_SYS_SIGLIST
73*a9fa9459Szrj #   define ENTRY(value, name, msg)	{value, name, msg}
74*a9fa9459Szrj #else
75*a9fa9459Szrj #   define ENTRY(value, name, msg)	{value, name}
76*a9fa9459Szrj #endif
77*a9fa9459Szrj 
78*a9fa9459Szrj static const struct signal_info signal_table[] =
79*a9fa9459Szrj {
80*a9fa9459Szrj #if defined (SIGHUP)
81*a9fa9459Szrj   ENTRY(SIGHUP, "SIGHUP", "Hangup"),
82*a9fa9459Szrj #endif
83*a9fa9459Szrj #if defined (SIGINT)
84*a9fa9459Szrj   ENTRY(SIGINT, "SIGINT", "Interrupt"),
85*a9fa9459Szrj #endif
86*a9fa9459Szrj #if defined (SIGQUIT)
87*a9fa9459Szrj   ENTRY(SIGQUIT, "SIGQUIT", "Quit"),
88*a9fa9459Szrj #endif
89*a9fa9459Szrj #if defined (SIGILL)
90*a9fa9459Szrj   ENTRY(SIGILL, "SIGILL", "Illegal instruction"),
91*a9fa9459Szrj #endif
92*a9fa9459Szrj #if defined (SIGTRAP)
93*a9fa9459Szrj   ENTRY(SIGTRAP, "SIGTRAP", "Trace/breakpoint trap"),
94*a9fa9459Szrj #endif
95*a9fa9459Szrj /* Put SIGIOT before SIGABRT, so that if SIGIOT==SIGABRT then SIGABRT
96*a9fa9459Szrj    overrides SIGIOT.  SIGABRT is in ANSI and POSIX.1, and SIGIOT isn't. */
97*a9fa9459Szrj #if defined (SIGIOT)
98*a9fa9459Szrj   ENTRY(SIGIOT, "SIGIOT", "IOT trap"),
99*a9fa9459Szrj #endif
100*a9fa9459Szrj #if defined (SIGABRT)
101*a9fa9459Szrj   ENTRY(SIGABRT, "SIGABRT", "Aborted"),
102*a9fa9459Szrj #endif
103*a9fa9459Szrj #if defined (SIGEMT)
104*a9fa9459Szrj   ENTRY(SIGEMT, "SIGEMT", "Emulation trap"),
105*a9fa9459Szrj #endif
106*a9fa9459Szrj #if defined (SIGFPE)
107*a9fa9459Szrj   ENTRY(SIGFPE, "SIGFPE", "Arithmetic exception"),
108*a9fa9459Szrj #endif
109*a9fa9459Szrj #if defined (SIGKILL)
110*a9fa9459Szrj   ENTRY(SIGKILL, "SIGKILL", "Killed"),
111*a9fa9459Szrj #endif
112*a9fa9459Szrj #if defined (SIGBUS)
113*a9fa9459Szrj   ENTRY(SIGBUS, "SIGBUS", "Bus error"),
114*a9fa9459Szrj #endif
115*a9fa9459Szrj #if defined (SIGSEGV)
116*a9fa9459Szrj   ENTRY(SIGSEGV, "SIGSEGV", "Segmentation fault"),
117*a9fa9459Szrj #endif
118*a9fa9459Szrj #if defined (SIGSYS)
119*a9fa9459Szrj   ENTRY(SIGSYS, "SIGSYS", "Bad system call"),
120*a9fa9459Szrj #endif
121*a9fa9459Szrj #if defined (SIGPIPE)
122*a9fa9459Szrj   ENTRY(SIGPIPE, "SIGPIPE", "Broken pipe"),
123*a9fa9459Szrj #endif
124*a9fa9459Szrj #if defined (SIGALRM)
125*a9fa9459Szrj   ENTRY(SIGALRM, "SIGALRM", "Alarm clock"),
126*a9fa9459Szrj #endif
127*a9fa9459Szrj #if defined (SIGTERM)
128*a9fa9459Szrj   ENTRY(SIGTERM, "SIGTERM", "Terminated"),
129*a9fa9459Szrj #endif
130*a9fa9459Szrj #if defined (SIGUSR1)
131*a9fa9459Szrj   ENTRY(SIGUSR1, "SIGUSR1", "User defined signal 1"),
132*a9fa9459Szrj #endif
133*a9fa9459Szrj #if defined (SIGUSR2)
134*a9fa9459Szrj   ENTRY(SIGUSR2, "SIGUSR2", "User defined signal 2"),
135*a9fa9459Szrj #endif
136*a9fa9459Szrj /* Put SIGCLD before SIGCHLD, so that if SIGCLD==SIGCHLD then SIGCHLD
137*a9fa9459Szrj    overrides SIGCLD.  SIGCHLD is in POXIX.1 */
138*a9fa9459Szrj #if defined (SIGCLD)
139*a9fa9459Szrj   ENTRY(SIGCLD, "SIGCLD", "Child status changed"),
140*a9fa9459Szrj #endif
141*a9fa9459Szrj #if defined (SIGCHLD)
142*a9fa9459Szrj   ENTRY(SIGCHLD, "SIGCHLD", "Child status changed"),
143*a9fa9459Szrj #endif
144*a9fa9459Szrj #if defined (SIGPWR)
145*a9fa9459Szrj   ENTRY(SIGPWR, "SIGPWR", "Power fail/restart"),
146*a9fa9459Szrj #endif
147*a9fa9459Szrj #if defined (SIGWINCH)
148*a9fa9459Szrj   ENTRY(SIGWINCH, "SIGWINCH", "Window size changed"),
149*a9fa9459Szrj #endif
150*a9fa9459Szrj #if defined (SIGURG)
151*a9fa9459Szrj   ENTRY(SIGURG, "SIGURG", "Urgent I/O condition"),
152*a9fa9459Szrj #endif
153*a9fa9459Szrj #if defined (SIGIO)
154*a9fa9459Szrj   /* "I/O pending" has also been suggested, but is misleading since the
155*a9fa9459Szrj      signal only happens when the process has asked for it, not everytime
156*a9fa9459Szrj      I/O is pending. */
157*a9fa9459Szrj   ENTRY(SIGIO, "SIGIO", "I/O possible"),
158*a9fa9459Szrj #endif
159*a9fa9459Szrj #if defined (SIGPOLL)
160*a9fa9459Szrj   ENTRY(SIGPOLL, "SIGPOLL", "Pollable event occurred"),
161*a9fa9459Szrj #endif
162*a9fa9459Szrj #if defined (SIGSTOP)
163*a9fa9459Szrj   ENTRY(SIGSTOP, "SIGSTOP", "Stopped (signal)"),
164*a9fa9459Szrj #endif
165*a9fa9459Szrj #if defined (SIGTSTP)
166*a9fa9459Szrj   ENTRY(SIGTSTP, "SIGTSTP", "Stopped (user)"),
167*a9fa9459Szrj #endif
168*a9fa9459Szrj #if defined (SIGCONT)
169*a9fa9459Szrj   ENTRY(SIGCONT, "SIGCONT", "Continued"),
170*a9fa9459Szrj #endif
171*a9fa9459Szrj #if defined (SIGTTIN)
172*a9fa9459Szrj   ENTRY(SIGTTIN, "SIGTTIN", "Stopped (tty input)"),
173*a9fa9459Szrj #endif
174*a9fa9459Szrj #if defined (SIGTTOU)
175*a9fa9459Szrj   ENTRY(SIGTTOU, "SIGTTOU", "Stopped (tty output)"),
176*a9fa9459Szrj #endif
177*a9fa9459Szrj #if defined (SIGVTALRM)
178*a9fa9459Szrj   ENTRY(SIGVTALRM, "SIGVTALRM", "Virtual timer expired"),
179*a9fa9459Szrj #endif
180*a9fa9459Szrj #if defined (SIGPROF)
181*a9fa9459Szrj   ENTRY(SIGPROF, "SIGPROF", "Profiling timer expired"),
182*a9fa9459Szrj #endif
183*a9fa9459Szrj #if defined (SIGXCPU)
184*a9fa9459Szrj   ENTRY(SIGXCPU, "SIGXCPU", "CPU time limit exceeded"),
185*a9fa9459Szrj #endif
186*a9fa9459Szrj #if defined (SIGXFSZ)
187*a9fa9459Szrj   ENTRY(SIGXFSZ, "SIGXFSZ", "File size limit exceeded"),
188*a9fa9459Szrj #endif
189*a9fa9459Szrj #if defined (SIGWIND)
190*a9fa9459Szrj   ENTRY(SIGWIND, "SIGWIND", "SIGWIND"),
191*a9fa9459Szrj #endif
192*a9fa9459Szrj #if defined (SIGPHONE)
193*a9fa9459Szrj   ENTRY(SIGPHONE, "SIGPHONE", "SIGPHONE"),
194*a9fa9459Szrj #endif
195*a9fa9459Szrj #if defined (SIGLOST)
196*a9fa9459Szrj   ENTRY(SIGLOST, "SIGLOST", "Resource lost"),
197*a9fa9459Szrj #endif
198*a9fa9459Szrj #if defined (SIGWAITING)
199*a9fa9459Szrj   ENTRY(SIGWAITING, "SIGWAITING", "Process's LWPs are blocked"),
200*a9fa9459Szrj #endif
201*a9fa9459Szrj #if defined (SIGLWP)
202*a9fa9459Szrj   ENTRY(SIGLWP, "SIGLWP", "Signal LWP"),
203*a9fa9459Szrj #endif
204*a9fa9459Szrj #if defined (SIGDANGER)
205*a9fa9459Szrj   ENTRY(SIGDANGER, "SIGDANGER", "Swap space dangerously low"),
206*a9fa9459Szrj #endif
207*a9fa9459Szrj #if defined (SIGGRANT)
208*a9fa9459Szrj   ENTRY(SIGGRANT, "SIGGRANT", "Monitor mode granted"),
209*a9fa9459Szrj #endif
210*a9fa9459Szrj #if defined (SIGRETRACT)
211*a9fa9459Szrj   ENTRY(SIGRETRACT, "SIGRETRACT", "Need to relinguish monitor mode"),
212*a9fa9459Szrj #endif
213*a9fa9459Szrj #if defined (SIGMSG)
214*a9fa9459Szrj   ENTRY(SIGMSG, "SIGMSG", "Monitor mode data available"),
215*a9fa9459Szrj #endif
216*a9fa9459Szrj #if defined (SIGSOUND)
217*a9fa9459Szrj   ENTRY(SIGSOUND, "SIGSOUND", "Sound completed"),
218*a9fa9459Szrj #endif
219*a9fa9459Szrj #if defined (SIGSAK)
220*a9fa9459Szrj   ENTRY(SIGSAK, "SIGSAK", "Secure attention"),
221*a9fa9459Szrj #endif
222*a9fa9459Szrj   ENTRY(0, NULL, NULL)
223*a9fa9459Szrj };
224*a9fa9459Szrj 
225*a9fa9459Szrj /* Translation table allocated and initialized at runtime.  Indexed by the
226*a9fa9459Szrj    signal value to find the equivalent symbolic value. */
227*a9fa9459Szrj 
228*a9fa9459Szrj static const char **signal_names;
229*a9fa9459Szrj static int num_signal_names = 0;
230*a9fa9459Szrj 
231*a9fa9459Szrj /* Translation table allocated and initialized at runtime, if it does not
232*a9fa9459Szrj    already exist in the host environment.  Indexed by the signal value to find
233*a9fa9459Szrj    the descriptive string.
234*a9fa9459Szrj 
235*a9fa9459Szrj    We don't export it for use in other modules because even though it has the
236*a9fa9459Szrj    same name, it differs from other implementations in that it is dynamically
237*a9fa9459Szrj    initialized rather than statically initialized. */
238*a9fa9459Szrj 
239*a9fa9459Szrj #ifndef HAVE_SYS_SIGLIST
240*a9fa9459Szrj 
241*a9fa9459Szrj static int sys_nsig;
242*a9fa9459Szrj static const char **sys_siglist;
243*a9fa9459Szrj 
244*a9fa9459Szrj #else
245*a9fa9459Szrj 
246*a9fa9459Szrj #ifdef NSIG
247*a9fa9459Szrj static int sys_nsig = NSIG;
248*a9fa9459Szrj #else
249*a9fa9459Szrj #ifdef _NSIG
250*a9fa9459Szrj static int sys_nsig = _NSIG;
251*a9fa9459Szrj #endif
252*a9fa9459Szrj #endif
253*a9fa9459Szrj extern const char * const sys_siglist[];
254*a9fa9459Szrj 
255*a9fa9459Szrj #endif
256*a9fa9459Szrj 
257*a9fa9459Szrj 
258*a9fa9459Szrj /*
259*a9fa9459Szrj 
260*a9fa9459Szrj NAME
261*a9fa9459Szrj 
262*a9fa9459Szrj 	init_signal_tables -- initialize the name and message tables
263*a9fa9459Szrj 
264*a9fa9459Szrj SYNOPSIS
265*a9fa9459Szrj 
266*a9fa9459Szrj 	static void init_signal_tables ();
267*a9fa9459Szrj 
268*a9fa9459Szrj DESCRIPTION
269*a9fa9459Szrj 
270*a9fa9459Szrj 	Using the signal_table, which is initialized at compile time, generate
271*a9fa9459Szrj 	the signal_names and the sys_siglist (if needed) tables, which are
272*a9fa9459Szrj 	indexed at runtime by a specific signal value.
273*a9fa9459Szrj 
274*a9fa9459Szrj BUGS
275*a9fa9459Szrj 
276*a9fa9459Szrj 	The initialization of the tables may fail under low memory conditions,
277*a9fa9459Szrj 	in which case we don't do anything particularly useful, but we don't
278*a9fa9459Szrj 	bomb either.  Who knows, it might succeed at a later point if we free
279*a9fa9459Szrj 	some memory in the meantime.  In any case, the other routines know
280*a9fa9459Szrj 	how to deal with lack of a table after trying to initialize it.  This
281*a9fa9459Szrj 	may or may not be considered to be a bug, that we don't specifically
282*a9fa9459Szrj 	warn about this particular failure mode.
283*a9fa9459Szrj 
284*a9fa9459Szrj */
285*a9fa9459Szrj 
286*a9fa9459Szrj static void
init_signal_tables(void)287*a9fa9459Szrj init_signal_tables (void)
288*a9fa9459Szrj {
289*a9fa9459Szrj   const struct signal_info *eip;
290*a9fa9459Szrj   int nbytes;
291*a9fa9459Szrj 
292*a9fa9459Szrj   /* If we haven't already scanned the signal_table once to find the maximum
293*a9fa9459Szrj      signal value, then go find it now. */
294*a9fa9459Szrj 
295*a9fa9459Szrj   if (num_signal_names == 0)
296*a9fa9459Szrj     {
297*a9fa9459Szrj       for (eip = signal_table; eip -> name != NULL; eip++)
298*a9fa9459Szrj 	{
299*a9fa9459Szrj 	  if (eip -> value >= num_signal_names)
300*a9fa9459Szrj 	    {
301*a9fa9459Szrj 	      num_signal_names = eip -> value + 1;
302*a9fa9459Szrj 	    }
303*a9fa9459Szrj 	}
304*a9fa9459Szrj     }
305*a9fa9459Szrj 
306*a9fa9459Szrj   /* Now attempt to allocate the signal_names table, zero it out, and then
307*a9fa9459Szrj      initialize it from the statically initialized signal_table. */
308*a9fa9459Szrj 
309*a9fa9459Szrj   if (signal_names == NULL)
310*a9fa9459Szrj     {
311*a9fa9459Szrj       nbytes = num_signal_names * sizeof (char *);
312*a9fa9459Szrj       if ((signal_names = (const char **) malloc (nbytes)) != NULL)
313*a9fa9459Szrj 	{
314*a9fa9459Szrj 	  memset (signal_names, 0, nbytes);
315*a9fa9459Szrj 	  for (eip = signal_table; eip -> name != NULL; eip++)
316*a9fa9459Szrj 	    {
317*a9fa9459Szrj 	      signal_names[eip -> value] = eip -> name;
318*a9fa9459Szrj 	    }
319*a9fa9459Szrj 	}
320*a9fa9459Szrj     }
321*a9fa9459Szrj 
322*a9fa9459Szrj #ifndef HAVE_SYS_SIGLIST
323*a9fa9459Szrj 
324*a9fa9459Szrj   /* Now attempt to allocate the sys_siglist table, zero it out, and then
325*a9fa9459Szrj      initialize it from the statically initialized signal_table. */
326*a9fa9459Szrj 
327*a9fa9459Szrj   if (sys_siglist == NULL)
328*a9fa9459Szrj     {
329*a9fa9459Szrj       nbytes = num_signal_names * sizeof (char *);
330*a9fa9459Szrj       if ((sys_siglist = (const char **) malloc (nbytes)) != NULL)
331*a9fa9459Szrj 	{
332*a9fa9459Szrj 	  memset (sys_siglist, 0, nbytes);
333*a9fa9459Szrj 	  sys_nsig = num_signal_names;
334*a9fa9459Szrj 	  for (eip = signal_table; eip -> name != NULL; eip++)
335*a9fa9459Szrj 	    {
336*a9fa9459Szrj 	      sys_siglist[eip -> value] = eip -> msg;
337*a9fa9459Szrj 	    }
338*a9fa9459Szrj 	}
339*a9fa9459Szrj     }
340*a9fa9459Szrj 
341*a9fa9459Szrj #endif
342*a9fa9459Szrj 
343*a9fa9459Szrj }
344*a9fa9459Szrj 
345*a9fa9459Szrj 
346*a9fa9459Szrj /*
347*a9fa9459Szrj 
348*a9fa9459Szrj @deftypefn Extension int signo_max (void)
349*a9fa9459Szrj 
350*a9fa9459Szrj Returns the maximum signal value for which a corresponding symbolic
351*a9fa9459Szrj name or message is available.  Note that in the case where we use the
352*a9fa9459Szrj @code{sys_siglist} supplied by the system, it is possible for there to
353*a9fa9459Szrj be more symbolic names than messages, or vice versa.  In fact, the
354*a9fa9459Szrj manual page for @code{psignal(3b)} explicitly warns that one should
355*a9fa9459Szrj check the size of the table (@code{NSIG}) before indexing it, since
356*a9fa9459Szrj new signal codes may be added to the system before they are added to
357*a9fa9459Szrj the table.  Thus @code{NSIG} might be smaller than value implied by
358*a9fa9459Szrj the largest signo value defined in @code{<signal.h>}.
359*a9fa9459Szrj 
360*a9fa9459Szrj We return the maximum value that can be used to obtain a meaningful
361*a9fa9459Szrj symbolic name or message.
362*a9fa9459Szrj 
363*a9fa9459Szrj @end deftypefn
364*a9fa9459Szrj 
365*a9fa9459Szrj */
366*a9fa9459Szrj 
367*a9fa9459Szrj int
signo_max(void)368*a9fa9459Szrj signo_max (void)
369*a9fa9459Szrj {
370*a9fa9459Szrj   int maxsize;
371*a9fa9459Szrj 
372*a9fa9459Szrj   if (signal_names == NULL)
373*a9fa9459Szrj     {
374*a9fa9459Szrj       init_signal_tables ();
375*a9fa9459Szrj     }
376*a9fa9459Szrj   maxsize = MAX (sys_nsig, num_signal_names);
377*a9fa9459Szrj   return (maxsize - 1);
378*a9fa9459Szrj }
379*a9fa9459Szrj 
380*a9fa9459Szrj 
381*a9fa9459Szrj /*
382*a9fa9459Szrj 
383*a9fa9459Szrj @deftypefn Supplemental {const char *} strsignal (int @var{signo})
384*a9fa9459Szrj 
385*a9fa9459Szrj Maps an signal number to an signal message string, the contents of
386*a9fa9459Szrj which are implementation defined.  On systems which have the external
387*a9fa9459Szrj variable @code{sys_siglist}, these strings will be the same as the
388*a9fa9459Szrj ones used by @code{psignal()}.
389*a9fa9459Szrj 
390*a9fa9459Szrj If the supplied signal number is within the valid range of indices for
391*a9fa9459Szrj the @code{sys_siglist}, but no message is available for the particular
392*a9fa9459Szrj signal number, then returns the string @samp{Signal @var{num}}, where
393*a9fa9459Szrj @var{num} is the signal number.
394*a9fa9459Szrj 
395*a9fa9459Szrj If the supplied signal number is not a valid index into
396*a9fa9459Szrj @code{sys_siglist}, returns @code{NULL}.
397*a9fa9459Szrj 
398*a9fa9459Szrj The returned string is only guaranteed to be valid only until the next
399*a9fa9459Szrj call to @code{strsignal}.
400*a9fa9459Szrj 
401*a9fa9459Szrj @end deftypefn
402*a9fa9459Szrj 
403*a9fa9459Szrj */
404*a9fa9459Szrj 
405*a9fa9459Szrj #ifndef HAVE_STRSIGNAL
406*a9fa9459Szrj 
407*a9fa9459Szrj char *
strsignal(int signo)408*a9fa9459Szrj strsignal (int signo)
409*a9fa9459Szrj {
410*a9fa9459Szrj   char *msg;
411*a9fa9459Szrj   static char buf[32];
412*a9fa9459Szrj 
413*a9fa9459Szrj #ifndef HAVE_SYS_SIGLIST
414*a9fa9459Szrj 
415*a9fa9459Szrj   if (signal_names == NULL)
416*a9fa9459Szrj     {
417*a9fa9459Szrj       init_signal_tables ();
418*a9fa9459Szrj     }
419*a9fa9459Szrj 
420*a9fa9459Szrj #endif
421*a9fa9459Szrj 
422*a9fa9459Szrj   if ((signo < 0) || (signo >= sys_nsig))
423*a9fa9459Szrj     {
424*a9fa9459Szrj       /* Out of range, just return NULL */
425*a9fa9459Szrj       msg = NULL;
426*a9fa9459Szrj     }
427*a9fa9459Szrj   else if ((sys_siglist == NULL) || (sys_siglist[signo] == NULL))
428*a9fa9459Szrj     {
429*a9fa9459Szrj       /* In range, but no sys_siglist or no entry at this index. */
430*a9fa9459Szrj       sprintf (buf, "Signal %d", signo);
431*a9fa9459Szrj       msg = buf;
432*a9fa9459Szrj     }
433*a9fa9459Szrj   else
434*a9fa9459Szrj     {
435*a9fa9459Szrj       /* In range, and a valid message.  Just return the message.  We
436*a9fa9459Szrj 	 can safely cast away const, since POSIX says the user must
437*a9fa9459Szrj 	 not modify the result.	 */
438*a9fa9459Szrj       msg = (char *) sys_siglist[signo];
439*a9fa9459Szrj     }
440*a9fa9459Szrj 
441*a9fa9459Szrj   return (msg);
442*a9fa9459Szrj }
443*a9fa9459Szrj 
444*a9fa9459Szrj #endif /* ! HAVE_STRSIGNAL */
445*a9fa9459Szrj 
446*a9fa9459Szrj /*
447*a9fa9459Szrj 
448*a9fa9459Szrj @deftypefn Extension {const char*} strsigno (int @var{signo})
449*a9fa9459Szrj 
450*a9fa9459Szrj Given an signal number, returns a pointer to a string containing the
451*a9fa9459Szrj symbolic name of that signal number, as found in @code{<signal.h>}.
452*a9fa9459Szrj 
453*a9fa9459Szrj If the supplied signal number is within the valid range of indices for
454*a9fa9459Szrj symbolic names, but no name is available for the particular signal
455*a9fa9459Szrj number, then returns the string @samp{Signal @var{num}}, where
456*a9fa9459Szrj @var{num} is the signal number.
457*a9fa9459Szrj 
458*a9fa9459Szrj If the supplied signal number is not within the range of valid
459*a9fa9459Szrj indices, then returns @code{NULL}.
460*a9fa9459Szrj 
461*a9fa9459Szrj The contents of the location pointed to are only guaranteed to be
462*a9fa9459Szrj valid until the next call to @code{strsigno}.
463*a9fa9459Szrj 
464*a9fa9459Szrj @end deftypefn
465*a9fa9459Szrj 
466*a9fa9459Szrj */
467*a9fa9459Szrj 
468*a9fa9459Szrj const char *
strsigno(int signo)469*a9fa9459Szrj strsigno (int signo)
470*a9fa9459Szrj {
471*a9fa9459Szrj   const char *name;
472*a9fa9459Szrj   static char buf[32];
473*a9fa9459Szrj 
474*a9fa9459Szrj   if (signal_names == NULL)
475*a9fa9459Szrj     {
476*a9fa9459Szrj       init_signal_tables ();
477*a9fa9459Szrj     }
478*a9fa9459Szrj 
479*a9fa9459Szrj   if ((signo < 0) || (signo >= num_signal_names))
480*a9fa9459Szrj     {
481*a9fa9459Szrj       /* Out of range, just return NULL */
482*a9fa9459Szrj       name = NULL;
483*a9fa9459Szrj     }
484*a9fa9459Szrj   else if ((signal_names == NULL) || (signal_names[signo] == NULL))
485*a9fa9459Szrj     {
486*a9fa9459Szrj       /* In range, but no signal_names or no entry at this index. */
487*a9fa9459Szrj       sprintf (buf, "Signal %d", signo);
488*a9fa9459Szrj       name = (const char *) buf;
489*a9fa9459Szrj     }
490*a9fa9459Szrj   else
491*a9fa9459Szrj     {
492*a9fa9459Szrj       /* In range, and a valid name.  Just return the name. */
493*a9fa9459Szrj       name = signal_names[signo];
494*a9fa9459Szrj     }
495*a9fa9459Szrj 
496*a9fa9459Szrj   return (name);
497*a9fa9459Szrj }
498*a9fa9459Szrj 
499*a9fa9459Szrj 
500*a9fa9459Szrj /*
501*a9fa9459Szrj 
502*a9fa9459Szrj @deftypefn Extension int strtosigno (const char *@var{name})
503*a9fa9459Szrj 
504*a9fa9459Szrj Given the symbolic name of a signal, map it to a signal number.  If no
505*a9fa9459Szrj translation is found, returns 0.
506*a9fa9459Szrj 
507*a9fa9459Szrj @end deftypefn
508*a9fa9459Szrj 
509*a9fa9459Szrj */
510*a9fa9459Szrj 
511*a9fa9459Szrj int
strtosigno(const char * name)512*a9fa9459Szrj strtosigno (const char *name)
513*a9fa9459Szrj {
514*a9fa9459Szrj   int signo = 0;
515*a9fa9459Szrj 
516*a9fa9459Szrj   if (name != NULL)
517*a9fa9459Szrj     {
518*a9fa9459Szrj       if (signal_names == NULL)
519*a9fa9459Szrj 	{
520*a9fa9459Szrj 	  init_signal_tables ();
521*a9fa9459Szrj 	}
522*a9fa9459Szrj       for (signo = 0; signo < num_signal_names; signo++)
523*a9fa9459Szrj 	{
524*a9fa9459Szrj 	  if ((signal_names[signo] != NULL) &&
525*a9fa9459Szrj 	      (strcmp (name, signal_names[signo]) == 0))
526*a9fa9459Szrj 	    {
527*a9fa9459Szrj 	      break;
528*a9fa9459Szrj 	    }
529*a9fa9459Szrj 	}
530*a9fa9459Szrj       if (signo == num_signal_names)
531*a9fa9459Szrj 	{
532*a9fa9459Szrj 	  signo = 0;
533*a9fa9459Szrj 	}
534*a9fa9459Szrj     }
535*a9fa9459Szrj   return (signo);
536*a9fa9459Szrj }
537*a9fa9459Szrj 
538*a9fa9459Szrj 
539*a9fa9459Szrj /*
540*a9fa9459Szrj 
541*a9fa9459Szrj @deftypefn Supplemental void psignal (int @var{signo}, char *@var{message})
542*a9fa9459Szrj 
543*a9fa9459Szrj Print @var{message} to the standard error, followed by a colon,
544*a9fa9459Szrj followed by the description of the signal specified by @var{signo},
545*a9fa9459Szrj followed by a newline.
546*a9fa9459Szrj 
547*a9fa9459Szrj @end deftypefn
548*a9fa9459Szrj 
549*a9fa9459Szrj */
550*a9fa9459Szrj 
551*a9fa9459Szrj #ifndef HAVE_PSIGNAL
552*a9fa9459Szrj 
553*a9fa9459Szrj void
psignal(int signo,char * message)554*a9fa9459Szrj psignal (int signo, char *message)
555*a9fa9459Szrj {
556*a9fa9459Szrj   if (signal_names == NULL)
557*a9fa9459Szrj     {
558*a9fa9459Szrj       init_signal_tables ();
559*a9fa9459Szrj     }
560*a9fa9459Szrj   if ((signo <= 0) || (signo >= sys_nsig))
561*a9fa9459Szrj     {
562*a9fa9459Szrj       fprintf (stderr, "%s: unknown signal\n", message);
563*a9fa9459Szrj     }
564*a9fa9459Szrj   else
565*a9fa9459Szrj     {
566*a9fa9459Szrj       fprintf (stderr, "%s: %s\n", message, sys_siglist[signo]);
567*a9fa9459Szrj     }
568*a9fa9459Szrj }
569*a9fa9459Szrj 
570*a9fa9459Szrj #endif	/* ! HAVE_PSIGNAL */
571*a9fa9459Szrj 
572*a9fa9459Szrj 
573*a9fa9459Szrj /* A simple little main that does nothing but print all the signal translations
574*a9fa9459Szrj    if MAIN is defined and this file is compiled and linked. */
575*a9fa9459Szrj 
576*a9fa9459Szrj #ifdef MAIN
577*a9fa9459Szrj 
578*a9fa9459Szrj #include <stdio.h>
579*a9fa9459Szrj 
580*a9fa9459Szrj int
main(void)581*a9fa9459Szrj main (void)
582*a9fa9459Szrj {
583*a9fa9459Szrj   int signo;
584*a9fa9459Szrj   int maxsigno;
585*a9fa9459Szrj   const char *name;
586*a9fa9459Szrj   const char *msg;
587*a9fa9459Szrj 
588*a9fa9459Szrj   maxsigno = signo_max ();
589*a9fa9459Szrj   printf ("%d entries in names table.\n", num_signal_names);
590*a9fa9459Szrj   printf ("%d entries in messages table.\n", sys_nsig);
591*a9fa9459Szrj   printf ("%d is max useful index.\n", maxsigno);
592*a9fa9459Szrj 
593*a9fa9459Szrj   /* Keep printing values until we get to the end of *both* tables, not
594*a9fa9459Szrj      *either* table.  Note that knowing the maximum useful index does *not*
595*a9fa9459Szrj      relieve us of the responsibility of testing the return pointer for
596*a9fa9459Szrj      NULL. */
597*a9fa9459Szrj 
598*a9fa9459Szrj   for (signo = 0; signo <= maxsigno; signo++)
599*a9fa9459Szrj     {
600*a9fa9459Szrj       name = strsigno (signo);
601*a9fa9459Szrj       name = (name == NULL) ? "<NULL>" : name;
602*a9fa9459Szrj       msg = strsignal (signo);
603*a9fa9459Szrj       msg = (msg == NULL) ? "<NULL>" : msg;
604*a9fa9459Szrj       printf ("%-4d%-18s%s\n", signo, name, msg);
605*a9fa9459Szrj     }
606*a9fa9459Szrj 
607*a9fa9459Szrj   return 0;
608*a9fa9459Szrj }
609*a9fa9459Szrj 
610*a9fa9459Szrj #endif
611