oops, I didn't mean to commit that...
[reactos.git] / reactos / apps / utils / ps / ps.c
index 078c1ae..48c0908 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: ps.c,v 1.3 2004/07/29 22:31:55 jimtabor Exp $
+/* 
  *
  *  ReactOS ps - process list console viewer
  *
        Thanks to Filip Navara patch for fixing the Xp crash problem.
 */
 
+#define NTOS_MODE_USER
+#define WIN32_NO_STATUS
 #include <windows.h>
-/* NOTE: W32API ddk/ntapi.h header has wrong definition of SYSTEM_PROCESSES. */
-#include <ntos/types.h>
+#include <ndk/ntndk.h>
 
+typedef struct _SYSTEM_THREADS          
+ {      
+    LARGE_INTEGER KernelTime;
+    LARGE_INTEGER UserTime;
+    LARGE_INTEGER CreateTime;
+    ULONG WaitTime;
+    PVOID StartAddress;
+    CLIENT_ID ClientId;
+    KPRIORITY Priority;
+    LONG BasePriority;
+    ULONG ContextSwitches;
+    ULONG ThreadState;
+    ULONG WaitReason;
+ } SYSTEM_THREADS, *PSYSTEM_THREADS;    
+        
+ typedef struct _SYSTEM_PROCESSES       
+ {      
+    ULONG NextEntryOffset;
+    ULONG NumberOfThreads;
+    LARGE_INTEGER SpareLi1;
+    LARGE_INTEGER SpareLi2;
+    LARGE_INTEGER SpareLi3;
+    LARGE_INTEGER CreateTime;
+    LARGE_INTEGER UserTime;
+    LARGE_INTEGER KernelTime;
+    UNICODE_STRING ImageName;
+    KPRIORITY BasePriority;
+    HANDLE UniqueProcessId;
+    HANDLE InheritedFromUniqueProcessId;
+    ULONG HandleCount;
+    ULONG SessionId;
+    ULONG PageDirectoryFrame;
+
+    /*
+     * This part corresponds to VM_COUNTERS_EX.
+     * NOTE: *NOT* THE SAME AS VM_COUNTERS!
+     */
+    ULONG PeakVirtualSize;
+    ULONG VirtualSize;
+    ULONG PageFaultCount;
+    ULONG PeakWorkingSetSize;
+    ULONG WorkingSetSize;
+    ULONG QuotaPeakPagedPoolUsage;
+    ULONG QuotaPagedPoolUsage;
+    ULONG QuotaPeakNonPagedPoolUsage;
+    ULONG QuotaNonPagedPoolUsage;
+    ULONG PagefileUsage;
+    ULONG PeakPagefileUsage;
+    ULONG PrivateUsage;
+
+    /* This part corresponds to IO_COUNTERS */
+    LARGE_INTEGER ReadOperationCount;
+    LARGE_INTEGER WriteOperationCount;
+    LARGE_INTEGER OtherOperationCount;
+    LARGE_INTEGER ReadTransferCount;
+    LARGE_INTEGER WriteTransferCount;
+    LARGE_INTEGER OtherTransferCount;
+
+         SYSTEM_THREADS       Threads [1];      
+ } SYSTEM_PROCESSES, *PSYSTEM_PROCESSES;        
 
 //                     x00000000 00000000 000:00:00  000:00:00 ()
