[net.unix] PWD Help Request

unix-wizards-request@brl-vgr@sri-unix.UUCP (10/13/83)

From:      Mike Muuss <unix-wizards-request@brl-vgr>


----- Forwarded message # 1:

Received: From Seismo.ARPA by BRL-VGR via smtp;  12 Oct 83 19:22 EDT
Date: 12 Oct 83 19:21:29 EDT (Wed)
From: rlgvax!cal-unix!wise@SEISMO
Subject: Getting the cwd from within a C program
Posted-Date: 12 Oct 83 19:21:29 EDT (Wed)
Message-Id: <8310122321.AA17334@SEISMO.ARPA>
Received: by SEISMO.ARPA (3.342/3.21)
	id AA17334; 12 Oct 83 19:21:29 EDT (Wed)
To: rlgvax!seismo!unix-wizards-request@BRL-VGR.ARPA


Does anybody out there know of an easy way to get the current directory
from within a C program?  Easy meaning not using popen ("pwd ... or
system ("pwd > ...  The envp argument to main doesn't include this
information.  We'd also like to avoid explicitly passing it from the
shell, as an argument or whatever.  Is the only choice to hack pwd.c
into a subroutine?  If so, and you've already done so, we'd appreciate
a copy.

Thanks in advance.

Rick Wise
wise@mit-ml





----- End of forwarded messages

guy@rlgvax.UUCP (Guy Harris) (10/17/83)

"getcwd", according to the S5 manual:

	This function is implemented by using popen(3S) to pipe the output
	of the pwd(1) command into the specified string space.

So if the requestor's objections to getting the current working directory by
running the "pwd" command are philosophical (i.e., it's not just the code
needed to put in the "popen"), "getcwd" isn't their cup of tea.

	Guy Harris
	{seismo,mcnc,brl-bmd,allegra}!rlgvax!guy

gwyn@brl-vld@sri-unix.UUCP (10/19/83)

From:      Doug Gwyn (VLD/VMB) <gwyn@brl-vld>

getcwd(3c) is the standard UNIX C library function for getting the
pathname of the current working directory.  However, you will not
find this on pre-System V UNIXes.  If you have a license you could
just obtain a copy of the source and add it to your C library.

thomas@utah-gr.UUCP (Spencer W. Thomas) (10/24/83)

Another reason for using the popen("pwd"...) hack is that you can make
pwd to be setuid (root), so when you are trying to find out the current
directory from within a setuid program which does not have access to the
path leading to the cwd, you can still do it.  (That's pretty
convoluted, let me make an example.)

Suppose that ~ is rwx------, but ~/foo is rwxr-xr-x.  You are connected
to ~/foo and you say
	uucp -c bar.c baz!~uucp/bar.x
Uucp forks pwd to find out your cwd, and if pwd is vanilla, uucp gives
up because it can't figure out the name of the working directory, even
though it has access to the file, and can copy it to ~uucp.  If the
getcwd routine were built into uucp, there would be no hope in this case.

=Spencer