home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / SNPD9404.ZIP / ISSHARE.C < prev    next >
C/C++ Source or Header  |  1994-04-03  |  2KB  |  87 lines

  1. /*
  2. ** is_share() - This is a routine that allows a program to determine
  3. **              if file-sharing is enabled at run-time.
  4. **
  5. **              What does this code do?  First - it checks to make sure
  6. **              it is running under DOS 3.0+ - otherwise - no sharing.
  7. **              Next, it opens the program itself (the .EXE file) by using
  8. **              "argv[0]".  This argument points to the actual program name
  9. **              complete with the path under DOS 3.0 or later.  It then
  10. **              attempts to lock the first 500 bytes of the program on
  11. **              disk.  If successful (i.e. return != -1), it unlocks the
  12. **              locked bytes and closes the file (actually the unlock is
  13. **              superfluous since closing the file releases all locks) and
  14. **              returns the a "TRUE" (1) result.  If it fails, it closes
  15. **              the .EXE file and returns a "FALSE" (0) result.  Note that
  16. **              this does not depend on opening a file in shared mode to
  17. **              test it.
  18. **
  19. ** Example of usage:
  20. **
  21. ** main(int argc, char *argv[])
  22. ** {
  23. **   int sharing;
  24. **
  25. **   sharing = is_share(argv[0]);
  26. **   .
  27. **   .
  28. **   if (sharing)
  29. **   {
  30. **     // open file in shared mode
  31. **     ...
  32. **   }
  33. **   else
  34. **   {
  35. **     // use "normal" open
  36. **     ...
  37. **   }
  38. ** }
  39. **
  40. ** Revision History:
  41. **
  42. ** 08/03/93  Original:  "is_sharing()" by Mike Ratledge of fidonet
  43. ** 10/20/93  Revision:  revised for library
  44. ** 04/03/94  Revision:  "Portabalized" for SNIPPETS by Bob Stout
  45. */
  46.  
  47. #include <stdio.h>
  48. #include <io.h>
  49. #include <dos.h>
  50.  
  51. #if defined(_MSC_VER)
  52.  #include <stdlib.h>
  53.  #include <sys\locking.h>
  54.  
  55.  int lock(int fp, long ofs, long lng)
  56.  {
  57.        lseek(fp,0L,SEEK_SET);
  58.        return locking(fp,LK_LOCK,lng);
  59.  }
  60.  
  61.  int unlock(fp,ofs,lng)
  62.  {
  63.        lseek(fp,0L,SEEK_SET);
  64.        return locking(fp,LK_UNLCK,lng);
  65.  }
  66. #endif
  67.  
  68. int is_share(char *arg)
  69. {
  70.       FILE *exe;
  71.  
  72.       if (_osmajor < 3)
  73.             return(0);
  74.  
  75.       exe = fopen(arg, "rb");
  76.  
  77.       if (0 == lock(fileno(exe), 0l, 500l))
  78.       {
  79.             unlock(fileno(exe), 0l, 500l);
  80.             fclose(exe);
  81.             return(1);
  82.       }
  83.  
  84.       fclose(exe);
  85.       return(0);
  86. }
  87.