home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / lan / semaph / sema.c < prev    next >
Text File  |  1988-02-09  |  4KB  |  145 lines

  1. /*
  2. ** SEMA.C  -  Novell NetWare Semaphore Function Call's
  3. **
  4. ** Written by:     Tom Scribner
  5. ** Date:           November 18, 1987
  6. ** 
  7. ** Description:    These functions provide a application to use
  8. **                 NetWare's semaphore's for various uses.  
  9. **                 Included are open, examine, and close a semaphore.
  10. **                 See SEMA.DOC for a more complete description of
  11. **                 these functions.
  12. **
  13. ** History:
  14. **    11/18/87  tes   Initial Release
  15. **    02/09/87  tes   Released to public domain
  16. **
  17. */
  18.  
  19. #include <dos.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <math.h>
  23.  
  24. #include "sema.h"
  25.  
  26. #define  SEMA_CALL            0xC5
  27. #define  OPEN_SEMAPHORE       0x00
  28. #define  EXAMINE_SEMAPHORE    0x01
  29. #define  CLOSE_SEMAPHORE      0x04
  30.  
  31.  
  32. #define MAKELONG(a,b)  \
  33.     ( (long) ( ((unsigned) a) | ((unsigned long)((unsigned)b)) << 16))
  34. #define HIBYTE(a)  ( (unsigned)(a) )
  35. #define LOBYTE(a)  ( (unsigned)( a >> 16 ) )
  36.  
  37.  
  38. /* --------------------------------------------------------------- */
  39. /*                O P E N   A   S E M A P H O R E                  */
  40. /* --------------------------------------------------------------- */
  41. long
  42. #ifdef MSC5
  43. OpenSemaphore( char *SName, unsigned int InitVal, int *OpenCount )
  44. #else
  45. OpenSemaphore( SName, InitVal, OpenCount )
  46. char *SName;
  47. unsigned int InitVal;
  48. int *OpenCount;
  49. #endif
  50. {
  51.    union REGS inregs,outregs;
  52.    struct SREGS segregs;
  53.    char buffer[127];
  54.    char far *lpBuffer = buffer;
  55.    long sema_handle;
  56.    int i = 0;
  57.  
  58.    inregs.h.ah = SEMA_CALL;
  59.    inregs.h.al = OPEN_SEMAPHORE;
  60.  
  61.    /* first char in buffer contains the length of the semaphore */
  62.    buffer[i++] = (char)strlen(SName);
  63.    while(*SName)
  64.       buffer[i++] = *SName++;
  65.  
  66.    segregs.ds = FP_SEG(lpBuffer);
  67.    inregs.x.dx = FP_OFF(lpBuffer);
  68.  
  69.    inregs.h.ch = (unsigned char )InitVal;
  70.  
  71.    intdosx(&inregs,&outregs,&segregs);
  72.    if( outregs.h.al == 0){
  73.       sema_handle = MAKELONG(outregs.x.cx,outregs.x.dx);
  74.       *OpenCount = outregs.h.bl;
  75.    }else{
  76.       sema_handle = (long)ERROR;
  77.       *OpenCount = 0;
  78.       NetError = outregs.h.al;
  79.    }
  80.    return(sema_handle);
  81. }
  82.  
  83. /* --------------------------------------------------------------- */
  84. /*            E X A M I N E   A   S E M A P H O R E                */
  85. /* --------------------------------------------------------------- */
  86. int
  87. #ifdef MSC5
  88. ExamineSemaphore( long handle )
  89. #else
  90. ExamineSemaphore( handle )
  91. long handle;
  92. #endif
  93. {
  94.    union REGS inregs,outregs;
  95.    int ret_val;
  96.  
  97.    inregs.h.ah = SEMA_CALL;
  98.    inregs.h.al = EXAMINE_SEMAPHORE;
  99.    inregs.x.cx = HIBYTE( handle );
  100.    inregs.x.dx = LOBYTE( handle );
  101.    intdos(&inregs,&outregs);
  102.    if( outregs.h.al == 0){
  103.       NetError = 0;
  104.       ret_val = outregs.h.dl;
  105.    }else{
  106.       NetError = outregs.h.al;
  107.       ret_val = ERROR;
  108.    }
  109.    return( ret_val );
  110. }
  111.  
  112.       
  113.  
  114. /* --------------------------------------------------------------- */
  115. /*             C L O S E   A   S E M A P H O R E                   */
  116. /* --------------------------------------------------------------- */
  117. int
  118. #ifdef MSC5
  119. CloseSemaphore( long handle )
  120. #else
  121. CloseSemaphore( handle )
  122. long handle;
  123. #endif
  124.  
  125. {
  126.    union REGS inregs,outregs;
  127.    int ret_val;
  128.  
  129.    inregs.h.ah = SEMA_CALL;
  130.    inregs.h.al = CLOSE_SEMAPHORE;
  131.    inregs.x.cx = HIBYTE( handle );
  132.    inregs.x.dx = HIBYTE( handle );
  133.    intdos(&inregs,&outregs);
  134.    if( outregs.h.al != 0){
  135.       NetError = outregs.h.al;
  136.       ret_val = ERROR;
  137.    }else{
  138.       NetError = 0;
  139.       ret_val = (int)outregs.h.al;
  140.    }
  141.    return( ret_val );
  142. }
  143.  
  144.       
  145.