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

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  DISK_SN.C - Retrieve a disk serial number
  5. **
  6. **  public domain demo by Paul Schlyter and Bob Stout
  7. **  OS/2 compatibility added by Mark Kimes
  8. */
  9.  
  10. #include <stdio.h>
  11.  
  12. #ifdef OS2
  13.  #define INCL_DOS
  14.  
  15.  #include <os2.h>
  16.  #include <stdlib.h>
  17. #else
  18.  #include <dos.h>
  19. #endif
  20.  
  21. #include "extkword.h"
  22. #include "disk_sn.h"
  23. #include "mk_fp.h"
  24.  
  25. /*
  26. **  Arguments: 1 - Drive (0 = default, 1 = A:, 2 = B:, etc.)
  27. **
  28. **  Returns: Static string containing the disk serial number, or
  29. **           an empty string if no serial number is available,
  30. **
  31. **  Notes: Drive errors result in an empty string returned, this function
  32. **         does not invoke the critical error handler.
  33. */
  34.  
  35. char *get_disk_id(int drive)
  36. {
  37.       static char serial_no[10];
  38. #ifndef OS2
  39.       union REGS r;
  40.       struct SREGS s;
  41.       unsigned sno1, sno2;
  42.  
  43.       r.x.ax = 0x6900;
  44.       r.h.bl = drive;
  45.       segread(&s);
  46.       intdosx(&r, &r, &s);
  47.       if (r.x.cflag)
  48.             *serial_no = '\0';
  49.       else
  50.       {
  51.             sno2 = *((unsigned FAR *)MK_FP(s.ds, r.x.dx+2));
  52.             sno1 = *((unsigned FAR *)MK_FP(s.ds, r.x.dx+4));
  53.             sprintf(serial_no, "%04X-%04X\n", sno1, sno2);
  54.       }
  55. #else
  56.       struct {
  57.             ULONG serial;
  58.             char  volumelength;
  59.             char  volumelabel[CCHMAXPATH];
  60.       } volser;
  61.  
  62.       DosError(FERR_DISABLEHARDERR);
  63.       if(0 == DosQueryFSInfo(drive, FSIL_VOLSER, &volser, sizeof(volser)))
  64.             sprintf(serial_no, "%04X-%04X", HIUSHORT(volser.serial),
  65.                   LOUSHORT(volser.serial));
  66.       else  *serial_no = 0;
  67. #endif
  68.       return serial_no;
  69. }
  70.  
  71. #ifdef TEST
  72.  
  73. #include <ctype.h>
  74.  
  75. main(int argc, char *argv[])
  76. {
  77.       char *sn;
  78.  
  79.       if (2 > argc)
  80.       {
  81.             puts("Usage: DISK_SN drive_letter [...drive_letter]");
  82.             return -1;
  83.       }
  84.       while (--argc)
  85.       {
  86.             int drive;
  87.  
  88.             drive = toupper(**++argv);
  89.             sn = get_disk_id(drive - '@');
  90.             printf("The serial number of %c: is %s\n", drive, sn);
  91.       }
  92.       return 0;
  93. }
  94.  
  95. #endif /* TEST */
  96.