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