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.

308 lines
6.5 KiB

  1. /*** creatfil.C - Win 32 create file
  2. *
  3. *
  4. * Title:
  5. *
  6. * creatfil - Win 32 Create File Main File
  7. *
  8. * Copyright (c) 1993, Microsoft Corporation.
  9. * HonWah Chan
  10. *
  11. *
  12. * Description:
  13. *
  14. * This is the main part of the create file tool.
  15. * It takes as a parameter a file name and file size.
  16. *
  17. * Usage: creatfil filename filesize
  18. *
  19. * filename: name of file to create
  20. * filesize: size of file in KBytes
  21. *
  22. *
  23. * The Cache Flusher is organized as follows:
  24. *
  25. * o creatfil.c ........ Tools main body
  26. * o creatfil.h
  27. *
  28. * o creatf.c ..... create file utility routines
  29. * o creatf.h
  30. *
  31. *
  32. *
  33. *
  34. *
  35. *
  36. * Modification History:
  37. *
  38. * 93.05.17 HonWah Chan -- Created
  39. *
  40. *
  41. */
  42. char *VERSION = "1.0 (93.05.17)";
  43. // default filesize
  44. #define DEFAULT_SIZE (1024 * 1024)
  45. #define BUFFER_SIZE (5 * 1024)
  46. #define MAX_FILE_SIZE (4 * 1024 * 1024)
  47. /* * * * * * * * * * * * * I N C L U D E F I L E S * * * * * * * * * * */
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <string.h>
  51. #include <malloc.h>
  52. #include <time.h>
  53. #include <nt.h>
  54. #include <ntrtl.h>
  55. #include <nturtl.h>
  56. #include <windows.h>
  57. #include "creatfil.h"
  58. #include "creatf.h"
  59. /* * * * * * * * * * G L O B A L D E C L A R A T I O N S * * * * * * * * */
  60. /* none */
  61. /* * * * * * * * * * F U N C T I O N P R O T O T Y P E S * * * * * * * * */
  62. RC __cdecl main (int argc, char *argv[]);
  63. VOID ParseCmdLine (int argc, char *argv[], char *pFileName, DWORD *pFileSize);
  64. VOID CreateTheFile (char *pFileName, DWORD dFileSize);
  65. VOID Usage (char *argv[]);
  66. VOID WriteToFile (HANDLE hFileHandle, DWORD dFileSize);
  67. /* * * * * * * * * * * G L O B A L V A R I A B L E S * * * * * * * * * */
  68. /* none */
  69. /* * * * * * E X P O R T E D G L O B A L V A R I A B L E S * * * * * */
  70. /* none */
  71. /********************************* m a i n **********************************
  72. *
  73. * main(argc, argv)
  74. *
  75. * ENTRY argc - number of input arguments
  76. * argv - contains command line arguments
  77. *
  78. * EXIT -none-
  79. *
  80. * RETURN rc - return code in case of failure
  81. * STATUS_SUCCESS - if successful
  82. *
  83. * WARNING:
  84. * -none-
  85. *
  86. * COMMENT:
  87. * -none-
  88. *
  89. */
  90. RC __cdecl main (int argc, char *argv[])
  91. {
  92. char FileName[FNAME_LEN];
  93. DWORD dFileSize;
  94. ParseCmdLine (argc, argv, FileName, &dFileSize);
  95. CreateTheFile (FileName, dFileSize) ;
  96. ExitProcess(STATUS_SUCCESS);
  97. return (STATUS_SUCCESS);
  98. } /* main() */
  99. /************************ R e a d P a r a m e t e r s ************************
  100. *
  101. * parseCmdLine(argc, argv, pFIleName, pdFileSize) -
  102. * For Parsing the command line switches
  103. *
  104. * ENTRY argc - number of input arguments (const input)
  105. * argv - contains command line arguments (const input)
  106. *
  107. * EXIT pFileName - FileName
  108. * pdFIleSize - FileSize
  109. *
  110. * RETURN -none-
  111. *
  112. * WARNING:
  113. * -none-
  114. *
  115. * COMMENT:
  116. * -none-
  117. *
  118. */
  119. VOID ParseCmdLine (int argc, char *argv[], char *pFileName, DWORD *pFileSize)
  120. {
  121. if (argc < 2 ||
  122. (argv[1][0] == '-' || argv[1][0] == '/') && argv[1][1] == '?')
  123. { /* process options */
  124. Usage (argv);
  125. }
  126. else
  127. {
  128. // get filename and filesize
  129. strcpy (pFileName, argv[1]);
  130. if (argc <= 2)
  131. {
  132. *pFileSize = DEFAULT_SIZE;
  133. }
  134. else
  135. {
  136. *pFileSize = 0;
  137. if (sscanf(argv[2], "%d", pFileSize) != 1)
  138. {
  139. Failed(FILESIZE_ERR, __FILE__, __LINE__, " ");
  140. exit(1);
  141. }
  142. if (*pFileSize == 0 || *pFileSize > MAX_FILE_SIZE)
  143. {
  144. Failed(FILESIZE_ERR, __FILE__, __LINE__, " ");
  145. exit(1);
  146. }
  147. else
  148. {
  149. *pFileSize *= 1024;
  150. }
  151. }
  152. }
  153. }
  154. /*
  155. *
  156. * CreateTheFile - create the file using the filename and filesize.
  157. *
  158. * Accepts - pFileName - char *[]
  159. * dFileSize - DWORD
  160. *
  161. * Returns - nothing.
  162. *
  163. */
  164. VOID CreateTheFile (char *pFileName, DWORD dFileSize)
  165. {
  166. RC rc;
  167. HANDLE hFileHandle;
  168. char achErrMsg[LINE_LEN];
  169. hFileHandle = (HANDLE) CreateFile (pFileName,
  170. GENERIC_WRITE,
  171. 0,
  172. NULL, CREATE_ALWAYS,
  173. FILE_ATTRIBUTE_NORMAL, NULL);
  174. if (hFileHandle == INVALID_HANDLE_VALUE || hFileHandle == 0)
  175. {
  176. // Could not create the file
  177. rc = GetLastError();
  178. if (!(rc == ERROR_FILE_EXISTS || rc == ERROR_ACCESS_DENIED))
  179. {
  180. sprintf(achErrMsg,
  181. "CreateFile() - Error creating %s: %lu",
  182. pFileName, rc);
  183. Failed(FILEARG_ERR, __FILE__, __LINE__, achErrMsg);
  184. return;
  185. }
  186. }
  187. else
  188. {
  189. // fill the filesize.
  190. WriteToFile (hFileHandle, dFileSize);
  191. }
  192. CloseHandle (hFileHandle);
  193. }
  194. /*
  195. *
  196. * WriteToFile - write dFileSize bytes to the file.
  197. *
  198. * Accepts - hFileHandle - handle of the file
  199. * dFileSize - size of data to write to file
  200. *
  201. * Returns - nothing.
  202. *
  203. */
  204. VOID WriteToFile (HANDLE hFileHandle, DWORD dFileSize)
  205. {
  206. BOOL bSuccess;
  207. DWORD nAmtToWrite, nAmtWritten;
  208. LPVOID lpMemory;
  209. DWORD BufferSize = BUFFER_SIZE;
  210. char achErrMsg[LINE_LEN];
  211. if (dFileSize < BUFFER_SIZE)
  212. {
  213. BufferSize = dFileSize;
  214. }
  215. lpMemory = (LPVOID) MemoryAllocate (BufferSize);
  216. if (lpMemory == NULL)
  217. {
  218. Failed(ERROR_NOT_ENOUGH_MEMORY, __FILE__, __LINE__, " ");
  219. return;
  220. }
  221. while (dFileSize)
  222. {
  223. if (dFileSize > BUFFER_SIZE)
  224. {
  225. dFileSize -= BUFFER_SIZE;
  226. nAmtToWrite = BUFFER_SIZE;
  227. }
  228. else
  229. {
  230. nAmtToWrite = dFileSize;
  231. dFileSize = 0;
  232. }
  233. bSuccess = WriteFile (hFileHandle, lpMemory, nAmtToWrite, &nAmtWritten, NULL);
  234. if (!bSuccess || (nAmtWritten != nAmtToWrite))
  235. {
  236. // write error, stop.
  237. Failed(FWRITE_ERR, __FILE__, __LINE__, " ");
  238. MemoryFree (lpMemory);
  239. return;
  240. }
  241. }
  242. MemoryFree (lpMemory);
  243. }
  244. /*
  245. *
  246. * Usage - generates a usage message and terminates program.
  247. *
  248. * Accepts - argv - char *[]
  249. *
  250. * Returns - nothing.
  251. *
  252. */
  253. VOID Usage (char *argv[])
  254. {
  255. printf( "Usage: ");
  256. printf( "%s FileName [FileSize]\n", argv[0]);
  257. printf( "\t-? : This message\n");
  258. printf( "\t-FileName -- name of the new file\n");
  259. printf( "\t-FileSize -- size of file in KBytes, default is 1024 KBytes\n");
  260. exit (1);
  261. }