New bitmap method SetRGBConversionFunction which can be used to
[tangerine.git] / workbench / c / Which.c
blob0502fdcc67cbb0597c60aa849e106411500e3f81
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Find the whereabouts of an executable file or directory
6 Lang: English
7 */
9 /******************************************************************************
11 NAME
13 Which
15 SYNOPSIS
17 FILE/A, NORES/S, RES/S, ALL/S
19 LOCATION
21 Workbench:C
23 FUNCTION
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
32 printed.
34 INPUTS
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
42 FILE was found in C:
44 RESULT
46 NOTES
48 EXAMPLE
50 BUGS
52 SEE ALSO
54 INTERNALS
56 Executable files in AROS currently haven't got the e-flag set,
57 which makes Which unusable for now in emulated mode.
59 HISTORY
61 09.02.1998 SDuvan -- implemented
62 11.11.2000 SDuvan -- rewrote most of the code and added
63 correct path support
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>
74 #include <stdio.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);
115 int __nocommandline;
117 int main(void)
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);
141 if(fib != NULL)
143 if(!noRes)
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);
154 // Printf("Path\n");
157 if(!found && !resOnly)
159 /* Check C: multiassign */
160 found |= FindCommandinC(commandName, checkAll, fib);
161 // Printf("C:\n");
163 if (found)
165 error = RETURN_OK;
168 FreeDosObject(DOS_FIB, fib);
171 FreeArgs(rda);
173 else
175 PrintFault(IoErr(), "Which");
176 error = RETURN_FAIL;
179 return error;
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)
196 return FALSE;
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))
209 break;
211 dp = dp2;
214 // SetFileSysTask(oldFST);
215 CurrentDir(oldCurDir);
216 FreeDeviceProc(dp);
218 return found;
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? */
231 if(cli == NULL)
232 return FALSE;
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);
256 return found;
260 BOOL CheckDirectory(STRPTR name, struct FileInfoBlock *fib)
262 BPTR lock; /* Lock on 'name' */
263 BOOL found = FALSE; /* For return value purposes */
264 STRPTR pathName;
266 lock = Lock(name, SHARED_LOCK);
268 // Printf("Locked command %s\n", name);
270 if(lock != NULL)
272 // Printf("Calling Examine()\n");
274 if(Examine(lock, fib) == DOSTRUE)
276 // Printf("Calling GetFullPath()\n");
278 pathName = GetFullPath(lock);
280 if(pathName != NULL)
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);
289 found = TRUE;
292 else
294 /* Directories are always printed */
295 Printf("%s\n", pathName);
296 found = TRUE;
299 FreeVec(pathName); /* Free memory holding the full path name */
303 UnLock(lock);
306 return found;
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);
319 if(buf == NULL)
320 break;
322 if(NameFromLock(lock, buf, size))
324 return (STRPTR)buf;
327 FreeVec(buf);
330 return NULL;
334 BOOL FindResidentCommand(STRPTR name)
336 BOOL found = FALSE; /* For return value purposes */
337 struct Segment *seg; /* Holder of segment if 'name' is a
338 resident command */
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);
351 if(seg != NULL)
353 found = TRUE;
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);
363 else
364 Printf("RES %s\n", name);
367 return found;