return the created object..
[AROS.git] / rom / dos / rootnode.c
blobbb1ec4dd471a58da15b9b404dd0b8eb97655d431
1 /*
2 Copyright © 1995-2007, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Miscellaneous functions for dealing with DOS rootnode.
6 Lang:
7 */
9 #include <aros/debug.h>
11 #include <exec/lists.h>
12 #include <exec/types.h>
13 #include <exec/memory.h>
14 #include <proto/exec.h>
15 #include <dos/dosextens.h>
16 #include "dos_intern.h"
19 /* Add a CLI process to the RootNode structure. This is used by commands
20 as C:Status and functions like dos.library/MaxCli() */
21 void addprocesstoroot(struct Process *process, struct DosLibrary *DOSBase)
23 IPTR *taskarray;
24 IPTR *newtaskarray;
25 ULONG size;
26 ULONG i;
28 struct CommandLineInterface *cli =
29 (struct CommandLineInterface *)BADDR(process->pr_CLI);
31 struct RootNode *root = DOSBase->dl_Root;
33 struct CLIInfo *ci;
35 D(bug("Calling addprocesstoroot(%p) with cli = %p\n", process, cli));
37 if(cli == NULL)
38 return;
40 ci = (struct CLIInfo *)AllocVec(sizeof(struct CLIInfo), MEMF_PUBLIC);
42 if(ci == NULL)
43 return;
45 ObtainSemaphore(&root->rn_RootLock);
47 D(bug("[addprocesstoroot] Adding to CliList\n"));
48 /* Set the node's name to the process' name so we may use FindName()
49 on the rn_CliList to locate a specific command */
51 /* This is kind of hacky but doing it another way will be more
52 troublesome; we rely here that even BSTR:s have a trailing 0. */
54 ci->ci_Process = process;
55 ci->ci_Node.ln_Name = AROS_BSTR_ADDR(cli->cli_CommandName);
57 /* Can't use AddTail() here as it complains about the list pointer */
58 ADDTAIL((struct List *)&root->rn_CliList, (struct Node *)ci);
60 D(bug("[addprocesstoroot] Adding to TaskArray\n"));
61 taskarray = BADDR(root->rn_TaskArray);
62 size = taskarray[0];
65 ** Check out the taskarray for an empty slot
67 i = 1;
69 while(i <= size)
71 if(0 == taskarray[i])
73 taskarray[i] = (IPTR)&process->pr_MsgPort;
74 process->pr_TaskNum = i;
75 goto done;
78 i++;
82 ** it seems like a new taskarray is needed
84 newtaskarray = AllocMem(sizeof(IPTR) + (size + 1)*sizeof(APTR), MEMF_ANY);
86 newtaskarray[0] = size + 1;
87 i = 1;
89 while(i <= size)
91 newtaskarray[i] = taskarray[i];
92 i++;
95 newtaskarray[size + 1] = (IPTR)&process->pr_MsgPort;
96 process->pr_TaskNum = size + 1;
98 root->rn_TaskArray = MKBADDR(newtaskarray);
100 FreeMem(taskarray, sizeof(IPTR) + size*sizeof(APTR));
102 done:
103 D(bug("Returning from addprocesstoroot() (%d)\n", process->pr_TaskNum));
104 ReleaseSemaphore(&root->rn_RootLock);
108 void removefromrootnode(struct Process *process, struct DosLibrary *DOSBase)
110 IPTR *taskarray;
111 struct CLIInfo *ci, *tmp;
113 struct RootNode *root = DOSBase->dl_Root;
115 D(bug("[removefromrootnode] %p, TaskNum %d\n", process, process->pr_TaskNum));
117 if (!__is_process(process) || process->pr_CLI == BNULL)
119 D(bug("[removefromrootnode] Strange. Doesn't seem be a CLI...\n"));
120 return;
123 ObtainSemaphore(&root->rn_RootLock);
125 ForeachNodeSafe(&root->rn_CliList, ci, tmp) {
126 if (ci->ci_Process == process) {
127 D(bug("[removefromrootnode] Removing from CLIList\n"));
128 Remove((struct Node *)ci);
129 FreeVec(ci);
133 D(bug("[removefromrootnode] Removing from TaskArray\n"));
134 taskarray = BADDR(root->rn_TaskArray);
135 taskarray[process->pr_TaskNum] = 0;
137 ReleaseSemaphore(&root->rn_RootLock);