www.digitalmars.com         C & C++   DMDScript  

D - Calling Linux System Functions - Need help with C to D declarations

reply Simon J Mackenzie <project.d smackoz.fastmail.fm> writes:
I need help trying to sort out a declaration for a system call to Linux 
but I'm not sure how to handle the situation where the function can 
return either a function pointer or a SIG_ERR value, which happens to be -1.

The man page for the "signal" system call provides the following 
information...

  #include <signal.h>
  typedef void (*sighandler_t)(int);
  sighandler_t signal(int signum, sighandler_t handler);

RETURN VALUE
The signal() function returns the previous value of the signal handler,
or SIG_ERR on error.

The following is my attempt at a solution which works fine if I don't use the enum type SIG_ERR to check for a return error condition from signal(...) typedef void function(int) sighandler_t; enum : sighandler_t { SIG_ERR = (sighandler_t)-1, /* Error return. */ SIG_DFL = (sighandler_t)0, /* Default action. */ SIG_IGN = (sighandler_t)1 /* Ignore signal. */ } extern (C) { void function(int) signal(int signum, void function(int) sighandler_t); } With the above declarations the following code fragment seems to work as expected. signal(SIGINT, exit_program); where the function exit_program(int) is declared as... extern (C) { void exit_program(int signum) { // some code } } but the next code fragment generates a compiler error... sighandler_t rval; rval = signal(SIGINT, exit_program); main.d(50): cannot implicitly convert void(C *)(int) to sighandler_t but I thought the typedef gave me a type sighandler_t of void(C *)(int) or have I totally missed something? Any suggestions 'cause I'm really not sure what I'm doing here! Simon J Mackenzie
Jul 19 2003
parent "Walter" <walter digitalmars.com> writes:
Need to put the typedef for sighandler_t inside an extern (C).

"Simon J Mackenzie" <project.d smackoz.fastmail.fm> wrote in message
news:bfbt85$3094$1 digitaldaemon.com...
 I need help trying to sort out a declaration for a system call to Linux
 but I'm not sure how to handle the situation where the function can
 return either a function pointer or a SIG_ERR value, which happens to

 The man page for the "signal" system call provides the following
 information...

  >  #include <signal.h>
  >  typedef void (*sighandler_t)(int);
  >  sighandler_t signal(int signum, sighandler_t handler);
 ...
  >RETURN VALUE
  >The signal() function returns the previous value of the signal handler,
  >or SIG_ERR on error.

 The following is my attempt at a solution which works fine if I don't
 use the enum type SIG_ERR to check for a return error condition from
 signal(...)

 typedef void function(int) sighandler_t;
 enum : sighandler_t
 {
    SIG_ERR = (sighandler_t)-1, /* Error return.   */
    SIG_DFL = (sighandler_t)0,  /* Default action. */
    SIG_IGN = (sighandler_t)1   /* Ignore signal.  */
 }
 extern (C)
 {
   void function(int) signal(int signum, void function(int) sighandler_t);
 }

 With the above declarations the following code fragment seems to work as
 expected.
 signal(SIGINT, exit_program);

 where the function exit_program(int) is declared as...
 extern (C)
 {
     void exit_program(int signum)
     {
        // some code
     }
 }

 but the next code fragment generates a compiler error...

 sighandler_t rval;
 rval = signal(SIGINT, exit_program);

 main.d(50): cannot implicitly convert void(C *)(int) to sighandler_t

 but I thought the typedef gave me a type sighandler_t of void(C *)(int)
 or have I totally missed something?

 Any suggestions 'cause I'm really not sure what I'm doing here!

 Simon J Mackenzie

Jul 22 2003