Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!agate!paris.Berkeley.EDU!mcgrath From: mcgrath@paris.Berkeley.EDU (Roland McGrath) Newsgroups: comp.unix.wizards Subject: How to write a current directory finder? Message-ID: Date: 2 Apr 89 05:08:07 GMT Sender: usenet@agate.BERKELEY.EDU Organization: Hackers Anonymous International, Ltd., Inc. (Applications welcome) Lines: 27 I've written a `getcwd' function (finds the absolute pathname of the current working directory), and it works; but it's immensely slow! The method I've used is this: Starting with `.', get the device and i-node numbers of each directory, back up to `..', search the directory, checking each file for matching numbers, and when one is found, add its name to the beginning of the path. Then, get the device and i-node numbers of that directory, back up to its parent, and search for matching numbers, repeating until a directory and its parent have the same device and i-node numbers (because /.. is /). Since my function is about 10 times slower than /bin/pwd, this is obviously not the optimum method. Since the manpage for `getwd' on my system (Sun Unix 3.2) warns that a failing `getwd' call may leave the current directory in the wrong place, I assume that `getwd' moves around in the process of figuring it out. But I don't see how this helps, using the same basic logic as my method. You trade time constructing a big `../../../../..' name and using that in all the lookups for moving around and cutting out all the `..'s. But can that really make such a difference? -- Roland McGrath Free Software Foundation, Inc. roland@wheaties.ai.mit.edu, mit-eddie!wheaties.ai.mit.edu!roland Copyright 1989 Roland McGrath, under the GNU General Public License, version 1.