home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_08_04 / 8n04086a < prev    next >
Text File  |  1990-03-20  |  2KB  |  71 lines

  1. *****Listing 2*****
  2.  
  3. semaphor.c
  4.  
  5. /* Supported operating systems:  UNIX, MS-DOS */
  6. #define UNIX    (1)
  7. #define MSDOS    (2)
  8. #define HOST    UNIX
  9.  
  10. #include <errno.h>
  11. #include <limits.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #if HOST == UNIX
  15. #define PATHDLM    ('/')    /* path name delimiter */
  16. #include <fcntl.h>    /* open() macro definitions */
  17. #include "syscalkr.h"    /* system call declarations */
  18. #include <sys/stat.h>    /* file mode macros */
  19. #elif HOST == MSDOS
  20. #define PATHDLM    ('\\')    /* path name delimiter */
  21. #include <fcntl.h>    /* open() macro definitions */
  22. #include <io.h>        /* close(), open() declarations */
  23. #include <sys/types.h>
  24. #include <sys/stat.h>    /* file mode macros */
  25. #endif
  26. #include "semaphor.h"
  27.  
  28. /* semaphore set table definition */
  29. static semset_t sst[SEMOPEN_MAX];
  30.  
  31. /* semlower:  lower semaphore */
  32. int semlower(semset_t *ssp, int semno)
  33. {
  34.     char path[PATH_MAX + 1];
  35.  
  36.     /* remove semaphore file */
  37.     sprintf(path, "%s%cs%d", ssp->semdir, (int)PATHDLM, semno);
  38.     if (unlink(path) == -1) {
  39.         if (errno == ENOENT) errno = EAGAIN;
  40.         return -1;
  41.     }
  42.  
  43.     errno = 0;
  44.     return 0;
  45. }
  46.  
  47. /* semraise:  raise semaphore */
  48. int semraise(semset_t *ssp, int semno)
  49. {
  50.     char path[PATH_MAX + 1];
  51.     int fd = 0;
  52.  
  53.     /* create semaphore file */
  54.     sprintf(path, "%s%cs%d", ssp->semdir, (int)PATHDLM, semno);
  55. #if HOST == UNIX
  56.     fd = open(path, O_WRONLY | O_CREAT, 0);
  57. #elif HOST == MSDOS
  58.     fd = open(path, O_WRONLY | O_CREAT, S_IREAD | S_IWRITE);
  59. #endif
  60.     if (fd == -1) {
  61.         return -1;
  62.     }
  63.     if (close(fd) == -1) {
  64.         return -1;
  65.     }
  66.  
  67.     errno = 0;
  68.     return 0;
  69. }
  70.  
  71.