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.

110 lines
3.9 KiB

  1. /***
  2. *chdir.c - change directory
  3. *
  4. * Copyright (c) 1989-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * This file has the _chdir() function - change current directory.
  8. *
  9. *Revision History:
  10. * 06-06-89 PHG Module created, based on asm version
  11. * 03-07-90 GJF Made calling type _CALLTYPE2 (for now), added #include
  12. * <cruntime.h>, fixed copyright and fixed compiler
  13. * warnings. Also, cleaned up the formatting a bit.
  14. * 03-30-90 GJF Now _CALLTYPE1.
  15. * 07-24-90 SBM Removed '32' from API names
  16. * 09-27-90 GJF New-style function declarator.
  17. * 12-04-90 SRW Changed to include <oscalls.h> instead of <doscalls.h>
  18. * 12-06-90 SRW Added _CRUISER_ and _WIN32 conditionals.
  19. * 01-16-91 GJF ANSI naming.
  20. * 05-19-92 GJF Revised to support the 'current directory' environment
  21. * variables of Win32/NT.
  22. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  23. * 11-24-93 CFW Rip out Cruiser.
  24. * 11-24-93 CFW No longer store current drive in CRT env strings.
  25. * 12-01-93 CFW Set OS drive letter variables.
  26. * 12-07-93 CFW Wide char enable.
  27. * 01-25-95 GJF New current directory can be a UNC path!
  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. * 05-17-99 PML Remove all Macintosh support.
  32. *
  33. *******************************************************************************/
  34. #include <cruntime.h>
  35. #include <oscalls.h>
  36. #include <mtdll.h>
  37. #include <internal.h>
  38. #include <direct.h>
  39. #include <stdlib.h>
  40. #include <tchar.h>
  41. /***
  42. *int _chdir(path) - change current directory
  43. *
  44. *Purpose:
  45. * Changes the current working directory to that given in path.
  46. *
  47. *Entry:
  48. * _TSCHAR *path - directory to change to
  49. *
  50. *Exit:
  51. * returns 0 if successful,
  52. * returns -1 and sets errno if unsuccessful
  53. *
  54. *Exceptions:
  55. *
  56. *******************************************************************************/
  57. int __cdecl _tchdir (
  58. const _TSCHAR *path
  59. )
  60. {
  61. _TSCHAR env_var[4];
  62. _TSCHAR abspath[MAX_PATH+1];
  63. if ( SetCurrentDirectory((LPTSTR)path) )
  64. {
  65. /*
  66. * If the new current directory path is NOT a UNC path, we must
  67. * update the OS environment variable specifying the current
  68. * directory for what is now current drive. To do this, get the
  69. * full current directory, build the environment variable string
  70. * and call SetEnvironmentVariable(). We need to do this because
  71. * SetCurrentDirectory does not (i.e., does not update the
  72. * current-directory-on-drive environment variables) and other
  73. * functions (fullpath, spawn, etc) need them to be set.
  74. *
  75. * If associated with a 'drive', the current directory should
  76. * have the form of the example below:
  77. *
  78. * D:\nt\private\mytests
  79. *
  80. * so that the environment variable should be of the form:
  81. *
  82. * =D:=D:\nt\private\mytests
  83. *
  84. */
  85. if ( GetCurrentDirectory(MAX_PATH+1, (LPTSTR)abspath) != 0 )
  86. {
  87. /*
  88. * check if it is a UNC name, just return if is
  89. */
  90. if ( ((abspath[0] == _T('\\')) || (abspath[0] == _T('/'))) &&
  91. (abspath[0] == abspath[1]) )
  92. return 0;
  93. env_var[0] = _T('=');
  94. env_var[1] = (_TSCHAR) _totupper((_TUCHAR)abspath[0]);
  95. env_var[2] = _T(':');
  96. env_var[3] = _T('\0');
  97. if ( SetEnvironmentVariable(env_var, abspath) )
  98. return 0;
  99. }
  100. }
  101. _dosmaperr(GetLastError());
  102. return -1;
  103. }