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.

150 lines
4.5 KiB

  1. #include <windows.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #define MAX_WAIT_HANDLES MAXIMUM_WAIT_OBJECTS
  6. __cdecl
  7. main (c, v)
  8. int c;
  9. char *v[];
  10. {
  11. HANDLE hEvents[ 4 * MAX_WAIT_HANDLES ];
  12. char *EventNames[ 4 * MAX_WAIT_HANDLES ];
  13. char *s;
  14. ULONG nEvents, dwWaitResult;
  15. BOOLEAN fWaitAny = FALSE;
  16. BOOLEAN fWait = FALSE;
  17. BOOLEAN fVerbose = FALSE;
  18. BOOLEAN fUsage = TRUE;
  19. nEvents = 0;
  20. while (--c) {
  21. s = *++v;
  22. if (!_stricmp( s, "-a" )) {
  23. fWaitAny = TRUE;
  24. fWait = TRUE;
  25. }
  26. else
  27. if (!_stricmp( s, "-w" ))
  28. fWait = TRUE;
  29. else
  30. if (!_stricmp( s, "-v" ))
  31. fVerbose = TRUE;
  32. else
  33. if (nEvents == (4 * MAX_WAIT_HANDLES)) {
  34. fprintf( stderr, "CMDEVENT: Cant wait on more than %u events.\n",
  35. 4 * MAX_WAIT_HANDLES
  36. );
  37. exit( -1 );
  38. }
  39. else {
  40. fUsage = FALSE;
  41. if (nEvents == MAX_WAIT_HANDLES && !fVerbose) {
  42. fprintf( stderr, "CMDEVENT: Waiting on more than %u events. Forcing -v\n",
  43. MAX_WAIT_HANDLES
  44. );
  45. fVerbose = TRUE;
  46. }
  47. {
  48. char *d;
  49. d = s;
  50. while (*d) {
  51. if (*d == '\\') {
  52. *d = '_';
  53. }
  54. d++;
  55. }
  56. }
  57. hEvents[ nEvents ] = fWait ? CreateEvent( NULL, TRUE, FALSE, s )
  58. : OpenEvent( EVENT_ALL_ACCESS, FALSE, s );
  59. if (hEvents[ nEvents ] == NULL) {
  60. fprintf( stderr, "CMDEVENT: Unable to %s event named '%s' - %u\n",
  61. fWait ? "create" : "open",
  62. s,
  63. GetLastError()
  64. );
  65. exit( -1 );
  66. break;
  67. }
  68. else
  69. if (!fWait) {
  70. if (!SetEvent( hEvents[ nEvents ] )) {
  71. fprintf( stderr, "CMDEVENT: Unable to signal event named '%s' - %u\n",
  72. s,
  73. GetLastError()
  74. );
  75. }
  76. }
  77. else {
  78. EventNames[ nEvents ] = s;
  79. nEvents += 1;
  80. }
  81. }
  82. }
  83. if (fUsage) {
  84. fprintf( stderr, "usage: CMDEVENT [-w] [-v] EventName(s)...\n" );
  85. exit( -1 );
  86. }
  87. else
  88. if (fWait) {
  89. if (fVerbose) {
  90. fprintf( stderr, "\nWaiting for %u events:", nEvents );
  91. fflush( stderr );
  92. }
  93. while (nEvents) {
  94. dwWaitResult = WaitForMultipleObjects( nEvents > MAX_WAIT_HANDLES ?
  95. MAX_WAIT_HANDLES :
  96. nEvents,
  97. hEvents,
  98. FALSE,
  99. INFINITE
  100. );
  101. if (dwWaitResult == WAIT_FAILED) {
  102. fprintf( stderr, "\nCMDEVENT: Unable to wait for event(s) - %u\n",
  103. GetLastError()
  104. );
  105. exit( -1 );
  106. }
  107. else
  108. if (dwWaitResult < nEvents) {
  109. CloseHandle( hEvents[ dwWaitResult ] );
  110. if (fVerbose) {
  111. fprintf( stderr, " %s", EventNames[ dwWaitResult ] );
  112. fflush( stderr );
  113. }
  114. if (fWaitAny) {
  115. exit( dwWaitResult+1 );
  116. }
  117. nEvents -= 1;
  118. if (dwWaitResult < nEvents) {
  119. memmove( &hEvents[ dwWaitResult ],
  120. &hEvents[ dwWaitResult+1 ],
  121. (nEvents - dwWaitResult + 1) * sizeof( hEvents[ 0 ] )
  122. );
  123. memmove( &EventNames[ dwWaitResult ],
  124. &EventNames[ dwWaitResult+1 ],
  125. (nEvents - dwWaitResult + 1) * sizeof( EventNames[ 0 ] )
  126. );
  127. }
  128. }
  129. }
  130. if (fVerbose) {
  131. fprintf( stderr, "\n" );
  132. }
  133. }
  134. exit( 0 );
  135. return( 0 );
  136. }