Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.8 KiB

  1. /***
  2. *drive.c - get and change current drive
  3. *
  4. * Copyright (c) 1989-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * This file has the _getdrive() and _chdrive() functions
  8. *
  9. *Revision History:
  10. * 06-06-89 PHG Module created, based on asm version
  11. * 03-07-90 GJF Made calling type _CALLTYPE1, added #include
  12. * <cruntime.h> and fixed copyright. Also, cleaned up
  13. * the formatting a bit.
  14. * 07-24-90 SBM Removed '32' from API names
  15. * 09-27-90 GJF New-style function declarators.
  16. * 12-04-90 SRW Changed to include <oscalls.h> instead of <doscalls.h>
  17. * 12-06-90 SRW Added _CRUISER_ and _WIN32 conditionals.
  18. * 05-10-91 GJF Fixed off-by-1 error in Win32 version and updated the
  19. * function descriptions a bit [_WIN32_].
  20. * 05-19-92 GJF Revised to use the 'current directory' environment
  21. * variables of Win32/NT.
  22. * 06-09-92 GJF Use _putenv instead of Win32 API call. Also, defer
  23. * adding env var until after the successful call to
  24. * change the dir/drive.
  25. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  26. * 11-24-93 CFW Rip out Cruiser.
  27. * 11-24-93 CFW No longer store current drive in CRT env strings.
  28. * 02-08-95 JWM Spliced _WIN32 & Mac versions.
  29. * 07-01-96 GJF Replaced defined(_WIN32) with !defined(_MAC). Also,
  30. * detab-ed and cleaned up the format a bit.
  31. * 12-17-97 GJF Exception-safe locking.
  32. * 05-17-99 PML Remove all Macintosh support.
  33. *
  34. *******************************************************************************/
  35. #include <cruntime.h>
  36. #include <oscalls.h>
  37. #include <mtdll.h>
  38. #include <internal.h>
  39. #include <msdos.h>
  40. #include <errno.h>
  41. #include <ctype.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. /***
  45. *int _getdrive() - get current drive (1=A:, 2=B:, etc.)
  46. *
  47. *Purpose:
  48. * Returns the current disk drive
  49. *
  50. *Entry:
  51. * No parameters.
  52. *
  53. *Exit:
  54. * returns 1 for A:, 2 for B:, 3 for C:, etc.
  55. * returns 0 if current drive cannot be determined.
  56. *
  57. *Exceptions:
  58. *
  59. *******************************************************************************/
  60. int __cdecl _getdrive (
  61. void
  62. )
  63. {
  64. ULONG drivenum;
  65. UCHAR curdirstr[_MAX_PATH];
  66. drivenum = 0;
  67. if (GetCurrentDirectory(sizeof(curdirstr), curdirstr))
  68. if (curdirstr[1] == ':')
  69. drivenum = toupper(curdirstr[0]) - 64;
  70. return drivenum;
  71. }
  72. /***
  73. *int _chdrive(int drive) - set the current drive (1=A:, 2=B:, etc.)
  74. *
  75. *Purpose:
  76. * Allows the user to change the current disk drive
  77. *
  78. *Entry:
  79. * drive - the number of drive which should become the current drive
  80. *
  81. *Exit:
  82. * returns 0 if successful, else -1
  83. *
  84. *Exceptions:
  85. *
  86. *******************************************************************************/
  87. int __cdecl _chdrive (
  88. int drive
  89. )
  90. {
  91. int retval;
  92. char newdrive[3];
  93. if (drive < 1 || drive > 31) {
  94. errno = EACCES;
  95. _doserrno = ERROR_INVALID_DRIVE;
  96. return -1;
  97. }
  98. #ifdef _MT
  99. _mlock( _ENV_LOCK );
  100. __try {
  101. #endif
  102. newdrive[0] = (char)('A' + (char)drive - (char)1);
  103. newdrive[1] = ':';
  104. newdrive[2] = '\0';
  105. /*
  106. * Set new drive. If current directory on new drive exists, it
  107. * will become the cwd. Otherwise defaults to root directory.
  108. */
  109. if ( SetCurrentDirectory((LPSTR)newdrive) )
  110. retval = 0;
  111. else {
  112. _dosmaperr(GetLastError());
  113. retval = -1;
  114. }
  115. #ifdef _MT
  116. }
  117. __finally {
  118. _munlock( _ENV_LOCK );
  119. }
  120. #endif
  121. return retval;
  122. }