home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / unix / question / 15062 < prev    next >
Encoding:
Text File  |  1992-12-30  |  2.0 KB  |  56 lines

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!spool.mu.edu!yale.edu!ira.uka.de!smurf.sub.org!easix!bc3!ktf
  3. From: ktf@bc3.GUN.de (Klaus ter Fehn)
  4. Subject: Re: how to change shell's PWD(directory) from a C program?
  5. Organization: private
  6. Date: Wed, 30 Dec 1992 06:02:31 GMT
  7. Message-ID: <C02648.Jq@bc3.GUN.de>
  8. References: <1hqe2cINN6ot@usenet.INS.CWRU.Edu>
  9. Lines: 45
  10.  
  11. In article <1hqe2cINN6ot@usenet.INS.CWRU.Edu> cl820@cleveland.Freenet.Edu (Wei Jin Mai) writes:
  12.  
  13.  
  14. >    Here is what I want to do: UPon the termination of my program, I 
  15. >want to change to another directory . Normally, a program exit to the
  16. >directory that invoke it. 
  17.  
  18. Because of the current newsgroup (comp.unix.questions) I assume, that
  19. you're programming under the UNIX-environment. Note, that the PWD (Path
  20. of Working Directory) is an environment-held information of a process.
  21. Each process gets a copy of the environment of it's parent-process at
  22. startup-time. So if you call the cd() funktion from within a C-Program,
  23. only the PWD of the C-program-process is changed. When you exit this
  24. program, you are returning to the parent-process (most cases: the
  25. shell). It's PWD information was not changed, because you only changed
  26. the child's copy.
  27.  
  28. There is a workaround with the alias/functions in some shells. Let's assume
  29. a very simple sample: The C-Program writes it's PWD into a file $HOME/.pwd
  30. let's name the C-Program myprogram.
  31.  
  32. Bourne-Shell: Define a Function 'mycall' (maybe in your .profile) 
  33.           which looks like this:
  34.  
  35. mycall() {
  36.     myprogram
  37.     cd `cat $HOME/.pwd`
  38. }
  39.  
  40. Korn-Shell: Define an alias to call your program:
  41.  
  42. alias mycall='myprogram; cd `cat ~/.pwd`'
  43.  
  44. In both cases you got to invoke your program with the command 'mycall'.
  45. I'm not familiar with the C-Shell, but I know there is a way to do so,
  46. too.
  47.  
  48. Note, that THIS is just one way to do what you need. There are some other,
  49. more efficient ways, but that depends on your program and the Shell you use.
  50.  
  51. -- 
  52. Klaus ter Fehn        <ktf@bc3.GUN.de>
  53. Neanderstr. 4        {mcshh,smurf,unido}!easix!bc3!ktf
  54. 4000 Duesseldorf 1
  55. FRG / Germany        Tel.: +49-211-676331
  56.