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.

268 lines
5.7 KiB

  1. /*** CreatF.C - Utility functions for Win 32 Cache Flusher.
  2. *
  3. *
  4. * Title:
  5. * CreatF - Create File Utility Routines
  6. *
  7. * Copyright (c) 1993, Microsoft Corporation.
  8. * HonWah Chan.
  9. *
  10. *
  11. * Description:
  12. *
  13. * This file includes all the utility functions used by the Win 32
  14. * Create File. (CreatF.c)
  15. *
  16. *
  17. * Design/Implementation Notes:
  18. *
  19. *
  20. * Modification History:
  21. * 93.05.17 HonWahChan -- created
  22. *
  23. */
  24. /* * * * * * * * * * * * * I N C L U D E F I L E S * * * * * * * * * * */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <malloc.h>
  29. #include <nt.h>
  30. #include <ntrtl.h>
  31. #include <nturtl.h>
  32. #include <windows.h>
  33. /* * * * * * * * * * G L O B A L D E C L A R A T I O N S * * * * * * * * */
  34. #include "Creatfil.h"
  35. /* * * * * * * * * * F U N C T I O N P R O T O T Y P E S * * * * * * * * */
  36. #include "Creatf.h"
  37. /* * * * * * * * * * * G L O B A L V A R I A B L E S * * * * * * * * * */
  38. /* none */
  39. /* * * * * * E X P O R T E D G L O B A L V A R I A B L E S * * * * * */
  40. /* none */
  41. /******************************* F a i l e d *******************************
  42. *
  43. * Failed(rc, lpstrFname, lineno, lpstrMsg) -
  44. * Checks the RC for an error type if an error has occured,
  45. * prints the appropriate error message. It logs the error
  46. * message to the testlog file.
  47. *
  48. * ENTRY rc - return code from the last API call
  49. * lpstrFname - contains file name of where error occured
  50. * lineno - contains line number of failed API call
  51. * lpstrMsg - contains a general purpose message about the error
  52. *
  53. * EXIT -none-
  54. *
  55. * RETURN TRUE - if API failed
  56. * FALSE - if API successful
  57. *
  58. * WARNING:
  59. * -none-
  60. *
  61. * COMMENT:
  62. * -none-
  63. *
  64. */
  65. BOOL Failed (RC rc, LPSTR lpstrFname, WORD lineno, LPSTR lpstrMsg)
  66. {
  67. LPSTR lpstrErrMsg;
  68. if (rc != STATUS_SUCCESS) {
  69. switch (rc) {
  70. case (NTSTATUS)STATUS_INVALID_PARAMETER:
  71. lpstrErrMsg = "Invalid parameter";
  72. break;
  73. case STATUS_TIMEOUT:
  74. lpstrErrMsg = "TimeOut occured";
  75. break;
  76. case STATUS_INVALID_HANDLE:
  77. lpstrErrMsg = "Invalid handle";
  78. break;
  79. case STATUS_BUFFER_OVERFLOW:
  80. lpstrErrMsg = "Buffer overflow";
  81. break;
  82. case STATUS_ABANDONED:
  83. lpstrErrMsg = "Object abandoned";
  84. break;
  85. case ERROR_NOT_ENOUGH_MEMORY:
  86. lpstrErrMsg = "Not enough memory";
  87. break;
  88. case INPUTARGS_ERR:
  89. lpstrErrMsg = "Invalid number of input arguments";
  90. break;
  91. case FILESIZE_ERR:
  92. lpstrErrMsg = "Invalid file size argument";
  93. break;
  94. case INSUFMEM_ERR:
  95. lpstrErrMsg = "Insufficient Memory";
  96. break;
  97. case FCLOSE_ERR:
  98. lpstrErrMsg = "fclose() failed";
  99. break;
  100. case FOPEN_ERR:
  101. lpstrErrMsg = "fopen() failed";
  102. break;
  103. case FSEEK_ERR:
  104. lpstrErrMsg = "fseek() failed";
  105. break;
  106. case FWRITE_ERR:
  107. lpstrErrMsg = "WriteFile() failed";
  108. break;
  109. default:
  110. lpstrErrMsg = "";
  111. } /* switch(rc) */
  112. printf(" **************************\n");
  113. printf(" * FAILure --> Line=%d File=%s (pid=0x%lX tid=0x%lX)\n",
  114. lineno, lpstrFname, GetCurrentProcessId(),
  115. GetCurrentThreadId());
  116. printf(" * RC=0x%lX (%s)\n", rc, lpstrErrMsg);
  117. printf(" * %s\n", lpstrMsg);
  118. printf(" **************************\n");
  119. return(TRUE);
  120. } /* if(rc..) */
  121. return(FALSE);
  122. } /* Failed() */
  123. /************************** D i s p l a y U s a g e ************************
  124. *
  125. * DisplayUsage() -
  126. * Displays usgae for Create File
  127. *
  128. * ENTRY -none-
  129. *
  130. * EXIT -none-
  131. *
  132. * RETURN -none-
  133. *
  134. * WARNING:
  135. * -none-
  136. *
  137. * COMMENT:
  138. * -none-
  139. *
  140. */
  141. void DisplayUsage (void)
  142. {
  143. printf("\nUsage: CreatFil <FileName> <FileSize>\n");
  144. printf(" CreatFil - Create a file with the specified File name with the specified file sizes in bytes\n");
  145. return;
  146. } /* DisplayUsage() */
  147. /*
  148. * MemoryAllocate() -
  149. * Allocate a memory with the specified size
  150. *
  151. * ENTRY dwSize - size of buffer to be allocated
  152. *
  153. * EXIT -none-
  154. *
  155. * RETURN -none-
  156. *
  157. * WARNING:
  158. * -none-
  159. *
  160. * COMMENT:
  161. * -none-
  162. *
  163. */
  164. LPVOID MemoryAllocate (DWORD dwSize)
  165. { // MemoryAllocate
  166. HGLOBAL hMemory ;
  167. LPVOID lpMemory ;
  168. hMemory = GlobalAlloc (GHND, dwSize) ;
  169. if (!hMemory)
  170. return (NULL) ;
  171. lpMemory = GlobalLock (hMemory) ;
  172. if (!lpMemory)
  173. GlobalFree (hMemory) ;
  174. return (lpMemory) ;
  175. } // MemoryAllocate
  176. /*
  177. * MemoryFree() -
  178. * Free a previously allocated memory.
  179. *
  180. * ENTRY lpMemory - Buffer address to be freeed
  181. *
  182. * EXIT -none-
  183. *
  184. * RETURN -none-
  185. *
  186. * WARNING:
  187. * -none-
  188. *
  189. * COMMENT:
  190. * -none-
  191. *
  192. */
  193. VOID MemoryFree (LPVOID lpMemory)
  194. { // MemoryFree
  195. HGLOBAL hMemory ;
  196. if (!lpMemory)
  197. return ;
  198. hMemory = GlobalHandle (lpMemory) ;
  199. if (hMemory)
  200. { // if
  201. GlobalUnlock (hMemory) ;
  202. GlobalFree (hMemory) ;
  203. } // if
  204. } // MemoryFree
  205.