Signal handler declaration confusion - help!

  • Thread starter Charles Sullivan
  • Start date
C

Charles Sullivan

I have existing code in which the signal() and signal
handler functions appear (for example) like this:

----------------------
#include <signal.h>
void my_handler(int signo);
signal(SIGALRM, my_handler);
---------------------

It appears to work on all Unix-like platforms reported on
(including Linux, various BSDs, Mac OSX, SYSV, AT&TSysVr4).
However on AT&TSysVr4 there are compiler warnings for the
signal() statements:
"warning: argument is incompatible with prototype: arg #2"

According to the signal man page (which I received from a
user of this OS):

------------------
#include <signal.h>
void (*signal(int sig, void (*disp)(int)))(int);
-----------------

I thought I would just change the declaration of the
signal handler to match the OS (ATTSYSVR4 is defined at
configuration) like this:

-------------------
#include <signal.h>

#ifdef ATTSYSVR4
typedef void (*disp)(int) SIGHANDLER;
#else
typedef void SIGHANDLER;
#endif

SIGHANDLER my_handler(int signo);
------------------

Does that look correct? I don't have access to a
computer running this OS and can't try it myself.

Thanks for your help.

Regards,
Charles Sullivan
 
D

Dave Thompson

On Tue, 05 Sep 2006 20:32:02 GMT, Charles Sullivan

According to the signal man page (which I received from a
user of this OS):

------------------
#include <signal.h>
void (*signal(int sig, void (*disp)(int)))(int);
-----------------

I thought I would just change the declaration of the
signal handler to match the OS (ATTSYSVR4 is defined at
configuration) like this:

-------------------
#include <signal.h>

#ifdef ATTSYSVR4
typedef void (*disp)(int) SIGHANDLER;
#else
typedef void SIGHANDLER;
#endif

SIGHANDLER my_handler(int signo);
No. Your first new typedef is illegal syntax (any OS).

To create a typedef for the same type as the second parameter to
signal shown would be:
typedef void (*SIGHANDLER) (int);

But that is not the type your handler should have; you want the actual
function type which _decays_ to that pointer type:
typedef void SIGHANDLER (int);
...
SIGHANDLER my_handler;
and that is equivalent to what you already have, which is also the
type defined by the C standard; so this isn't the error, or the fix.

Perhaps your user can get you a copy of the (relevant parts of) their
<signal.h> (and anything else it uses), or someone else who uses this
system can do so, or someone who knows about it can tell you, since
clearly it is looking for something different than your user has
gotten from the man page and the standard specifies. Especially watch
out for any possibility that their implementation of the standard
conditionally compiles different declarations depending on the
environment settings, as many do, that you look at the one in use --
or conversely, you find an environment that can be selected that
provides the standard and preferred type.

Or perhaps just possibly the compiler in use on that system is so old
it doesn't automatically decay functions as required by C89. If so,
you could use
signal (SIGALRM, &my_handler) /* note & */
and that will also work fine on any standard system.

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top