getting the current working directory
pefv700 at perv.pe.utexas.edu
pefv700 at perv.pe.utexas.edu
Fri Jan 11 11:45:57 AEST 1991
Like the subject says, I want to get the current working directory.
According to my SunOS man page on getwd(3), getwd(buf) will put the absolute
path of the current working directory in buf UNLESS there is an error, in
which case buf contains some message. The problem is, you don't know if you
got the directory name or the message.
I can't find any other std library function or system call that will get the
job done right (using malloc if necessary). So...to write a "good" getwd,
it seems you would have to do something ugly like (skipping error checking)
getwd(buf)
char *buf;
{
ino_t inode;
struct stat stbuf;
DIR *dfd;
struct dirent *dp;
stat(".", &stbuf);
inode = stbuf.st_ino;
if (. is /) { /* not sure how to do this, stat .. and check inodes? */
change buf to include / using [m,re]alloc as necessary
return;
} else
chdir("..");
dfd = opendir(".");
while ((dp = readdir(dfd)) != NULL) {
stat(dp->d_name, &stbuf);
if (stbuf.st_ino == inode) {
change buf to include dp->d_name using [m,re]alloc as necessary
break;
}
getwd(buf);
}
Is there a better way, a function I'm missing...?
Thanks,
Chris
More information about the Comp.unix.programmer
mailing list