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.

189 lines
3.6 KiB

  1. /*++
  2. Copyright (c) 1996, 1997 Microsoft Corporation
  3. Module Name:
  4. filemisc.c
  5. Abstract:
  6. This module contains routines to perform miscellaneous file related
  7. operations in the protected store.
  8. Author:
  9. Scott Field (sfield) 27-Nov-96
  10. --*/
  11. #include <windows.h>
  12. #include <sha.h>
  13. #include "filemisc.h"
  14. #include "unicode5.h"
  15. #include "debug.h"
  16. BOOL
  17. GetFileNameFromPath(
  18. IN LPCWSTR FullPath,
  19. IN OUT LPCWSTR *FileName // points to filename component in FullPath
  20. )
  21. {
  22. DWORD cch = lstrlenW(FullPath);
  23. *FileName = FullPath;
  24. while( cch ) {
  25. if( FullPath[cch] == L'\\' ||
  26. FullPath[cch] == L'/' ||
  27. (cch == 1 && FullPath[1] == L':') ) {
  28. *FileName = &FullPath[cch+1];
  29. break;
  30. }
  31. cch--;
  32. }
  33. return TRUE;
  34. }
  35. BOOL
  36. GetFileNameFromPathA(
  37. IN LPCSTR FullPath,
  38. IN OUT LPCSTR *FileName // points to filename component in FullPath
  39. )
  40. {
  41. DWORD cch = lstrlenA(FullPath);
  42. *FileName = FullPath;
  43. while( cch ) {
  44. if( FullPath[cch] == '\\' ||
  45. FullPath[cch] == '/' ||
  46. (cch == 1 && FullPath[1] == ':') ) {
  47. *FileName = &FullPath[cch+1];
  48. break;
  49. }
  50. cch--;
  51. }
  52. return TRUE;
  53. }
  54. BOOL
  55. TranslateFromSlash(
  56. IN LPWSTR szInput,
  57. IN OUT LPWSTR *pszOutput // optional
  58. )
  59. {
  60. return TranslateString(szInput, pszOutput, L'\\', L'*');
  61. }
  62. BOOL
  63. TranslateToSlash(
  64. IN LPWSTR szInput,
  65. IN OUT LPWSTR *pszOutput // optional
  66. )
  67. {
  68. return TranslateString(szInput, pszOutput, L'*', L'\\');
  69. }
  70. BOOL
  71. TranslateString(
  72. IN LPWSTR szInput,
  73. IN OUT LPWSTR *pszOutput, // optional
  74. IN WCHAR From,
  75. IN WCHAR To
  76. )
  77. {
  78. LPWSTR szOut;
  79. DWORD cch = lstrlenW(szInput);
  80. DWORD i; // scan forward for cache - locality of reference
  81. if(pszOutput == NULL) {
  82. //
  83. // translate in place in existing string.
  84. //
  85. szOut = szInput;
  86. } else {
  87. DWORD cb = (cch+1) * sizeof(WCHAR);
  88. //
  89. // allocate new string and translate there.
  90. //
  91. szOut = (LPWSTR)SSAlloc( cb );
  92. *pszOutput = szOut;
  93. if(szOut == NULL)
  94. return FALSE;
  95. CopyMemory((LPBYTE)szOut, (LPBYTE)szInput, cb);
  96. }
  97. for(i = 0 ; i < cch ; i++) {
  98. if( szOut[ i ] == From )
  99. szOut[ i ] = To;
  100. }
  101. return TRUE;
  102. }
  103. BOOL
  104. FindAndOpenFile(
  105. IN LPCWSTR szFileName, // file to search for + open
  106. IN LPWSTR pszFullPath, // file to fill fullpath with
  107. IN DWORD cchFullPath, // size of full path buffer, including NULL
  108. IN OUT PHANDLE phFile // resultant open file handle
  109. )
  110. /*++
  111. This function searches the path for the specified file and if a file
  112. is found, the file is opened for read access and a handle to the open
  113. file is returned to the caller in the phFile parameter.
  114. --*/
  115. {
  116. LPWSTR szPart;
  117. *phFile = INVALID_HANDLE_VALUE;
  118. if(SearchPathW(
  119. NULL,
  120. szFileName,
  121. NULL,
  122. cchFullPath,
  123. pszFullPath,
  124. &szPart
  125. ) == 0)
  126. {
  127. return FALSE;
  128. }
  129. *phFile = CreateFileU(
  130. pszFullPath,
  131. GENERIC_READ,
  132. FILE_SHARE_READ,
  133. NULL,
  134. OPEN_EXISTING,
  135. 0,
  136. NULL
  137. );
  138. if(*phFile == INVALID_HANDLE_VALUE)
  139. return FALSE;
  140. return TRUE;
  141. }