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

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