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.

173 lines
4.4 KiB

  1. //----------------------------------------------------------------------//
  2. //
  3. // Oct 1996 - fixed bug 54583 - simple typo in usage text //
  4. // a-martih (Martin Holladay)
  5. //
  6. //----------------------------------------------------------------------//
  7. #include <windows.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. SC_HANDLE hSvc;
  12. ENUM_SERVICE_STATUS SvcStat[1024];
  13. ULONG i;
  14. ULONG Resume;
  15. ULONG NumSvc;
  16. LPSTR p;
  17. LPSTR CmdLine;
  18. BOOL StoppedOnly;
  19. BOOL RunningOnly;
  20. CHAR MachineName[256];
  21. CHAR ch;
  22. int
  23. __cdecl
  24. main( void )
  25. {
  26. CmdLine = GetCommandLine();
  27. //
  28. // skip the program name
  29. //
  30. while( *CmdLine && *CmdLine != ' ' ) {
  31. CmdLine += 1;
  32. }
  33. //
  34. // skip any white space
  35. //
  36. while( *CmdLine && *CmdLine == ' ' ) {
  37. CmdLine += 1;
  38. }
  39. //
  40. // get the command line options
  41. //
  42. while( *CmdLine && (*CmdLine == '-' || *CmdLine == '/') ) {
  43. CmdLine += 1;
  44. ch = (CHAR)tolower(*CmdLine);
  45. CmdLine += 1;
  46. switch( ch ) {
  47. case 's':
  48. StoppedOnly = TRUE;
  49. break;
  50. case 'r':
  51. RunningOnly = TRUE;
  52. break;
  53. default:
  54. fputs("\n"
  55. "Microsoft (R) Windows NT (TM) Version 5.0 SCLIST\n"
  56. "Copyright (C) Microsoft Corp. All rights reserved\n\n"
  57. "usage: SCLIST [-?] [-r] [-s] [MachineName]\n"
  58. " [-?] Display this message\n"
  59. " [-r] Display only running services\n"
  60. " [-s] Display only stopped services\n"
  61. " [-MachineName] Machine name to list services\n",
  62. stderr );
  63. exit(0);
  64. }
  65. while( *CmdLine == ' ' ) {
  66. CmdLine += 1;
  67. }
  68. }
  69. //
  70. // get the machine name
  71. //
  72. if (*CmdLine) {
  73. //
  74. // skip any white space
  75. //
  76. while( *CmdLine && *CmdLine == ' ' ) {
  77. CmdLine += 1;
  78. }
  79. //
  80. // get the machine name
  81. //
  82. p = MachineName;
  83. while( *CmdLine && *CmdLine != ' ' ) {
  84. *p++ = *CmdLine;
  85. CmdLine += 1;
  86. }
  87. *p = 0;
  88. }
  89. hSvc = OpenSCManager( MachineName, NULL, SC_MANAGER_ENUMERATE_SERVICE );
  90. if (!hSvc) {
  91. printf( "could not open service manager for %s\n",
  92. MachineName[0] ? MachineName : "Local Machine" );
  93. return 1;
  94. }
  95. if (!EnumServicesStatus(
  96. hSvc,
  97. SERVICE_WIN32,
  98. SERVICE_ACTIVE | SERVICE_INACTIVE,
  99. SvcStat,
  100. sizeof(SvcStat),
  101. &i,
  102. &NumSvc,
  103. &Resume
  104. )) {
  105. printf( "could not enumerate services for %s\n",
  106. MachineName[0] ? MachineName : "Local Machine" );
  107. return 1;
  108. }
  109. printf( "\n" );
  110. printf( "--------------------------------------------\n" );
  111. printf( "- Service list for %s ", MachineName[0] ? MachineName : "Local Machine" );
  112. if (StoppedOnly || RunningOnly) {
  113. printf( "(" );
  114. if (RunningOnly) {
  115. printf( "running" );
  116. }
  117. if (StoppedOnly) {
  118. printf( "stopped" );
  119. }
  120. printf( ")" );
  121. }
  122. printf( "\n" );
  123. printf( "--------------------------------------------\n" );
  124. for (i=0; i<NumSvc; i++) {
  125. if (RunningOnly && SvcStat[i].ServiceStatus.dwCurrentState != SERVICE_RUNNING) {
  126. continue;
  127. }
  128. if (StoppedOnly && SvcStat[i].ServiceStatus.dwCurrentState != SERVICE_STOPPED) {
  129. continue;
  130. }
  131. switch( SvcStat[i].ServiceStatus.dwCurrentState ) {
  132. case SERVICE_STOPPED:
  133. p = "stopped";
  134. break;
  135. case SERVICE_START_PENDING:
  136. p = "start pending";
  137. break;
  138. case SERVICE_STOP_PENDING:
  139. p = "stop pending";
  140. break;
  141. case SERVICE_RUNNING:
  142. p = "running";
  143. break;
  144. case SERVICE_CONTINUE_PENDING:
  145. p = "continue pending";
  146. break;
  147. case SERVICE_PAUSE_PENDING:
  148. p = "pause pending";
  149. break;
  150. case SERVICE_PAUSED:
  151. p = "paused";
  152. break;
  153. }
  154. printf( "%-16s %-32s %s\n", p, SvcStat[i].lpServiceName, SvcStat[i].lpDisplayName );
  155. }
  156. return 0;
  157. }