I have tried to modified the code below found on the net
- so as to compile for gcc
- so as tot always return a Lock on the fullpath (if it can be found) of the file that could be somewhere in the path ( i suppressed the "if (file > 0)
block)
1. I had a strange crash in my version below, see the comments anq question marks
Maybe there is an explanation in the code above, that i don't see. I hope so.
Otherwise it may be due to the following :
findpath() is used in the Gui4Cli launcher (name 'Gui' gui.c source is available) which uses Olaf Barthel's wbpath.c that Thomas was able to dig out.
findpath() could be a SAS/C provided function. The findpath.c is my reconstruction based on a non SAS/C findpath.c source.
If somebody has the latter source that would help too of course
2. I wondered for the first source what IoErr() really could detect. (where a significant IoErr() was set. Indeed, the autodocs neither for Lock() Cli() nor CurrentDir() mention even IoErr().
/* SAS/C compatibility This function locates a file in the currently defined path. If the process is not a Shell process, it uses the path in effect when Workbench was loaded. If the findpath function finds the file, it returns a lock on that directory even if it is the current directory. The lock must be unlocked with the AmigaDOS UnLock function. If the findpath function cannot find the file, it returns a -1. The value NULL is not used because NULL is a valid lock. */ #include <proto/dos.h> void* findpath(const char *filename) { BPTR file = Lock(filename, SHARED_LOCK); if (file <= 0) { struct CommandLineInterface *cli = Cli(); BPTR oldCurDir = CurrentDir(NULL); BPTR *paths = BADDR(cli->cli_CommandDir); if (IoErr() == ERROR_OBJECT_IN_USE) return -1; for (; !file && paths; paths = BADDR(paths[0])) { CurrentDir(paths[1]); file = Lock(filename, SHARED_LOCK); } CurrentDir(oldCurDir); } if (file > 0) { BPTR dir = ParentOfFH(file); UnLock(file); file = dir; } return file ? file : -1; }
#define __USE_INLINE__ #define ZERO ((BPTR)NULL) //OS4 added #include <proto/dos.h> #include <dos/dosextens.h> /// added BPTR findpath(const char *filename) /// filename used is Gui4Cli and we expect this file to be { struct CommandLineInterface *cli; BPTR file; BPTR *paths; BPTR oldCurDir; char dirname[200]; oldCurDir = CurrentDir(ZERO); NameFromLock(oldCurDir, dirname, 180); Printf("current dir %s\n",dirname); /// prints datas:Gui4Cli, wher Gui4Cli lives Printf("filename %s\n", filename); /// prints Gui4Cli if ((file = Lock(filename, SHARED_LOCK))) { Printf("Lock ok\n"); /// not seen ???? why ????????? UnLock(file); } if (!(file = Lock(filename, SHARED_LOCK))) /// not in the current Dir or in the path { PrintFault(IoErr(),NULL); /// no output Printf("could not lock %s\n", filename); //// this is printed out ???? if ((cli = Cli())) { ///oldCurDir = CurrentDir(ZERO); // was NULL); paths = BADDR(cli->cli_CommandDir); // used to crash in some cases } if (IoErr() == ERROR_OBJECT_IN_USE) //only possible value ? why return ((BPTR)(-1)) ; //-1; ///else PrintFault(IoErr(),NULL); for (; !file && paths; paths = BADDR(paths[0])) ///searchin in the path till Lock ok { CurrentDir(paths[1]); file = Lock(filename, SHARED_LOCK); ///>>>>> crash here now } CurrentDir(oldCurDir); // the original current directory } return file ? file : ((BPTR)(-1));//-1; }