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.

146 lines
5.1 KiB

  1. /***
  2. *fullpath.c -
  3. *
  4. * Copyright (c) 1987-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose: contains the function _fullpath which makes an absolute path out
  7. * of a relative path. i.e. ..\pop\..\main.c => c:\src\main.c if the
  8. * current directory is c:\src\src
  9. *
  10. *Revision History:
  11. * 12-21-87 WAJ Initial version
  12. * 01-08-88 WAJ now treats / as an \
  13. * 06-22-88 WAJ now handles network paths ie \\sl\users
  14. * 01-31-89 SKS/JCR Renamed _canonic to _fullpath
  15. * 04-03-89 WAJ Now returns "d:\dir" for "."
  16. * 05-09-89 SKS Do not change the case of arguments
  17. * 11-30-89 JCR Preserve errno setting from _getdcwd() call on errors
  18. * 03-07-90 GJF Replaced _LOAD_DS with _CALLTYPE1, added #include
  19. * <cruntime.h>, removed #include <register.h> and fixed
  20. * the copyright.
  21. * 04-25-90 JCR Fixed an incorrect errno setting
  22. * 06-14-90 SBM Fixed bugs in which case of user provided drive letter
  23. * was not always preserved, and c:\foo\\bar did not
  24. * generate an error
  25. * 08-10-90 SBM Compiles cleanly with -W3
  26. * 08-28-90 SBM Fixed bug in which UNC names were being rejected
  27. * 09-27-90 GJF New-style function declarator.
  28. * 01-18-91 GJF ANSI naming.
  29. * 11-30-92 KRS Ported _MBCS code from 16-bit tree.
  30. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  31. * 04-26-93 SKS Add check for drive validity
  32. * 08-03-93 KRS Change to use _ismbstrail instead of isdbcscode.
  33. * 09-27-93 CFW Avoid cast bug.
  34. * 12-07-93 CFW Wide char enable.
  35. * 01-26-94 CFW Remove unused isdbcscode function.
  36. * 11-08-94 GJF Revised to use GetFullPathName.
  37. * 02-08-95 JWM Spliced _WIN32 & Mac versions.
  38. * 03-28-96 GJF Free malloc-ed buffer if GetFullPathName fails.
  39. * Detab-ed. Also, cleaned up the ill-formatted Mac
  40. * version a bit and renamed isdbcscode to __isdbcscode.
  41. * 07-01-96 GJF Replaced defined(_WIN32) by !defined(_MAC).
  42. * 12-15-98 GJF Changes for 64-bit size_t.
  43. * 05-17-99 PML Remove all Macintosh support.
  44. *
  45. *******************************************************************************/
  46. #include <cruntime.h>
  47. #include <stdio.h>
  48. #include <direct.h>
  49. #include <errno.h>
  50. #include <stdlib.h>
  51. #include <internal.h>
  52. #include <tchar.h>
  53. #include <windows.h>
  54. /***
  55. *_TSCHAR *_fullpath( _TSCHAR *buf, const _TSCHAR *path, maxlen );
  56. *
  57. *Purpose:
  58. *
  59. * _fullpath - combines the current directory with path to form
  60. * an absolute path. i.e. _fullpath takes care of .\ and ..\
  61. * in the path.
  62. *
  63. * The result is placed in buf. If the length of the result
  64. * is greater than maxlen NULL is returned, otherwise
  65. * the address of buf is returned.
  66. *
  67. * If buf is NULL then a buffer is malloc'ed and maxlen is
  68. * ignored. If there are no errors then the address of this
  69. * buffer is returned.
  70. *
  71. * If path specifies a drive, the curent directory of this
  72. * drive is combined with path. If the drive is not valid
  73. * and _fullpath needs the current directory of this drive
  74. * then NULL is returned. If the current directory of this
  75. * non existant drive is not needed then a proper value is
  76. * returned.
  77. * For example: path = "z:\\pop" does not need z:'s current
  78. * directory but path = "z:pop" does.
  79. *
  80. *
  81. *
  82. *Entry:
  83. * _TSCHAR *buf - pointer to a buffer maintained by the user;
  84. * _TSCHAR *path - path to "add" to the current directory
  85. * int maxlen - length of the buffer pointed to by buf
  86. *
  87. *Exit:
  88. * Returns pointer to the buffer containing the absolute path
  89. * (same as buf if non-NULL; otherwise, malloc is
  90. * used to allocate a buffer)
  91. *
  92. *Exceptions:
  93. *
  94. *******************************************************************************/
  95. _TSCHAR * __cdecl _tfullpath (
  96. _TSCHAR *UserBuf,
  97. const _TSCHAR *path,
  98. size_t maxlen
  99. )
  100. {
  101. _TSCHAR *buf;
  102. _TSCHAR *pfname;
  103. unsigned long count;
  104. if ( !path || !*path ) /* no work to do */
  105. return( _tgetcwd( UserBuf, (int)maxlen ) );
  106. /* allocate buffer if necessary */
  107. if ( !UserBuf )
  108. if ( !(buf = malloc(_MAX_PATH * sizeof(_TSCHAR))) ) {
  109. errno = ENOMEM;
  110. return( NULL );
  111. }
  112. else
  113. maxlen = _MAX_PATH;
  114. else
  115. buf = UserBuf;
  116. count = GetFullPathName ( path,
  117. (int)maxlen,
  118. buf,
  119. &pfname );
  120. if ( count >= maxlen ) {
  121. if ( !UserBuf )
  122. free(buf);
  123. errno = ERANGE;
  124. return( NULL );
  125. }
  126. else if ( count == 0 ) {
  127. if ( !UserBuf )
  128. free(buf);
  129. _dosmaperr( GetLastError() );
  130. return( NULL );
  131. }
  132. return( buf );
  133. }