Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!cs.utexas.edu!ut-emx!perv.pe.utexas.edu!pefv700 From: pefv700@perv.pe.utexas.edu Newsgroups: comp.unix.programmer Subject: getting the current working directory Message-ID: <42380@ut-emx.uucp> Date: 11 Jan 91 00:45:57 GMT Sender: news@ut-emx.uucp Distribution: usa Organization: Dept. of Petroleum Engineering, The University of Texas at Aust Lines: 42 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