home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / os / os2 / programm / 4519 < prev    next >
Encoding:
Text File  |  1992-08-29  |  3.1 KB  |  130 lines

  1. Path: sparky!uunet!dtix!darwin.sura.net!sgiblab!tsoft!dennis
  2. From: bbs.dennis@tsoft.sf-bay.org (Dennis Yelle)
  3. Newsgroups: comp.os.os2.programmer
  4. Subject: How can I read the MBR (Master Boot Record) under OS/2?
  5. Message-ID: <N2c9PB2w165w@tsoft.sf-bay.org>
  6. Date: 28 Aug 92 18:54:34 GMT
  7. Sender: bbs@tsoft.sf-bay.org (BBS User)
  8. Organization: The TSoft BBS and Public Access Unix, +1 415 969 8238
  9. Lines: 119
  10.  
  11. #if 0
  12. I am trying to read the Master Boot Record from my C: drive
  13. under OS/2.
  14.  
  15. When I compile the following program with:
  16.     icc getmbr.c
  17. And then run it with:
  18.     getmbr z
  19. I get this:
  20.     GETMBR
  21.     Got handle 5.
  22.     Sorry, the error code is 4400-19
  23. Does anyone know what I am doing wrong?
  24. Can anyone help me?
  25. #endif
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #define INCL_DOSDEVICES
  31. #include <os2.h>
  32.  
  33. char mbr_buf[ 512];
  34.  
  35. void internal_error_x( int code, long rc)
  36. {
  37.     printf( "Sorry, the error code is %d-%ld\n", code, rc);
  38.     exit(99);
  39. }
  40.  
  41. int get_pd_handle( int disk, HFILE *handle)
  42. {
  43.     char cdisk[3];
  44.     APIRET rc;
  45.     
  46.     cdisk[0] = disk; cdisk[1] = ':'; cdisk[2] = 0;
  47.     *handle = 0;
  48.     rc = DosPhysicalDisk( INFO_GETIOCTLHANDLE,
  49.                           handle,
  50.                           2, /* sizeof( *handle), */
  51.                           cdisk,
  52.                           sizeof( cdisk) );
  53.     if ( rc)
  54.         internal_error_x( 1234, rc);
  55.     return 0;
  56. }
  57. int free_pd_handle( HFILE handle)
  58. {
  59.     APIRET rc;
  60.     
  61.     rc = DosPhysicalDisk( INFO_FREEIOCTLHANDLE, 0, 0,
  62.                           &handle, 2 /* sizeof(handle) */ );
  63.     if ( rc)
  64.         internal_error_x( 1245, rc);
  65.     return 0;
  66. }
  67.  
  68. typedef unsigned short WORD;  /* long didn't work, so... */
  69.  
  70. typedef struct pp_s {
  71.     BYTE command;         /* 1 */
  72.     WORD head;            /* 0 */
  73.     WORD cyl;             /* 0 */
  74.     WORD first_sector;    /* 1 */
  75.     WORD num_sectors;     /* 1 */
  76.         WORD sector_num;  /* 1 */
  77.         WORD sector_size; /* 512 */
  78. } pp_t;
  79.  
  80. void read_mbr( int disk, void *p_buf, ULONG p_buf_len)
  81. {
  82.     HFILE handle;
  83.     APIRET rc;
  84.     pp_t pp = { 1, 0, 0, 1, 1, 1, 512};
  85.     ULONG pp_len = sizeof( pp);
  86.     ULONG  b_len = 0 /* p_buf_len */;
  87.  
  88.     memset( p_buf, 0, p_buf_len);
  89.  
  90.     get_pd_handle( disk, &handle);
  91.  
  92.     printf( "Got handle %ld.\n", (long)handle);
  93.  
  94.     rc = DosDevIOCtl( handle, 9, 0x64 /* read */,
  95.                       &pp, sizeof(pp), &pp_len,
  96.                       p_buf, p_buf_len, &b_len);
  97.     if ( rc)
  98.         internal_error_x( 4400, rc);
  99.     
  100.     free_pd_handle( handle);
  101. }
  102.  
  103. main( int argc, char **argv)
  104. {
  105.     char *name;
  106.     FILE *out;
  107.     int len;
  108.     
  109.     printf( "GETMBR\n");
  110.     if ( argc != 2 )
  111.         internal_error_x( 5555, argc);
  112.     name = argv[1];
  113.     out = fopen( name, "wb");
  114.     if ( ! out ) {
  115.         printf( "Sorry, I can't write to the file %s\n", name);
  116.         exit(9);
  117.     }
  118.     read_mbr( '1', mbr_buf, sizeof( mbr_buf) );
  119.     len = fwrite( mbr_buf, 1, 512, out);
  120.     if ( len != 512 ) {
  121.         printf( "Sorry, I can't write to the file %s\n", name);
  122.         printf( "Disk full?\n");
  123.         exit(9);
  124.     }
  125.     return 0;
  126. }
  127.  
  128. --
  129. Dennis Yelle (bbs.dennis@tsoft.sf-bay.org)
  130.