Article View: comp.sys.amiga.tech
Article #12083Re: getcwd() and chdir() functions
From: Tomas G. Rokicki
Date: Sun, 02 Apr 1989 11:03
Date: Sun, 02 Apr 1989 11:03
105 lines
3288 bytes
3288 bytes
/*
* All you ever wanted to know about changing your current
* working directory. Make *sure* you call initcd() during
* startup. cleancd() can be called anytime as many times
* as you feel. cwd(s) returns a success code---1=success,
* and can be called at any point. getcwd() returns the
* name of the current working directory if you pass it "" as
* the first argument, or it returns the full name of the file
* referenced by the given name.
*
* This code assumes that no one else will change the current
* working directory out from under you.
*/
#include "exec/memory.h"
#include "libraries/dos.h"
#include "functions.h"
static struct Lock *original_lock = -1 ;
/*
* Call me before any others!
*/
initcd() {
register struct Lock *new_lock ;
new_lock = Lock("",SHARED_LOCK);
if (!new_lock)
error("! can't lock cwd") ;
original_lock = CurrentDir(new_lock);
}
/*
* Call me before you exit!
*/
cleancd() {
register struct Lock *new_lock ;
if (original_lock != -1) {
new_lock = CurrentDir(original_lock);
if (new_lock)
UnLock(new_lock);
original_lock = -1 ;
}
}
/*
* Call me to set the current working directory!
* Returns `0' on failure.
*/
int cwd(s)
char *s ;
{
register struct Lock *new_lock ;
new_lock = Lock(s,SHARED_LOCK);
if (!new_lock)
return(0) ;
new_lock = CurrentDir(new_lock);
if (new_lock)
UnLock(new_lock);
return(1) ;
}
/*
* Call me to get the full path name of a file! Pass "" as the name for
* a `pwd'. Give me the buffer to use and the size of that buffer.
*/
char *getcwd(name, buffer, size)
char *name ;
char *buffer ;
int size ;
{
register struct Lock *lock, *nlock ;
register int len ;
register struct FileInfoBlock *fib ;
register char *p ;
fib = (struct FileInfoBlock *)AllocMem((long)sizeof(struct FileInfoBlock),
MEMF_PUBLIC | MEMF_CLEAR) ;
size -= 2 ;
p = buffer + size + 1 ;
*p = 0 ;
if (fib) {
lock = Lock(name, SHARED_LOCK) ;
while (1) {
Examine(lock, fib) ;
len = strlen(fib->fib_FileName) ;
p -= len + 1 ;
if (p < buffer)
return(NULL) ;
strcpy(p, fib->fib_FileName) ;
p[len] = '/' ;
nlock = ParentDir(lock) ;
UnLock(lock) ;
if (nlock != NULL)
lock = nlock ;
else
break ;
}
p[len] = ':' ;
if (buffer[size] == '/')
buffer[size] = 0 ;
FreeMem(fib, (long)sizeof(struct FileInfoBlock)) ;
return(p) ;
} else
return(NULL) ;
}
-tom
Technical Details
Message-ID:
<8116@polya.Stanford.EDU>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!decwrl!shelby!polya!rokickiReferences:
<6481@dayton.UUCP>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!decwrl!shelby!polya!rokicki From: rokicki@polya.Stanford.EDU (Tomas G. Rokicki) Newsgroups: comp.sys.amiga.tech Subject: Re: getcwd() and chdir() functions Message-ID: <8116@polya.Stanford.EDU> Date: 2 Apr 89 11:03:13 GMT References: <6481@dayton.UUCP> Sender: Tomas G. Rokicki <rokicki@polya.Stanford.EDU> Reply-To: rokicki@polya.Stanford.EDU (Tomas G. Rokicki) Distribution: usa Organization: Stanford University Lines: 104