home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.questions
- Path: sparky!uunet!spool.mu.edu!yale.edu!ira.uka.de!smurf.sub.org!easix!bc3!ktf
- From: ktf@bc3.GUN.de (Klaus ter Fehn)
- Subject: Re: how to change shell's PWD(directory) from a C program?
- Organization: private
- Date: Wed, 30 Dec 1992 06:02:31 GMT
- Message-ID: <C02648.Jq@bc3.GUN.de>
- References: <1hqe2cINN6ot@usenet.INS.CWRU.Edu>
- Lines: 45
-
- In article <1hqe2cINN6ot@usenet.INS.CWRU.Edu> cl820@cleveland.Freenet.Edu (Wei Jin Mai) writes:
-
-
- > Here is what I want to do: UPon the termination of my program, I
- >want to change to another directory . Normally, a program exit to the
- >directory that invoke it.
-
- Because of the current newsgroup (comp.unix.questions) I assume, that
- you're programming under the UNIX-environment. Note, that the PWD (Path
- of Working Directory) is an environment-held information of a process.
- Each process gets a copy of the environment of it's parent-process at
- startup-time. So if you call the cd() funktion from within a C-Program,
- only the PWD of the C-program-process is changed. When you exit this
- program, you are returning to the parent-process (most cases: the
- shell). It's PWD information was not changed, because you only changed
- the child's copy.
-
- There is a workaround with the alias/functions in some shells. Let's assume
- a very simple sample: The C-Program writes it's PWD into a file $HOME/.pwd
- let's name the C-Program myprogram.
-
- Bourne-Shell: Define a Function 'mycall' (maybe in your .profile)
- which looks like this:
-
- mycall() {
- myprogram
- cd `cat $HOME/.pwd`
- }
-
- Korn-Shell: Define an alias to call your program:
-
- alias mycall='myprogram; cd `cat ~/.pwd`'
-
- In both cases you got to invoke your program with the command 'mycall'.
- I'm not familiar with the C-Shell, but I know there is a way to do so,
- too.
-
- Note, that THIS is just one way to do what you need. There are some other,
- more efficient ways, but that depends on your program and the Shell you use.
-
- --
- Klaus ter Fehn <ktf@bc3.GUN.de>
- Neanderstr. 4 {mcshh,smurf,unido}!easix!bc3!ktf
- 4000 Duesseldorf 1
- FRG / Germany Tel.: +49-211-676331
-