Leaked source code of windows server 2003
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.

161 lines
4.7 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. * 04-26-02 GB fixed bug in _getdrive if path is greater then
  34. * MAX_PATH, i.e. "\\?\" prepended to path.
  35. *
  36. *******************************************************************************/
  37. #include <cruntime.h>
  38. #include <oscalls.h>
  39. #include <mtdll.h>
  40. #include <internal.h>
  41. #include <msdos.h>
  42. #include <errno.h>
  43. #include <ctype.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include <malloc.h>
  47. #include <dbgint.h>
  48. /***
  49. *int _getdrive() - get current drive (1=A:, 2=B:, etc.)
  50. *
  51. *Purpose:
  52. * Returns the current disk drive
  53. *
  54. *Entry:
  55. * No parameters.
  56. *
  57. *Exit:
  58. * returns 1 for A:, 2 for B:, 3 for C:, etc.
  59. * returns 0 if current drive cannot be determined.
  60. *
  61. *Exceptions:
  62. *
  63. *******************************************************************************/
  64. int __cdecl _getdrive (
  65. void
  66. )
  67. {
  68. ULONG drivenum;
  69. UCHAR curdirstr[_MAX_PATH+1];
  70. UCHAR *cdirstr = curdirstr;
  71. int memfree=0,r=0;
  72. r = GetCurrentDirectory(MAX_PATH+1,(LPTSTR)cdirstr);
  73. if (r> MAX_PATH) {
  74. __try{
  75. cdirstr=(UCHAR *)_alloca((r+1)*sizeof(UCHAR));
  76. } __except(EXCEPTION_EXECUTE_HANDLER){
  77. _resetstkoflw();
  78. if ((cdirstr= (UCHAR *)_malloc_crt((r+1)*sizeof(UCHAR))) == NULL) {
  79. r = 0;
  80. } else {
  81. memfree = 1;
  82. }
  83. }
  84. if (r)
  85. r = GetCurrentDirectory(r+1,(LPTSTR)cdirstr);
  86. }
  87. drivenum = 0;
  88. if (r)
  89. if (cdirstr[1] == ':')
  90. drivenum = toupper(cdirstr[0]) - 64;
  91. if (memfree)
  92. _free_crt(cdirstr);
  93. return drivenum;
  94. }
  95. /***
  96. *int _chdrive(int drive) - set the current drive (1=A:, 2=B:, etc.)
  97. *
  98. *Purpose:
  99. * Allows the user to change the current disk drive
  100. *
  101. *Entry:
  102. * drive - the number of drive which should become the current drive
  103. *
  104. *Exit:
  105. * returns 0 if successful, else -1
  106. *
  107. *Exceptions:
  108. *
  109. *******************************************************************************/
  110. int __cdecl _chdrive (
  111. int drive
  112. )
  113. {
  114. int retval;
  115. char newdrive[3];
  116. if (drive < 1 || drive > 31) {
  117. errno = EACCES;
  118. _doserrno = ERROR_INVALID_DRIVE;
  119. return -1;
  120. }
  121. #ifdef _MT
  122. _mlock( _ENV_LOCK );
  123. __try {
  124. #endif
  125. newdrive[0] = (char)('A' + (char)drive - (char)1);
  126. newdrive[1] = ':';
  127. newdrive[2] = '\0';
  128. /*
  129. * Set new drive. If current directory on new drive exists, it
  130. * will become the cwd. Otherwise defaults to root directory.
  131. */
  132. if ( SetCurrentDirectory((LPSTR)newdrive) )
  133. retval = 0;
  134. else {
  135. _dosmaperr(GetLastError());
  136. retval = -1;
  137. }
  138. #ifdef _MT
  139. }
  140. __finally {
  141. _munlock( _ENV_LOCK );
  142. }
  143. #endif
  144. return retval;
  145. }