ptg10805159
Section 10.11Signal Sets 345
Using this implementation,sigaddsetturns on a single bit andsigdelsetturns
offasingle bit;sigismembertests a certain bit. Since no signal is ever numbered 0, we
subtract 1 from the signal number to obtain the bit to manipulate. Figure10.12 shows
implementations of these functions.
#include <signal.h>
#include <errno.h>
/*
*<signal.h> usually defines NSIG to include signal number 0.
*/
#define SIGBAD(signo) ((signo) <= 0 || (signo) >= NSIG)
int
sigaddset(sigset_t *set, int signo)
{
if (SIGBAD(signo)) {
errno = EINVAL;
return(-1);
}
*set |= 1 << (signo - 1); /* turn bit on */
return(0);
}
int
sigdelset(sigset_t *set, int signo)
{
if (SIGBAD(signo)) {
errno = EINVAL;
return(-1);
}
*set &= ̃(1 << (signo - 1)); /* turn bit off */
return(0);
}
int
sigismember(const sigset_t *set, int signo)
{
if (SIGBAD(signo)) {
errno = EINVAL;
return(-1);
}
return((*set & (1 << (signo - 1))) != 0);
}
Figure 10.12 An implementation ofsigaddset,sigdelset,andsigismember
We might be tempted to implement these three functions as one-line macros in the
<signal.h>header,but POSIX.1 requires us to check the signal number argument for
validity and to seterrnoif it is invalid. This is moredifficult to do in a macrothan in a
function.