name of current dir in kernel

Guy Harris guy at auspex.UUCP
Sat Nov 26 05:52:56 AEST 1988


>What i am looking for is an EASY and FAST way to get the name of the
>current directory of the program that is running.

The kernel generally does not keep that information around.  On many
systems, "getwd" runs "pwd", which basically starts at the current
directory, looks at that directory's parent to see what the name of the
current directory is in that parent directory, and repeats the process
until it hits the root directory.  Other systems have a library routine
that encapsulates "pwd"s algorithm.

This is not quick, in general.  You may be able to speed it up by:

	1) noting that some shells set up an environment variable that
	   purports to be the current directory; you can get that
	   variable, "stat" both it and "." and make sure it really *is*
	   the current directory, and if so use its value

	2) getting the current directory only once and remembering it;
	   if your program executes a "chdir" system call, it can either
	   recompute the current directory or attempt to combine the
	   argument to "chdir" with the remembered current directory.



More information about the Comp.unix.wizards mailing list