joe@dayton.UUCP (Joseph P. Larson) (04/02/89)
What is the *proper* way to perform an change-working-directory from C? Ditto a get-working-directory? (That is "cd place" and "cd" from the CLI.) As per Rob Peck's book, I did the following for the getcwd() -- me = FindTask(0); success = Examine(me->pr_CurrentDir, &fib); printf("Current directory: %s\n", fib.fib_FileName); However, this doesn't include the disk name. Arg. -- As for the chdir() -- if (i created an earlier directory lock) UnLock(oldlock); myLock = Lock(dname, ACCESS_READ); dummyreturn = ChangeDir(myLock); Rob's book indicates I shouldn't unlock any lock I didn't create. But doesn't this leave a lock on the old directory sitting around? Shouldn't I just do this: myLock = Lock(dname, ACCESS_READ); oldlock = ChangeDir(myLock); if (oldlock != NULL) UnLock(oldlock); But Rob's book says this'll piss off DOS. -- Am I going about all this the hard way? I'm using Manx and couldn't find equivalent routines in their library, but it *was* late and dark and.... -Joe -- Life is a cabaret (old chum). UUCP: rutgers!dayton!joe (Picts 1-13 are DHDSC - Joe Larson/MIS 1060 ATT : (612) 375-3537 now ready.) 700 on the Mall, Mpls, Mn. 55402
rokicki@polya.Stanford.EDU (Tomas G. Rokicki) (04/02/89)
/* * 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