home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / std_unix / volume.20 / text0021.txt < prev    next >
Encoding:
Internet Message Format  |  1990-08-02  |  3.4 KB

  1. From:  Doug Gwyn <gwyn@smoke.brl.mil>
  2.  
  3. In article <711@longway.TIC.COM> Andy Tanenbaum <ast@cs.vu.nl> writes:
  4. >We have gone through this before, but I still haven't gotten an unambiguous
  5. >answer.  Let me try a specific example to make it clearer.
  6.  
  7. I'm sure I've given unambiguous answers.
  8. The rules are really quite clear and simple.
  9.  
  10. >Suppose you want to write a routine raise() that is ANSI conformant and also
  11. >POSIX conformant.  Raise calls the POSIX kill function, so it includes
  12. ><signal.h>.  The <signal.h> header contains the prototype for kill(), which
  13. >uses pid_t, a type defined in <sys/types.h>.  POSIX mandates <sys/types.h>
  14. >but ANSI does not require it.  Thus it is probably not ANSI-legal to put
  15. >#include <sys/types.h>
  16. >in raise(), since an ANSI routine cannot depend on something that ANSI
  17. >doesn't require.  The raise routine would not be portable then, and maybe
  18. >not even conformant.  On the other hand, putting the #include in <signal.h>
  19. >probably isn't legal either, and certainly not portable to non POSIX
  20. >systems.  
  21. >The question is: it has to go somewhere, but where?  I have thought about
  22. >putting it in <signal.h> but protected by #ifdef _POSIX_SOURCE, just as
  23. >the prototype for kill is.  However, our earlier discussion in this group
  24. >suggested that this wasn't legal either.  Does anybody know what the story
  25. >is?
  26.  
  27. The above description makes no sense.  The run-time code for raise
  28. does not "include <signal.h>".  In fact, it must not even (in a
  29. conventional linkage environment) invoke the POSIX kill() function,
  30. because the external identifier "kill" is not in the name space
  31. reserved for ANSI C implementations (and thus is reserved for use
  32. by programs, with perhaps totally non-POSIX meaning).  It could,
  33. however, invoke a function named "_kill" or some other such name
  34. reserved for use by implementations.
  35.  
  36. <signal.h> must not define extra garbage such as the kill() interface
  37. or the POSIX *_t typedefs, except under control of some "feature test
  38. macro" such as _POSIX_SOURCE.  In the case of the typedefs, I would
  39. urge that they not be defined as a side effect of #including <signal.h>
  40. even in the presence of _POSIX_SOURCE.  You don't need pid_t to properly
  41. declare getpid() or kill() in an implementation; instead, simply use the
  42. equivalent type derived from basic types (in this case, probably "int").
  43. Note that this is essential also for the ANSI C declaration of vprintf()
  44. in <stdio.h>, since the va_* identifiers must NOT be defined as a side
  45. effect of #including <stdio.h>, so the implementation of <stdio.h> must
  46. not #include <stdarg.h>.
  47.  
  48. On the other hand, the implementation of an ANSI C library routine can
  49. certainly depend on things not mentioned in the C standard, such as
  50. for example _kill() and <sys/types.h>.  The following is valid source
  51. code to implement ANSI C conformant raise() in a hypothetical POSIX
  52. implementation:
  53.  
  54. /*
  55.     raise() -- send a signal to the current process
  56.  
  57.     public-domain implementation
  58.  
  59.     last edit:    16-Jan-1990    Gwyn@BRL.MIL
  60.  
  61.     complies with the following standards:
  62.         ANSI X3.159-1989
  63.         IEEE Std 1003.1-1988
  64.         SVID Issue 3
  65.  */
  66. #define    getpid    _getpid            /* required support for ANSI C */
  67. #define    kill    _kill            /* required support for ANSI C */
  68.  
  69. #include    <sys/types.h>        /* for pid_t */
  70.  
  71. extern pid_t    getpid();        /* UNIX/POSIX system call */
  72. extern int    kill();            /* UNIX/POSIX system call */
  73.  
  74. int
  75. raise(sig)
  76.     int    sig;
  77.     {
  78.     return kill(getpid(), sig);    /* set errno to EINVAL if sig invalid */
  79.     }
  80.  
  81. Volume-Number: Volume 20, Number 21
  82.  
  83.