Dereference symlinks when copying.
[tangerine.git] / workbench / c / TaskList.c
blob6b58d79682bbf9e16716e4a57324ddb8d902aa8d
1 /*
2 Copyright © 1995-2007, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc:
6 Lang: english
7 */
9 #include <exec/memory.h>
10 #include <exec/tasks.h>
11 #include <exec/execbase.h>
12 #include <proto/exec.h>
13 #include <dos/dosextens.h>
14 #include <proto/dos.h>
16 const TEXT version[] = "$VER: tasklist 41.1 (14.3.1997)\n";
18 struct task
20 STRPTR name;
21 APTR address;
22 WORD type;
23 WORD state;
24 IPTR stacksize;
25 IPTR stackused;
26 WORD pri;
29 static int addtask(struct Task *task, struct task **t, STRPTR *e)
31 STRPTR s1,s2;
32 (*t)->address=task;
33 (*t)->type=task->tc_Node.ln_Type;
34 (*t)->pri =(WORD)task->tc_Node.ln_Pri;
35 (*t)->state=task->tc_State;
36 (*t)->stacksize=(STRPTR)task->tc_SPUpper-(STRPTR)task->tc_SPLower;
37 #if AROS_STACK_GROWS_DOWNWARDS
38 (*t)->stackused=(STRPTR)task->tc_SPUpper-SP_OFFSET-(STRPTR)task->tc_SPReg;
39 #else
40 (*t)->stackused=(STRPTR)task->tc_SPReg-SP_OFFSET-(STRPTR)task->tc_SPLower;
41 #endif
42 if(task->tc_State==TS_RUN)
43 /* The tc_SPReg for the actual process is invalid
44 if it had no context switch yet */
45 (*t)->stackused=0;
46 s1=task->tc_Node.ln_Name;
47 if(task->tc_Node.ln_Type==NT_PROCESS&&((struct Process *)task)->pr_CLI)
49 #warning TODO: Use cli_CommandName field for the name
50 (*t)->type=-1;
52 if(s1!=NULL)
54 s2=s1;
55 while(*s2++)
57 while(s2>s1)
59 if(*e<=(STRPTR)t)
60 return 0;
61 *--(*e)=*--s2;
64 if((STRPTR)(*t+1)>*e)
65 return 0;
66 (*t)->name=*e;
67 ++*t;
68 return 1;
71 static int fillbuffer(struct task **buffer, IPTR size)
73 STRPTR end=(STRPTR)*buffer+size;
74 struct Task *task;
75 Disable();
76 if(!addtask(SysBase->ThisTask,buffer,&end))
78 Enable();
79 return 0;
81 for(task=(struct Task *)SysBase->TaskReady.lh_Head;
82 task->tc_Node.ln_Succ!=NULL;
83 task=(struct Task *)task->tc_Node.ln_Succ)
84 if(!addtask(task,buffer,&end))
86 Enable();
87 return 0;
89 for(task=(struct Task *)SysBase->TaskWait.lh_Head;
90 task->tc_Node.ln_Succ!=NULL;
91 task=(struct Task *)task->tc_Node.ln_Succ)
92 if(!addtask(task,buffer,&end))
94 Enable();
95 return 0;
97 Enable();
98 return 1;
101 int __nocommandline;
103 int main(void)
105 IPTR size;
106 struct task *buffer,*tasks,*tasks2;
107 for(size=2048;;size+=2048)
109 buffer=AllocVec(size,MEMF_ANY);
110 if(buffer==NULL)
112 FPuts(Output(),"Not Enough memory for task buffer\n");
113 return 20;
115 tasks=buffer;
116 if(fillbuffer(&tasks,size))
118 FPuts(Output(),"address\t\ttype\tpri\tstate\tstack\tused\tname\n");
119 for(tasks2=buffer;tasks2<tasks;tasks2++)
121 IPTR args[7];
122 args[0]=(IPTR)tasks2->address;
123 args[1]=(IPTR)(tasks2->type==NT_TASK?"task":
124 tasks2->type==NT_PROCESS?"process":"CLI");
125 args[2]=tasks2->pri;
126 args[3]=(IPTR)(tasks2->state==TS_RUN?"running":
127 tasks2->state==TS_READY?"ready":"waiting");
128 args[4]=tasks2->stacksize;
129 args[5]=tasks2->stackused;
130 args[6]=tasks2->name!=NULL?(IPTR)tasks2->name:0;
131 VPrintf("0x%08.lx\t%s\t%ld\t%s\t%ld\t%ld\t%s\n",args);
133 FreeVec(buffer);
134 return 0;
136 FreeVec(buffer);