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.

181 lines
4.9 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1996 - 1999
  3. Module Name:
  4. DTCommon.c
  5. Abstract:
  6. Common stuff for the Data Tranfer tests
  7. Author:
  8. Brian Wong (t-bwong) 27-Mar-96
  9. Revision History:
  10. --*/
  11. #include <rpcperf.h>
  12. #include "DTCommon.h"
  13. //
  14. // Strings
  15. //
  16. const char *szFormatCantOpenTempFile="%s: cannot open %s.\n";
  17. const char *szFormatCantOpenServFile="%s: cannot open file on server.\n";
  18. //---------------------------------------------------------
  19. void PrintSysErrorStringA (DWORD dwWinErrCode)
  20. /*++
  21. Routine Description:
  22. Given a Win32 error code, print a string
  23. that describes the error condition.
  24. Arguments:
  25. dwWinErrCode - the error code.
  26. Return Value:
  27. NONE
  28. --*/
  29. {
  30. const DWORD ulBufLength = 255;
  31. char szErrorString[256];
  32. //
  33. // Use the Win32 API FormatMessage().
  34. //
  35. if (0 != FormatMessageA (FORMAT_MESSAGE_FROM_SYSTEM,
  36. NULL,
  37. dwWinErrCode,
  38. MAKELANGID (LANG_NEUTRAL, SUBLANG_NEUTRAL),
  39. szErrorString,
  40. ulBufLength,
  41. NULL))
  42. {
  43. printf(szErrorString);
  44. }
  45. return;
  46. }
  47. //---------------------------------------------------------
  48. BOOL CreateTempFile (LPCTSTR pszPath, // [in]
  49. LPCTSTR pszPrefix, // [in]
  50. DWORD ulLength, // [in]
  51. LPTSTR pszFileName) // [out]
  52. /*++
  53. Routine Description:
  54. Creates a temporary file.
  55. Arguments:
  56. pszPath - the system temp path is used of NULL
  57. pszPrefix - the prefix for the temporary file
  58. ulLength - the length of the file is set to this value
  59. pszFileName - where to store the name of the temporary file
  60. Return Value:
  61. TRUE if successful, FALSE otherwise
  62. --*/
  63. {
  64. static const char *szFuncName = "CreateTempFile";
  65. TCHAR pTempPath[MAX_PATH];
  66. HANDLE hFile;
  67. //
  68. // Use the Win32 API GetTempPath() if a path is not provide.
  69. //
  70. if (NULL == pszPath)
  71. GetTempPath (MAX_PATH, pTempPath);
  72. //
  73. // Note: GetTempFileName() also creates the file.
  74. //
  75. if (0 == GetTempFileName ((NULL == pszPath ? pTempPath : pszPath),
  76. pszPrefix,
  77. 0,
  78. pszFileName))
  79. {
  80. printf("%s: GetTempFileName failed. %s\n",
  81. szFuncName);
  82. PrintSysErrorStringA(GetLastError());
  83. return FALSE;
  84. }
  85. //
  86. // If ulLength != 0, we set the temp file to the specified length.
  87. //
  88. if (ulLength != 0)
  89. {
  90. //
  91. // Open the file for writing.
  92. //
  93. hFile = CreateFile (pszFileName,
  94. GENERIC_WRITE,
  95. 0,
  96. (LPSECURITY_ATTRIBUTES) NULL,
  97. CREATE_ALWAYS,
  98. FILE_ATTRIBUTE_NORMAL,
  99. (HANDLE) NULL
  100. );
  101. if (hFile == INVALID_HANDLE_VALUE)
  102. {
  103. printf(szFormatCantOpenTempFile,
  104. szFuncName,
  105. pszFileName);
  106. PrintSysErrorStringA(GetLastError());
  107. DeleteFile(pszFileName);
  108. return FALSE;
  109. }
  110. //
  111. // If SEND_BLANKS is defined, then we don't do any pre-initializing
  112. // of the files transferred. Otherwise, we fill the file up with
  113. // stuff. (See below.)
  114. //
  115. #ifdef SEND_BLANKS
  116. if (Length != SetFilePointer (hFile, ulLength, NULL, FILE_BEGIN))
  117. {
  118. DWORD dwErrCode = GetLastError();
  119. printf("%s: %ld.\n",
  120. szFuncName,
  121. dwErrCode);
  122. PrintSysErrorStringA(dwErrCode);
  123. CloseHandle (hFile);
  124. DeleteFile (pszFileName);
  125. return FALSE;
  126. }
  127. SetEndOfFile(hFile);
  128. #else // ! defined (SEND_BLANKS)
  129. //
  130. // Here we fill the file up with 8-byte blocks of the form
  131. // "#######<blank>", where ####### is a descending number
  132. // indicating the number of bytes till EOF. If the length
  133. // is not a multiple of 8, then the file is padded with $'s
  134. //
  135. {
  136. long i;
  137. DWORD dwBytesWritten;
  138. char pTempString[9];
  139. for (i = ulLength; i >= 8; i -= 8)
  140. {
  141. sprintf(pTempString, "%7ld ",i);
  142. WriteFile (hFile, pTempString, 8, &dwBytesWritten, NULL);
  143. }
  144. if (i > 0)
  145. {
  146. WriteFile (hFile, "$$$$$$$", i, &dwBytesWritten, NULL);
  147. }
  148. }
  149. #endif // SEND_BLANKS
  150. CloseHandle(hFile);
  151. }
  152. return TRUE;
  153. }