home *** CD-ROM | disk | FTP | other *** search
/ Media Share 13 / mediashare_13.zip / mediashare_13 / ZIPPED / PROGRAM / SNIP9404.ZIP / ISWPROT.C < prev    next >
C/C++ Source or Header  |  1994-04-03  |  2KB  |  83 lines

  1. /*
  2. **  ISWPROT.C - Detect if floppy drive is write protected
  3. **
  4. **  public domain by Bob Stout w/ corrections & additions by Wayne King
  5. */
  6.  
  7. #include <dos.h>
  8.  
  9. #ifdef __TURBOC__
  10.  #define FAR far
  11. #else
  12.  #define FAR _far
  13. #endif
  14.  
  15. /*
  16. **  isWprot()
  17. **
  18. **  Parameters: 1 - Drive number (A: = 0, B: = 1)
  19. **
  20. **  Returns: -1 - Error
  21. **            0 - Not write protected
  22. **            1 write protected
  23. **
  24. **  Note: If drive door is open, an error is returned but the critical
  25. **        error handler is NOT tripped
  26. */
  27.  
  28. int isWprot(int drive)
  29. {
  30.       union REGS regs;
  31.       struct SREGS sregs;
  32.       char buf[512], FAR *bufptr = (char FAR *)buf;   /* Needed by MSC  */
  33.  
  34.       /* First read sector 0  */
  35.  
  36.       segread(&sregs);
  37.       regs.x.ax = 0x201;
  38.       regs.x.cx = 1;
  39.       regs.x.dx = drive & 0x7f;
  40.       sregs.es  = FP_SEG(bufptr);
  41.       regs.x.bx = FP_OFF(bufptr);
  42.       int86x(0x13, ®s, ®s, &sregs);
  43.       if (regs.x.cflag && regs.h.ah != 6)
  44.       {
  45.             regs.h.ah = 0x00;         /* reset diskette subsystem */
  46.             regs.h.dl = drive & 0x7f;
  47.             int86x(0x13, ®s, ®s, &sregs);
  48.             return -1;
  49.       }
  50.  
  51.       /* Try to write it back */
  52.  
  53.       segread(&sregs);
  54.       regs.x.ax = 0x301;
  55.       regs.x.cx = 1;
  56.       regs.x.dx = drive & 0x7f;
  57.       sregs.es  = FP_SEG(bufptr);
  58.       regs.x.bx = FP_OFF(bufptr);
  59.       int86x(0x13, ®s, ®s, &sregs);
  60.       return (3 == regs.h.ah);
  61. }
  62.  
  63. #ifdef TEST
  64.  
  65. #include <stdio.h>
  66. #include <ctype.h>
  67.  
  68. int main(int argc, char *argv[])
  69. {
  70.       int drive;
  71.  
  72.       if (2 > argc)
  73.       {
  74.             puts("Usage: ISWPROT drive_letter");
  75.             return -1;
  76.       }
  77.       drive = toupper(argv[1][0]) - 'A';
  78.       printf("isWprot(%c:) returned %d\n", drive + 'A', isWprot(drive));
  79.       return 0;
  80. }
  81.  
  82. #endif
  83.