home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / s / snip1292.zip / SHARING.TXT < prev    next >
Internet Message Format  |  1990-09-03  |  2KB

  1. From:    Mike Ratledge 
  2. To:      All                                      Msg #245, 03-Aug-88 12:45.00
  3. Subject: File sharing enabled test
  4.  
  5. Someone asked the other day about an easy way to determine if file-sharing
  6. is enabled at program run-time.  I use the following code in all of my
  7. Turbo C program to do just that:
  8.  
  9. #define TRUE 1
  10. #define FALSE 0
  11.  
  12. int sharing;
  13.  
  14.  
  15. main (int argc, char *argv[])
  16.  
  17. {
  18.   sharing = is_sharing(argv[0]);
  19.   .
  20.   .
  21.   .
  22.   if (sharing)
  23.   {
  24.     /* open file in shared mode */
  25.     ...
  26.   }
  27.   else
  28.   {
  29.     /* use "normal" open */
  30.     ...
  31.   }
  32. }
  33.  
  34.  
  35. int is_sharing(char *arg)
  36.  
  37. {
  38.  FILE *exe;
  39.  
  40.   if (_osmajor < 3)
  41.     return(FALSE);
  42.   exe = fopen(arg, "rb");
  43.   ii = lock(fileno(exe), 0l, 500l);
  44.   if (ii != -1)
  45.   {
  46.     ii = unlock(fileno(exe), 0l, 500l);
  47.     fclose(exe);
  48.     return(TRUE);
  49.   }
  50.   fclose(exe);
  51.   return(FALSE);
  52. }
  53.  
  54. What does this code do?  First - it checks to make sure it's running under
  55. DOS 3.0+ - if not - no sharing.  Next - it opens the program itself (the
  56. .EXE file) by using "argv[0]", which points to the actual program name
  57. complete with the path under DOS 3.0 or later.  It then attempts to lock
  58. the first 500 bytes of the program on disk, and if successful (i.e. return
  59. != -1) it unlocks the same bytes and closes the file (actually - the unlock
  60. is superfluous, since closing the file releases all locks) and returns the
  61. "TRUE" result.  If it fails - it closes the .EXE file and returns FALSE.
  62. Note that this does not depend on opening a file in shared mode to test it.
  63.  
  64. Note that this code must be modified slightly to be useful for MicroSoft
  65. C, since they use the "locking" procedure for both lock & unlock.  You
  66. also have to "rewind" before the unlock, since M/S C works from the
  67. current file-pointer forward.  I could post both - but I'm sure all you
  68. C-jockeys out there know what I'm talking about if it concerns you (i.e.
  69. you're using M/S C instead of Turbo).  I also have this coded in Turbo
  70. Pascal if anyone needs it...
  71.