Updated PCI IDs to latest snapshot.
[tangerine.git] / workbench / c / shellcommands / Resident.c
blob5a436cf84bc377782dc4a85ee55c124a3f2c3bdb
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Resident CLI command
6 Lang: English
7 */
10 #include <proto/dos.h>
11 #include <dos/dosextens.h>
12 #include <string.h>
13 #include <exec/lists.h>
14 #include <exec/nodes.h>
15 #include <exec/memory.h>
17 #include <string.h>
19 #include <aros/shcommands.h>
21 struct SegNode
23 struct MinNode node;
24 IPTR data[2];
27 static struct SegNode *NewSegNode(struct ExecBase *SysBase, STRPTR name, LONG uc);
29 AROS_SH7(Resident, 41.1,
30 AROS_SHA(STRPTR, ,NAME, ,NULL),
31 AROS_SHA(STRPTR, ,FILE, ,NULL),
32 AROS_SHA(BOOL, ,REMOVE,/S,FALSE),
33 AROS_SHA(BOOL, ,ADD,/S,FALSE),
34 AROS_SHA(BOOL, ,REPLACE,/S,FALSE),
35 AROS_SHA(BOOL,PURE=,FORCE,/S,FALSE),
36 AROS_SHA(BOOL, ,SYSTEM,/S,FALSE))
38 AROS_SHCOMMAND_INIT
41 if (SHArg(FILE) || SHArg(NAME))
43 STRPTR name, file;
44 BPTR seglist;
45 struct FileInfoBlock *fib;
47 if (SHArg(FILE))
49 name = SHArg(NAME);
50 file = SHArg(FILE);
52 else
54 name = FilePart(SHArg(NAME));
55 file = SHArg(NAME);
58 SetIoErr(0);
59 if (SHArg(REMOVE))
61 struct Segment *found;
63 Forbid();
64 found = FindSegment(name, NULL, TRUE);
65 if (!found)
67 Permit();
68 SetIoErr(ERROR_OBJECT_NOT_FOUND);
70 else
71 if (!RemSegment(found))
73 if (found->seg_UC == CMD_INTERNAL)
74 found->seg_UC = CMD_DISABLED;
75 else
76 SetIoErr(ERROR_OBJECT_IN_USE);
79 if (IoErr())
81 PrintFault(IoErr(), SHArg(NAME));
82 return RETURN_FAIL;
85 return RETURN_OK;
88 if (SHArg(REPLACE))
90 struct Segment *found;
92 Forbid();
93 found = FindSegment(name, NULL, TRUE);
94 if (found && found->seg_UC == CMD_DISABLED)
96 found->seg_UC = CMD_INTERNAL;
97 Permit();
98 return RETURN_OK;
100 Permit();
103 if (!SHArg(ADD)) SHArg(REPLACE) = TRUE;
105 if (!SHArg(FORCE) && (fib = (struct FileInfoBlock *)AllocDosObject(DOS_FIB, NULL)))
107 BPTR lock;
109 if ((lock = Lock(file, SHARED_LOCK)))
111 if (Examine(lock, fib))
113 if (fib->fib_Protection & FIBF_PURE)
114 SetIoErr(ERROR_OBJECT_WRONG_TYPE);
117 UnLock(lock);
120 FreeDosObject(DOS_FIB, fib);
123 if (IoErr() || !(seglist = LoadSeg(file)))
125 PrintFault(IoErr(), file);
126 return RETURN_FAIL;
129 if (SHArg(REPLACE))
131 struct Segment *found;
133 Forbid();
134 found = FindSegment(name, NULL, FALSE);
136 if (found)
138 if (found->seg_UC != 0)
140 Permit();
141 PrintFault(ERROR_OBJECT_IN_USE, file);
142 UnLoadSeg(seglist);
143 return RETURN_FAIL;
146 UnLoadSeg(found->seg_Seg);
147 found->seg_Seg = seglist;
148 Permit();
150 return RETURN_OK;
152 /* Fall through */
155 if (!AddSegment(name, seglist, SHArg(SYSTEM)?CMD_SYSTEM:0))
157 UnLoadSeg(seglist);
158 PrintFault(IoErr(), "Resident");
159 return RETURN_FAIL;
162 else
165 struct MinList l;
166 struct Segment *curr;
167 struct SegNode *n;
169 NEWLIST((struct List *)&l);
171 SetIoErr(0);
172 Forbid();
173 curr = (struct Segment *)BADDR(DOSBase->dl_ResList);
174 while (curr)
176 n = NewSegNode(SysBase, AROS_BSTR_ADDR(curr->seg_Name), curr->seg_UC);
178 if (!n)
180 SetIoErr(ERROR_NO_FREE_STORE);
181 break;
184 AddTail((struct List *)&l, (struct Node *)n);
185 curr = (struct Segment *)BADDR(curr->seg_Next);
187 Permit();
189 if (IoErr())
191 PrintFault(IoErr(), "Resident");
192 return RETURN_FAIL;
195 PutStr("NAME USECOUNT\n\n");
196 while ((n = (struct SegNode *)RemHead((struct List *)&l)))
198 if (n->data[1] == CMD_SYSTEM)
199 VPrintf("%-30s SYSTEM\n", n->data);
200 else
201 if (n->data[1] == CMD_INTERNAL)
202 VPrintf("%-30s INTERNAL\n", n->data);
203 else
204 if (n->data[1] == CMD_DISABLED)
205 VPrintf("%-30s DISABLED\n", n->data);
206 else
207 VPrintf("%-30s %-ld\n", n->data);
209 FreeVec((APTR)n->data[0]);
210 FreeVec(n);
215 return RETURN_OK;
217 AROS_SHCOMMAND_EXIT
220 static STRPTR StrDup(struct ExecBase *SysBase, STRPTR str)
222 size_t len = strlen(str)+1;
223 STRPTR ret = (STRPTR) AllocVec(len, MEMF_ANY);
225 if (ret)
227 CopyMem(str, ret, len);
230 return ret;
234 static struct SegNode *NewSegNode(struct ExecBase *SysBase, STRPTR name,
235 LONG uc)
237 struct SegNode *sn = AllocVec(sizeof (struct SegNode), MEMF_ANY);
239 if (sn)
241 sn->data[0] = (IPTR) StrDup(SysBase, name);
242 if (sn->data[0])
244 sn->data[1] = uc;
245 return sn;
248 FreeVec(sn);
251 return NULL;