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.

202 lines
4.8 KiB

  1. /*++
  2. Copyright (c) 1998-1999 Microsoft Corporation
  3. Module Name:
  4. main.c
  5. Abstract:
  6. simple bvt-like test code for SR.SYS.
  7. Author:
  8. Paul McDaniel (paulmcd) 07-Mar-2000
  9. Revision History:
  10. --*/
  11. #include "precomp.h"
  12. #include "stdlib.h"
  13. void __cdecl main(int argc, char **argv)
  14. {
  15. char sz[1024];
  16. HANDLE TestFile;
  17. ULONG Length;
  18. BOOL b;
  19. ULONG Disposition = 0;
  20. ULONG ShareMode = 0;
  21. ULONG DesiredAccess = GENERIC_WRITE|GENERIC_READ;
  22. if (argc < 3)
  23. {
  24. printf("usage:\n\tsrclient.exe filename [op] [share_mode] [disposition] \n");
  25. printf("\t\t0=WriteFile\n\t\t1=MapViewOfFile\n\t\t2=hold_open\n\t\t3=GetFileAttributesEx\n\t\t4=NtLockFile\n");
  26. return;
  27. }
  28. printf("performing %s on %s\n", argv[2], argv[1]);
  29. if (argv[2][0] == '0')
  30. Disposition = CREATE_ALWAYS;
  31. else if (argv[2][0] == '1')
  32. Disposition = OPEN_ALWAYS;
  33. else if (argv[2][0] == '2' || argv[2][0] == '4')
  34. {
  35. ShareMode = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE;
  36. DesiredAccess = GENERIC_READ;
  37. Disposition = OPEN_EXISTING;
  38. }
  39. else if (argv[2][0] == '3')
  40. {
  41. WIN32_FILE_ATTRIBUTE_DATA AttribData;
  42. GetFileAttributesEx(argv[1], GetFileExInfoStandard , &AttribData);
  43. printf("attribs = 0x%X\n", AttribData.dwFileAttributes);
  44. return;
  45. }
  46. else
  47. {
  48. printf("unknown code\n");
  49. return;
  50. }
  51. if (argc >= 4)
  52. {
  53. ShareMode = atoi(argv[3]);
  54. }
  55. if (argc >= 5)
  56. {
  57. Disposition = atoi(argv[4]);
  58. }
  59. printf ("share=%d,disposition=%d\n", ShareMode, Disposition);
  60. TestFile = CreateFile( argv[1],
  61. DesiredAccess,
  62. ShareMode,
  63. NULL,
  64. Disposition,
  65. FILE_ATTRIBUTE_NORMAL,
  66. NULL );
  67. if (TestFile == INVALID_HANDLE_VALUE)
  68. {
  69. printf("CreateFile failed 0x%X\n", GetLastError());
  70. return;
  71. }
  72. Length = wsprintf(sz, "This is a test file");
  73. if (argv[2][0] == '1')
  74. {
  75. HANDLE FileMapping;
  76. PVOID pFileMap;
  77. FileMapping = CreateFileMapping( TestFile,
  78. NULL,
  79. PAGE_READWRITE,
  80. 0,
  81. 19,
  82. NULL );
  83. if (FileMapping == NULL)
  84. {
  85. printf("CreateFileMapping failed 0x%X\n", GetLastError());
  86. return;
  87. }
  88. pFileMap = MapViewOfFile(FileMapping, FILE_MAP_WRITE, 0, 0, 19);
  89. if (pFileMap == NULL)
  90. {
  91. printf("MapViewOfFile failed 0x%X\n", GetLastError());
  92. return;
  93. }
  94. CopyMemory(pFileMap, sz, Length);
  95. if (UnmapViewOfFile(pFileMap) == FALSE)
  96. {
  97. printf("UnmapViewOfFile failed 0x%X\n", GetLastError());
  98. return;
  99. }
  100. CloseHandle(FileMapping);
  101. }
  102. else if (argv[2][0] == '0')
  103. {
  104. b = WriteFile( TestFile,
  105. sz,
  106. Length,
  107. &Length,
  108. NULL );
  109. if (b == FALSE)
  110. {
  111. printf("WriteFile failed 0x%X\n", GetLastError());
  112. return;
  113. }
  114. }
  115. else if (argv[2][0] == '2')
  116. {
  117. //
  118. // wait forever
  119. //
  120. printf("holding the file open...[ctl-c to break]\n");
  121. Sleep(INFINITE);
  122. }
  123. else if (argv[2][0] == '4')
  124. {
  125. LARGE_INTEGER ByteOffset;
  126. LARGE_INTEGER Length;
  127. NTSTATUS Status;
  128. IO_STATUS_BLOCK IoStatusBlock;
  129. //
  130. // lock the first byte of the file
  131. //
  132. ByteOffset.QuadPart = 0;
  133. Length.QuadPart = -1;
  134. Status = NtLockFile( TestFile,
  135. NULL,
  136. NULL,
  137. NULL, // ApcContext OPTIONAL,
  138. &IoStatusBlock,
  139. &ByteOffset,
  140. &Length,
  141. 1, // Key
  142. FALSE, // FailImmediately
  143. TRUE ); // Exclusive
  144. if (NT_SUCCESS(Status) == FALSE)
  145. {
  146. printf("NtLockFile failed 0x%X\n", Status);
  147. return;
  148. }
  149. //
  150. // wait forever
  151. //
  152. printf("holding the file open locked...[ctl-c to break]\n");
  153. Sleep(INFINITE);
  154. }
  155. printf("success\n");
  156. CloseHandle(TestFile);
  157. return;
  158. }