-static char* title  = "P     PID     PPID     KTime      UTime   NAME\n";
-static char* title1 = "t              TID     KTime      UTime   State      WaitResson\n";
-static char* title2 = "w     PID     Hwnd  WndStile        TID   WndName\n";
+static char title[]  = "P     PID     PPID     KTime      UTime   NAME\n";
+static char title1[] = "t              TID     KTime      UTime   State      WaitResson\n";
+static char title2[] = "w     PID     Hwnd  WndStile        TID   WndName\n";
 
 
 struct status {
     DWORD state;
-    char  desc[10];
+    const char  desc[10];
 }   thread_stat[8 + 1] = { 
     {0,        "Init      "},
     {1,        "Ready     "},
@@ -50,40 +112,47 @@ struct status {
 
 struct waitres {
     DWORD state;
-    char  desc[11];
-}   waitreason[28 + 1] = { 
-    {0,        "Executive  "},
-    {1,        "FreePage   "},
-    {2,        "PageIn     "},
-    {3,        "PoolAlloc  "},
-    {4,        "DelayExec  "},
-    {5,        "Suspended  "},
-    {6,        "UserReq    "},
-    {7, "WrExecutive"},
-    {8,        "WrFreePage "},
-    {9,        "WrPageIn   "},
-    {10,"WrPoolAlloc"},
-    {11,"WrDelayExec"},
-    {12,"WrSuspended"},
-    {13,"WrUserReq  "},
-    {14,"WrEventPair"},
-    {15,"WrQueue    "},
-    {16,"WrLpcRec   "},
-    {17,"WrLpcReply "},
-    {18,"WrVirtualMm"},
-    {19,"WrPageOut  "},
-    {20,"WrRendez   "},
-    {21,"Spare1     "},
-    {22,"Spare2     "},
-    {23,"Spare3     "},
-    {24,"Spare4     "},
-    {25,"Spare5     "},
-    {26,"Spare6     "},
-    {27,"WrKernel   "},
-    {-1,"     ?     "}
+    char  desc[17];
+}   waitreason[35 + 1] = { 
+   {0, "Executive        "},
+   {1, "FreePage         "},
+   {2, "PageIn           "},
+   {3, "PoolAllocation   "},
+   {4, "DelayExecution   "},
+   {5, "Suspended        "},
+   {6, "UserRequest      "},
+   {7, "WrExecutive      "},
+   {8, "WrFreePage       "},
+   {9, "WrPageIn         "},
+   {10,"WrPoolAllocation "},
+   {11,"WrDelayExecution "},
+   {12,"WrSuspended      "},
+   {13,"WrUserRequest    "},
+   {14,"WrEventPair      "},
+   {15,"WrQueue          "},
+   {16,"WrLpcReceive     "},
+   {17,"WrLpcReply       "},
+   {18,"WrVirtualMemory  "},
+   {19,"WrPageOut        "},
+   {20,"WrRendezvous     "},
+   {21,"Spare2           "},
+   {22,"WrGuardedMutex   "},
+   {23,"Spare4           "},
+   {24,"Spare5           "},
+   {25,"Spare6           "},
+   {26,"WrKernel         "},
+   {27,"WrResource       "},
+   {28,"WrPushLock       "},
+   {29,"WrMutex          "},
+   {30,"WrQuantumEnd     "},
+   {31,"WrDispatchInt    "},
+   {32,"WrPreempted      "},
+   {33,"WrYieldExecution "},
+   {34,"MaximumWaitReason"},
+   {-1,"       ?         "}
 };
 
-BOOL CALLBACK
+static BOOL CALLBACK
 EnumThreadProc(HWND hwnd, LPARAM lp)
 {
        DWORD r, pid, tid;
@@ -140,13 +209,13 @@ int main()
 
     /* For every process print the information. */
     CurrentProcess = SystemProcesses;
-    while (CurrentProcess->NextEntryDelta != 0)
+    while (CurrentProcess->NextEntryOffset != 0)
     {
         int hour, hour1, thour, thour1;
         unsigned char minute, minute1, tmin, tmin1;
         unsigned char  seconds, seconds1, tsec, tsec1;
 
-       int ti;
+       unsigned int ti;
        LARGE_INTEGER ptime;
 
        ptime.QuadPart = CurrentProcess->KernelTime.QuadPart;
@@ -159,17 +228,17 @@ int main()
        minute1  = (ptime.QuadPart / (10000000LL * 60LL)) % 60LL;
        seconds1 = (ptime.QuadPart / 10000000LL) % 60LL;
 
-       RtlUnicodeStringToAnsiString(&astring, &CurrentProcess->ProcessName, TRUE);
+       RtlUnicodeStringToAnsiString(&astring, &CurrentProcess->ImageName, TRUE);
 
         wsprintf(buf,"P%8d %8d %3d:%02d:%02d  %3d:%02d:%02d   ProcName: %s\n",
-                 CurrentProcess->ProcessId, CurrentProcess->InheritedFromProcessId,
+                 CurrentProcess->UniqueProcessId, CurrentProcess->InheritedFromUniqueProcessId,
                  hour, minute, seconds, hour1, minute1, seconds1,
                  astring.Buffer);
         WriteFile(stdout, buf, lstrlen(buf), &r, NULL);
         
         RtlFreeAnsiString(&astring);
 
-       for (ti = 0; ti < CurrentProcess->ThreadCount; ti++)
+       for (ti = 0; ti < CurrentProcess->NumberOfThreads; ti++)
           {
                struct status *statt;
                struct waitres *waitt;
@@ -180,13 +249,13 @@ int main()
                tmin  = (ptime.QuadPart / (10000000LL * 60LL)) % 60LL;
                tsec  = (ptime.QuadPart / 10000000LL) % 60LL;
 
-               ptime  = CurrentProcess->Threads[ti].UserTime;
+               ptime = CurrentProcess->Threads[ti].UserTime;
                thour1 = (ptime.QuadPart / (10000000LL * 3600LL));
                tmin1  = (ptime.QuadPart / (10000000LL * 60LL)) % 60LL;
                tsec1  = (ptime.QuadPart / 10000000LL) % 60LL;
 
                statt = thread_stat;
-                while (statt->state != CurrentProcess->Threads[ti].State  && statt->state >= 0)
+                while (statt->state != CurrentProcess->Threads[ti].ThreadState  && statt->state >= 0)
                        statt++;
 
                waitt = waitreason;
@@ -200,12 +269,13 @@ int main()
                          statt->desc , waitt->desc);
                WriteFile(stdout, buf1, lstrlen(buf1), &r, NULL);
 
-               EnumThreadWindows((DWORD)CurrentProcess->Threads[ti].ClientId.UniqueThread,
-                                 (ENUMWINDOWSPROC) EnumThreadProc,
+               EnumThreadWindows(PtrToUlong(CurrentProcess->Threads[ti].ClientId.UniqueThread),
+                                 (WNDENUMPROC) EnumThreadProc,
                                  (LPARAM)(LPTSTR) szWindowName );
           }
+
           CurrentProcess = (PSYSTEM_PROCESSES)((ULONG_PTR)CurrentProcess +
-                            CurrentProcess->NextEntryDelta);
+                            (ULONG_PTR)CurrentProcess->NextEntryOffset);
        } 
        return (0);
 }