[comp.unix.programmer] how to change parent environment?

njacobs@kong.gsfc.nasa.gov (Nick Jacobs - EOS) (05/03/91)

How do you change the current working directory in a program, so
that when the program is invoked from a shell, the cwd stays
changed after the program exits?
You can't do it with chdir(2) of course, because that only affects
the current process.

Nick

flee@cs.psu.edu (Felix Lee) (05/03/91)

>How do you change the current working directory in a program, so
>that when the program is invoked from a shell, the cwd stays
>changed after the program exits?

Use ptrace().  In the child process you PTRACE_ATTACH the parent, save
the pc and registers and errno, find some spare room in the parent's
address space, write some code that will call chdir() and stop,
continue the parent at the code you've inserted, restore the pc and
registers and errno, and PTRACE_DETACH.

Implementation left as an exercise to the reader.
--
Felix Lee	flee@cs.psu.edu

mike@bria.UUCP (mike.stefanik) (05/04/91)

In an article, njacobs@kong.gsfc.nasa.gov (Nick Jacobs - EOS) writes:
|How do you change the current working directory in a program, so
|that when the program is invoked from a shell, the cwd stays
|changed after the program exits?
|
|You can't do it with chdir(2) of course, because that only affects
|the current process.

Seems that you have answered your own question.  The current working
directory is unique to each process.  It is inherited in only one
direction -- parent to child. 

Yes, yes, you could go read the kernel namelist, search the proc table,
read and modify the u area of the parent.  Of course, it is unportable,
requires root privilege, subject to races with the kernel, and is
generally bletcherous.  So, that's not really an option -- is it?

-- 
Michael Stefanik, MGI Inc, Los Angeles | Opinions stated are never realistic
Title of the week: Systems Engineer    | UUCP: ...!uunet!bria!mike
-------------------------------------------------------------------------------
If MS-DOS didn't exist, who would UNIX programmers have to make fun of?

pfinkel@cbnewsb.cb.att.com (paul.d.finkel) (05/14/91)

In article <1991May2.194624.21819@kong.gsfc.nasa.gov> njacobs@kong.gsfc.nasa.gov (Nick Jacobs - EOS) writes:
>How do you change the current working directory in a program, so
>that when the program is invoked from a shell, the cwd stays
>changed after the program exits?
>You can't do it with chdir(2) of course, because that only affects
>the current process.
>
>Nick

 You could run it from a shell function:

	$pwd    
	/usr/bin
	$go_go()
	{
	cd /etc
	your_command
	pwd
	}
	$go_go
	/etc
	$pwd
	/etc