home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / drive.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  4KB  |  191 lines

  1. /***
  2. *drive.c - get and change current drive
  3. *
  4. *       Copyright (c) 1989-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       This file has the _getdrive() and _chdrive() functions
  8. *
  9. *******************************************************************************/
  10.  
  11. #ifndef _MAC
  12.  
  13. #include <cruntime.h>
  14. #include <oscalls.h>
  15. #include <mtdll.h>
  16. #include <internal.h>
  17. #include <msdos.h>
  18. #include <errno.h>
  19. #include <ctype.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22.  
  23.  
  24. /***
  25. *int _getdrive() - get current drive (1=A:, 2=B:, etc.)
  26. *
  27. *Purpose:
  28. *       Returns the current disk drive
  29. *
  30. *Entry:
  31. *       No parameters.
  32. *
  33. *Exit:
  34. *       returns 1 for A:, 2 for B:, 3 for C:, etc.
  35. *       returns 0 if current drive cannot be determined.
  36. *
  37. *Exceptions:
  38. *
  39. *******************************************************************************/
  40.  
  41. int __cdecl _getdrive (
  42.         void
  43.         )
  44. {
  45.         ULONG drivenum;
  46.         UCHAR curdirstr[_MAX_PATH];
  47.  
  48.         drivenum = 0;
  49.         if (GetCurrentDirectory(sizeof(curdirstr), curdirstr))
  50.                 if (curdirstr[1] == ':')
  51.                         drivenum = toupper(curdirstr[0]) - 64;
  52.  
  53.         return drivenum;
  54. }
  55.  
  56.  
  57. /***
  58. *int _chdrive(int drive) - set the current drive (1=A:, 2=B:, etc.)
  59. *
  60. *Purpose:
  61. *       Allows the user to change the current disk drive
  62. *
  63. *Entry:
  64. *       drive - the number of drive which should become the current drive
  65. *
  66. *Exit:
  67. *       returns 0 if successful, else -1
  68. *
  69. *Exceptions:
  70. *
  71. *******************************************************************************/
  72.  
  73. int __cdecl _chdrive (
  74.         int drive
  75.         )
  76. {
  77.         char  newdrive[3];
  78.  
  79.         if (drive < 1 || drive > 31) {
  80.             errno = EACCES;
  81.             _doserrno = ERROR_INVALID_DRIVE;
  82.             return -1;
  83.         }
  84.  
  85.         _mlock(_ENV_LOCK);
  86.  
  87.         newdrive[0] = (char)('A' + (char)drive - (char)1);
  88.         newdrive[1] = ':';
  89.         newdrive[2] = '\0';
  90.  
  91.         /*
  92.          * Set new drive. If current directory on new drive exists, it
  93.          * will become the cwd. Otherwise defaults to root directory.
  94.          */
  95.  
  96.         if ( SetCurrentDirectory((LPSTR)newdrive) ) {
  97.             _munlock(_ENV_LOCK);
  98.             return 0;
  99.         }
  100.         else {
  101.             _dosmaperr(GetLastError());
  102.             _munlock(_ENV_LOCK);
  103.             return -1;
  104.         }
  105. }
  106.  
  107. #else  /* _MAC */
  108.  
  109.  
  110. #include <cruntime.h>
  111. #include <ctype.h>
  112. #include <stdlib.h>
  113. #include <macos\osutils.h>
  114. #include <macos\files.h>
  115. #include <macos\errors.h>
  116.  
  117. /***
  118. *int _getdrive() - get current drive (-1=BootDisk, -2=Second mounted drive, etc.)
  119. *
  120. *Purpose:
  121. *       Returns the current disk drive
  122. *
  123. *Entry:
  124. *       No parameters.
  125. *
  126. *Exit:
  127. *       returns 1 for BootDisk, 2 for Second mounted drive, etc.
  128. *       returns 0 if current drive cannot be determined.
  129. *
  130. *Exceptions:
  131. *
  132. *******************************************************************************/
  133.  
  134. int __cdecl _getdrive (
  135.         void
  136.         )
  137. {
  138.         OSErr osErr;
  139.         WDPBRec wdPB;
  140.         char st[256];
  141.  
  142.         wdPB.ioNamePtr = &st[0];
  143.         osErr = PBHGetVolSync(&wdPB);
  144.         if (osErr)
  145.         {
  146.             return 0;
  147.         }
  148.  
  149.  
  150.         return wdPB.ioWDVRefNum;
  151. }
  152.  
  153.  
  154. /***
  155. *int _chdrive(int drive) - set the current drive (-1=BootDisk, -2=Second drive, etc.)
  156. *
  157. *Purpose:
  158. *       Allows the user to change the current disk drive
  159. *
  160. *
  161. *Entry:
  162. *       drive - the number of drive which should become the current drive
  163. *
  164. *Exit:
  165. *       returns 0 if successful, else -1
  166. *
  167. *Exceptions:
  168. *
  169. *******************************************************************************/
  170.  
  171. int __cdecl _chdrive (
  172.         int drive
  173.         )
  174. {
  175.         OSErr osErr;
  176.         WDPBRec wdPB;
  177.  
  178.         wdPB.ioNamePtr = NULL;
  179.         wdPB.ioWDDirID = 0;
  180.         wdPB.ioVRefNum = drive;
  181.         osErr = PBHSetVolSync(&wdPB);
  182.         if (osErr)
  183.         {
  184.             return -1;
  185.         }
  186.  
  187.         return 0;
  188. }
  189.  
  190. #endif  /* _MAC */
  191.