proper semi-portable use of signal()?

Karl Heuer karl at ima.isc.com
Thu Mar 28 05:48:29 AEST 1991


In article <3223 at charon.cwi.nl> guido at cwi.nl (Guido van Rossum) writes:
>Is the header wrong, is gcc overly worried, or is my code wrong?

All of the above.

The header is wrong, because the Standard specifies that signal handlers take
a single arg of type |int|; the BSDism of pretending they're variadic is not
compatible with existing practice.  (Recommended fix: make your own copy of
<signal.h> that adheres to the standard.  See enclosure.)

GCC is overly worried, but it did restrict it to a warning (rather than a
fatal error), which is appropriate behavior for a compiler when presented with
improper but fixable code.

Your code is wrong because it uses the obsolescent |void (*sigsave)()|
rather than the prototype |void (*sigsave)(int)|.  This should not cause any
problems, though, unless you're using the -Wstrict-prototypes option.

Karl W. Z. Heuer (karl at ima.isc.com or uunet!ima!karl), The Walking Lint
--------cut here--------
/*
 * Karl Heuer, 27-Mar-1991.  Public Domain.
 *
 * For systems whose native <signal.h> is not ANSI-compatible.  Imports most
 * stuff from the native <signal.h>, which we assume to be in /usr/include
 * (but we spell it with a double slash because some compilers warn about
 * including from /usr/include explicitly).  The second paragraph can begin
 * with either an include of <whoami.h>, which is expected to define
 * appropriate symbols in the _[SMCX]_* namespace, or the defines can be
 * inlined at this point.
 */
#if !defined(_H_SIGNAL)
/* some implementations misdeclare signal() */
#define signal __dummy_signal
#define sigvec __dummy_sigvec
#include "/usr//include/signal.h"
#undef signal
#undef sigvec
#define _H_SIGNAL

#include <whoami.h>
#if defined(_S_UNIX_BSD) || defined(_X_HAS_SIGVEC)
struct sigvec {
#if defined(__STDC__)
    void (*sv_handler)(int);
#else
    void (*sv_handler)();
#endif
    int    sv_mask;
    int    sv_flags;
};
#if defined(__STDC__)
extern int sigvec(int, struct sigvec const *, struct sigvec *);
#else
extern int sigvec();
#endif
#endif /* BSD mutation */

#undef SIG_DFL
#undef SIG_IGN
#undef SIG_ERR
#if defined(__STDC__)
#if defined(lint)
#define SIG_DFL         ((void (*)(int))0)
#define SIG_IGN         ((void (*)(int))0)
#define SIG_ERR         ((void (*)(int))0)
#else
#define SIG_DFL         ((void (*)(int))0)
#define SIG_IGN         ((void (*)(int))1)
#define SIG_ERR         ((void (*)(int))-1)
#endif
extern void (*signal(int, void (*)(int)))(int);
extern int    raise(int);
#else
#if defined(lint)
#define SIG_DFL         ((void (*)())0)
#define SIG_IGN         ((void (*)())0)
#define SIG_ERR         ((void (*)())0)
#else
#define SIG_DFL         ((void (*)())0)
#define SIG_IGN         ((void (*)())1)
#define SIG_ERR         ((void (*)())-1)
#endif
extern void (*signal())();
extern int    raise();
#endif
#if !defined(SIGABRT)
#define SIGABRT SIGIOT
#endif
#endif /* include guard */



More information about the Comp.std.c mailing list