home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / ISSHARE.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  2KB  |  90 lines

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