Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!cbmvax!carolyn From: carolyn@cbmvax.UUCP (Carolyn Scheppner CATS) Newsgroups: comp.sys.amiga Subject: Re: How can I get to the command search path? Message-ID: <2810@cbmvax.UUCP> Date: Tue, 17-Nov-87 19:18:20 EST Article-I.D.: cbmvax.2810 Posted: Tue Nov 17 19:18:20 1987 Date-Received: Fri, 20-Nov-87 04:28:55 EST References: <1538@tut.cis.ohio-state.edu> Reply-To: carolyn@cbmvax.UUCP (Carolyn Scheppner CATS) Organization: Commodore Technology, West Chester, PA Lines: 229 Keywords: which In article <1538@tut.cis.ohio-state.edu> mills-c@pike.cis.ohio-state.edu (Chris Mills) writes: > >I'm trying to write a program which, among other things, will do command and >filename completion on the Amiga. What I need to know is how do I get at >the search path (given by the PATH command)? I need to be able to search >all the directories from the current dir down to C:. I have lots of paths set up on my A2000, and recently got annoyed because I forgot where I had put a command I needed to update. So I wrote this. It contains code to search paths and to build full path names. ------- /* * which.c --- C. Scheppner CBM 11/87 * * Usage: which commandname * Tells you the path to that command */ #include #include #include #include #define SBUFSZ 256 #define CBUFSZ 80 extern BOOL getPath(); struct Path { BPTR path_Next; LONG path_Lock; }; main(argc,argv) int argc; char **argv; { struct Process *proc; struct CommandLineInterface *cli; struct Path *path; struct FileInfoBlock *fib; APTR oldWindowPtr; LONG lock, startcd; BOOL Found, InitialCD, FullPath; int i; char *command, sbuf[SBUFSZ], cbuf[CBUFSZ]; /* Fail from WB, give usage from CLI if command name not passed */ if(argc == 0) exit(RETURN_ERROR); if((argc != 2)||(*argv[1]=='?')) printf("Usage: which commandname\n"), exit(RETURN_OK); command = argv[1]; /* Fail if not CLI process */ proc = (struct Process *)FindTask(NULL); cli = (struct CommandLineInterface *)(proc->pr_CLI << 2); if(!cli) exit(RETURN_ERROR); /* Allocate a FileInfoBlock - must be longword aligned */ if(!(fib=(struct FileInfoBlock *) AllocMem(sizeof(struct FileInfoBlock),MEMF_PUBLIC|MEMF_CLEAR))) printf("Not enough memory\n"), exit(RETURN_FAIL); /* Save old WindowPtr, and disable volume requesters */ oldWindowPtr = proc->pr_WindowPtr; proc->pr_WindowPtr = (APTR)-1L; /* Were we given full path in command name ? */ for(FullPath = FALSE, i=0; icli_CommandDir); (path) && (!Found); path = (struct Path *) BADDR(path->path_Next)) { /* CD to each path */ lock = CurrentDir(path->path_Lock); if(InitialCD) startcd = lock, InitialCD = FALSE; /* See if command is there */ Found = getPath(command,fib,sbuf); } /* If we CD'd anywhere, restore initial CD */ if(! InitialCD) CurrentDir(startcd); } /* Check C: */ if((!Found)&&(!FullPath)) { strcpy(cbuf,"C:"); strcpy(&cbuf[2],command); if(Found = getPath(cbuf,fib,sbuf)) strcpy(sbuf,cbuf); } if(Found) printf("%s\n",sbuf); else printf("%s Not Found\n",command); /* Re-enable volume requesters */ proc->pr_WindowPtr = oldWindowPtr; /* We wouldn't have got here if we hadn't allocated this */ FreeMem(fib, sizeof(struct FileInfoBlock)); /* Return warning if not found */ exit(Found ? RETURN_OK : RETURN_WARN); } BOOL getPath(command,fib,buf) char *command; struct FileInfoBlock *fib; char *buf; { LONG lock; BOOL Success = FALSE; if(lock = Lock(command,ACCESS_READ)) { if(Examine(lock,fib)) { Success = TRUE; buildPath(lock,fib,buf); } UnLock(lock); } return(Success); } buildPath(inlock,fib,buf) LONG inlock; struct FileInfoBlock *fib; char *buf; { int i; LONG lock,oldlock; BOOL MyOldLock = FALSE; buf[0] = NULL; lock = inlock; while(lock) { if(Examine(lock,fib)) { if(fib->fib_FileName[0] > ' ') { if(buf[0]) insert(buf,"/"); insert(buf,fib->fib_FileName); } } oldlock = lock; lock = ParentDir(lock); if(MyOldLock) UnLock(oldlock); else MyOldLock = TRUE; } if(fib->fib_FileName[0] > ' ') { for(i=0; i<(strlen(buf)); i++) { if(buf[i] == '/') { buf[i] = ':'; break; } } } else insert(buf,"RAM:"); return(strlen(buf)); } insert(buf,s) char *buf,*s; { char tmp[SBUFSZ]; strcpy(tmp,buf); strcpy(buf,s); strcpy(&buf[strlen(s)],tmp); } strlen(s) char *s; { int i = 0; while(*s++) i++; return(i); } strcpy(to,from) char *to, *from; { do { *to++ = *from; } while(*from++); } /* end */ -- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= Carolyn Scheppner -- CATS >>Commodore Amiga Technical Support<< UUCP ...{allegra,ihnp4,rutgers}!cbmvax!carolyn PHONE 215-431-9180 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=