2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
5 Desc: Find the whereabouts of an executable file or directory
9 /******************************************************************************
17 FILE/A, NORES/S, RES/S, ALL/S
25 Find and print the location of a specific program or directory.
26 Resident programs are marked as RESIDENT if they are not
27 interal resident in which case they are marked as INTERNAL.
29 Which searches the resident list, the current directory,
30 the command paths and the C: assign. If the item was not
31 found the condition flag is set to WARN but no error is
36 FILE -- the command/directory to search for
37 NORES -- don't include resident programs in the search
38 RES -- consider resident programs only
39 ALL -- find all locations of the FILE. This may cause the
40 printing of the same location several times, for
41 instance if the current directory is C: and the
56 Executable files in AROS currently haven't got the e-flag set,
57 which makes Which unusable for now in emulated mode.
61 09.02.1998 SDuvan -- implemented
62 11.11.2000 SDuvan -- rewrote most of the code and added
65 ******************************************************************************/
68 #include <aros/debug.h>
69 #include <proto/exec.h>
70 #include <proto/dos.h>
71 #include <exec/memory.h>
72 #include <dos/dosextens.h>
73 #include <utility/tagitem.h>
76 #define ARG_COUNT 4 /* Number of ReadArgs() arguments */
78 /* NOTE: For now, compatibility to the Amiga Which command is kept, but
79 I think that the restriction to only executable files should be
80 removed, especially considering soft links and such. */
84 * Check the resident list for the command 'name'.
86 BOOL
FindResidentCommand(STRPTR name
);
90 * Check the paths for the command 'name'.
92 BOOL
FindCommandinPath(STRPTR name
, BOOL checkAll
, struct FileInfoBlock
*fib
);
96 * Check the C: multiassign for the command 'name'.
98 BOOL
FindCommandinC(STRPTR name
, BOOL checkAll
, struct FileInfoBlock
*fib
);
102 * Look in the current directory for the command 'name'.
104 BOOL
CheckDirectory(STRPTR name
, struct FileInfoBlock
*fib
);
108 * Get a string that specifies the full path to a file or NULL if there
109 * was not enough memory to allocate the string. This string should be
110 * freed using FreeVec().
112 STRPTR
GetFullPath(BPTR lock
);
119 /* Array filled by ReadArgs() call */
120 IPTR args
[ARG_COUNT
] = {0, 0, 0, 0};
122 struct RDArgs
*rda
; /* ReadArgs standard struct */
123 BOOL found
= FALSE
; /* Indicates whether we've found a file
124 or not -- used for ALL ReadArgs() tag. */
125 int error
= RETURN_WARN
; /* Error value to return */
127 struct FileInfoBlock
*fib
; /* Used in Examine(). Allocated at top level
128 to skip multiple calls to
129 AllocDosObject() / FreeDosObject */
131 if((rda
= ReadArgs("FILE/A,NORES/S,RES/S,ALL/S", args
, NULL
)) != NULL
)
133 BOOL noRes
= (BOOL
)args
[1]; /* Don't check resident commands */
134 BOOL resOnly
= (BOOL
)args
[2]; /* Check resident commands only */
135 BOOL checkAll
= (BOOL
)args
[3]; /* Check for multiple occurances */
137 STRPTR commandName
= (STRPTR
)args
[0]; /* Command to look for */
139 fib
= AllocDosObject(DOS_FIB
, NULL
);
145 /* Check resident lists */
146 found
|= FindResidentCommand(commandName
);
147 // Printf("Resident list\n");
150 if(!found
&& !resOnly
)
152 /* Check all available paths */
153 found
|= FindCommandinPath(commandName
, checkAll
, fib
);
157 if(!found
&& !resOnly
)
159 /* Check C: multiassign */
160 found
|= FindCommandinC(commandName
, checkAll
, fib
);
168 FreeDosObject(DOS_FIB
, fib
);
175 PrintFault(IoErr(), "Which");
183 /* NOTE: The filesystemtask stuff is only necessary for this to work
184 correctly on AmigaOS. AROS doesn't use this concept. */
185 BOOL
FindCommandinC(STRPTR name
, BOOL checkAll
, struct FileInfoBlock
*fib
)
187 BOOL found
= FALSE
; /* Object found? */
188 struct DevProc
*dp
= NULL
, *dp2
; /* For GetDeviceProc() call */
189 BPTR oldCurDir
; /* Temporary holder of old current dir */
190 // struct MsgPort *oldFST; /* Temporary holder of old FileSysTask */
192 /* If FilePart(name) is not name itself, it can't be in the C: directory;
193 or rather, it isn't in the C: directory or we found it in
194 FindCommandinPath(). */
195 if(FilePart(name
) != name
)
198 oldCurDir
= CurrentDir(NULL
); /* Just to save the old current dir... */
199 // oldFST = GetFileSysTask(); /* ... and the filesystem task */
201 while(((dp2
= GetDeviceProc("C:", dp
)) != NULL
) && (!found
|| checkAll
))
203 // SetFileSysTask(dp2->dvp_Port);
204 CurrentDir(dp2
->dvp_Lock
);
205 found
|= CheckDirectory(name
, fib
);
207 /* Is this a multi assign? */
208 if(!(dp2
->dvp_Flags
& DVPF_ASSIGN
))
214 // SetFileSysTask(oldFST);
215 CurrentDir(oldCurDir
);
222 BOOL
FindCommandinPath(STRPTR name
, BOOL checkAll
, struct FileInfoBlock
*fib
)
224 BOOL found
; /* Have we found the 'file' yet? */
225 BPTR oldCurDir
; /* Space to store the current dir */
226 BPTR
*paths
; /* Loop variable */
228 struct CommandLineInterface
*cli
= Cli();
229 // Printf("checkAll = %ld\n", checkAll);
230 /* Can this happen at all? */
234 /* Check the current directory */
235 // Printf("Calling CheckDirectory()\n");
236 found
= CheckDirectory(name
, fib
);
238 oldCurDir
= CurrentDir(NULL
);
240 /* Check all paths */
241 paths
= (BPTR
*)BADDR(cli
->cli_CommandDir
);
242 // Printf("paths = 0x%08lx\n", paths);
244 while((!found
|| checkAll
) && (paths
!= NULL
))
246 CurrentDir(paths
[1]);
248 // Printf("Calling CheckDirectory()\n");
249 found
|= CheckDirectory(name
, fib
);
251 paths
= (BPTR
*)BADDR(paths
[0]); /* Go on with the next path */
254 CurrentDir(oldCurDir
);
260 BOOL
CheckDirectory(STRPTR name
, struct FileInfoBlock
*fib
)
262 BPTR lock
; /* Lock on 'name' */
263 BOOL found
= FALSE
; /* For return value purposes */
266 lock
= Lock(name
, SHARED_LOCK
);
268 // Printf("Locked command %s\n", name);
272 // Printf("Calling Examine()\n");
274 if(Examine(lock
, fib
) == DOSTRUE
)
276 // Printf("Calling GetFullPath()\n");
278 pathName
= GetFullPath(lock
);
282 /* File or directory? */
283 if(fib
->fib_DirEntryType
< 0)
285 /* FIBF_EXECUTE is active low! */
286 if(!(fib
->fib_Protection
& FIBF_EXECUTE
))
288 Printf("%s\n", pathName
);
294 /* Directories are always printed */
295 Printf("%s\n", pathName
);
299 FreeVec(pathName
); /* Free memory holding the full path name */
310 STRPTR
GetFullPath(BPTR lock
)
312 UBYTE
*buf
; /* Pointer to the memory allocated for the string */
313 ULONG size
; /* Holder of the (growing) size of the string */
315 for(size
= 512; ; size
+= 512)
317 buf
= AllocVec(size
, MEMF_ANY
);
322 if(NameFromLock(lock
, buf
, size
))
334 BOOL
FindResidentCommand(STRPTR name
)
336 BOOL found
= FALSE
; /* For return value purposes */
337 struct Segment
*seg
; /* Holder of segment if 'name' is a
340 /* Look in both system and normal list. Or rather, in the normal list
341 ONLY if it wasn't found in the system list. This is what the Amiga
342 Which does thus not giving the whole picture if you have 'cmd'
343 resident while 'cmd' is an internal command also. However, if this
344 is the case, you may never access it as the system list is searched
345 first by the Shell? */
346 if((seg
= FindSegment(name
, NULL
, TRUE
)) == NULL
)
348 seg
= FindSegment(name
, NULL
, FALSE
);
355 if(seg
->seg_UC
== CMD_INTERNAL
)
357 Printf("INTERNAL %s\n", name
);
359 else if(seg
->seg_UC
== CMD_DISABLED
)
361 Printf("INTERNAL %s ;(DISABLED)\n", name
);
364 Printf("RES %s\n", name
);