summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/SNIP/iswprot.c
blob: 3d2fb6b33c7e4c94b6f6c9266e6bbe146e5550e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
**  ISWPROT.C - Detect if floppy drive is write protected
**
**  public domain by Bob Stout w/ corrections & additions by Wayne King
*/

#include <dos.h>

#ifdef __TURBOC__
 #define FAR far
#else
 #define FAR _far
#endif

/*
**  isWprot()
**
**  Parameters: 1 - Drive number (A: = 0, B: = 1)
**
**  Returns: -1 - Error
**            0 - Not write protected
**            1 write protected
**
**  Note: If drive door is open, an error is returned but the critical
**        error handler is NOT tripped
*/

int isWprot(int drive)
{
      union REGS regs;
      struct SREGS sregs;
      char buf[512], FAR *bufptr = (char FAR *)buf;   /* Needed by MSC  */

      /* First read sector 0  */

      segread(&sregs);
      regs.x.ax = 0x201;
      regs.x.cx = 1;
      regs.x.dx = drive & 0x7f;
      sregs.es  = FP_SEG(bufptr);
      regs.x.bx = FP_OFF(bufptr);
      int86x(0x13, &regs, &regs, &sregs);
      if (regs.x.cflag && regs.h.ah != 6)
      {
            regs.h.ah = 0x00;         /* reset diskette subsystem */
            regs.h.dl = drive & 0x7f;
            int86x(0x13, &regs, &regs, &sregs);
            return -1;
      }

      /* Try to write it back */

      segread(&sregs);
      regs.x.ax = 0x301;
      regs.x.cx = 1;
      regs.x.dx = drive & 0x7f;
      sregs.es  = FP_SEG(bufptr);
      regs.x.bx = FP_OFF(bufptr);
      int86x(0x13, &regs, &regs, &sregs);
      return (3 == regs.h.ah);
}

#ifdef TEST

#include <stdio.h>
#include <ctype.h>

int main(int argc, char *argv[])
{
      int drive;

      if (2 > argc)
      {
            puts("Usage: ISWPROT drive_letter");
            return -1;
      }
      drive = toupper(argv[1][0]) - 'A';
      printf("isWprot(%c:) returned %d\n", drive + 'A', isWprot(drive));
      return 0;
}

#endif