Changing directory from within a C program.

Changing directory from within a C program.

Post by Elio Martin » Thu, 08 Sep 1994 06:12:12



Hey people.  I'm posting this for a friend.  Read and divulge.  Mail me
answers up till Thursday September 8 and I'l get them to him.  Otherwise,
post the answer and it'll get to him somehow.
-------------------------------------------------------------------------

I have what should prove to be an interesting problem. If anyone reading this
can suggest a solution, it would be greatly appreciated. Basically, I'm
looking for a way to change directories from within a C program such that
when the program terminates, the directory changed to becomes current.

That is, if the current directory is

"/usr/bin"

after the program is run, the
new directory will be

"/etc"

Assume that the directory to be changed to is
hard coded. I've asked several people how this could be done,
(after many many failed attempts at hacking) but no one knows how it can be
done. I've tried the following methods without success.

system("cd /etc");
system("pushd /etc");
chdir("/etc");
setenv("PWD", "/etc", TRUE);
putenv("PWD=/etc");

Each of these methods succeeds in changing the default directory to "/etc"
(I have proved that to myself, by printing the current directory after
modifying it) during the program's execution, but when control returns to
shell (bash), the default directory before the "change directory" program
was run is restored.

Once again, any suggestions as to what else I could try would be greatly
appreciated.

Luis Correia.

------------------------------------------------------------------------

There ya go.  Hope y'all can help.  POst the answers so that everyone
benefits, ok?  Ciao.

--
-Pav.  (a.k.a Elio Martinho)

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
* PPPPPP     A    V       V * 'Never leave for tomorrow what you can do      *
* P     P   A A    V     V  * today, my friend.  DO IT!  And if you like it, *
* PPPPPP   A   A    V   V   * you can do it again tomorrow!'                 *
* P       AAAAAAA    V V    * -From the B.C.I Student Council Bulletin Board *
* P      A       A    V     * (Pav A.K.A Elio Martinho)                      *
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

 
 
 

Changing directory from within a C program.

Post by Andrew R. Tef » Thu, 08 Sep 1994 21:15:59



Quote:>I have what should prove to be an interesting problem. If anyone reading this
>can suggest a solution, it would be greatly appreciated. Basically, I'm
>looking for a way to change directories from within a C program such that
>when the program terminates, the directory changed to becomes current.

I'm glad you asked. I was interested in doing this before I got into Linux
(I'll tell why in a minute).

The answer is: you can't.

Quote:>Each of these methods succeeds in changing the default directory to "/etc"
>(I have proved that to myself, by printing the current directory after
>modifying it) during the program's execution, but when control returns to
>shell (bash), the default directory before the "change directory" program
>was run is restored.

Correct. A process can't change its parent's working directory. The
parent is in this case the shell.

Now it *would* be possible to hack bash or tcsh to provide a method
for a process to tell it to change directories. A bit complicated for me...
and while it might be a fabulous feature, it would open the door to
some real and some minor problems (if only the confusion factor).

Quote:>Once again, any suggestions as to what else I could try would be greatly
>appreciated.

If the only purpose of your program is to change the directory (mine was),
define an alias which runs it, grabs the output, and does the cd. Mine (csh) is:

set lastpwd=`pwd`; set cdtmp=`mycd !*`; echo $cdtmp[2-]; ""cd $cdtmp[1];pwd

perhaps a bit complicated but it does a number of things. $lastpwd is
used in another alias that lets me pop between the last two directories
visited. 'mycd' will go along the path of the given directory as long
as it is valid. For example, if you say "mycd /home/users/joe/test" and
there is no "joe" in /home/users, mycd will output the valid part of the
directory plus an error message, which is printed (yes, it was a design
decision not to make the error go to stderr, which makes the alias more
complicated than otherwise). the ""cd executes the unaliased cd command
to cd to the valid part of the path. This alias saves me much time as I
often mistype directory names! If something happens to 'mycd' I can
still unalias cd or use ""cd temporarily to avoid the alias.

Well, mycd doesn't work quite the same under Linux as it does on the
sun, and I modified it just a bit to work on Linux but now it always
drops the last component of any relative pathnames you give it, but
it works great on my sun :-)

---



 
 
 

Changing directory from within a C program.

Post by Grant Edwar » Sat, 10 Sep 1994 00:47:37


: I'm looking for a way to change directories from within a C program
: such that when the program terminates, the directory changed to
: becomes current.

The short answer: it can't be done.

You can only change the current directory for the current process, not
for the parent process.

: I've tried the following methods without success.

: system("cd /etc");
: system("pushd /etc");
: chdir("/etc");
: setenv("PWD", "/etc", TRUE);
: putenv("PWD=/etc");

: Each of these methods succeeds in changing the default directory to "/etc"
: (I have proved that to myself, by printing the current directory after
: modifying it) during the program's execution, but when control returns to
: shell (bash), the default directory before the "change directory" program
: was run is restored.

The current directory of the process in which bash is running was
never changed (and, hence, wasn't really "restored" either).

In fact, the current directory of the process running the "C" program
was never changed by the system() library routine.  The system()
routine forks a child process that runs the command you've given it,
and the current directory of that child process is changed.

The chdir() system call does change the current directory of the calling
process, but not of the parent process (which in your case is the process
in which bash is running).

The only thing that I can think of that would come close to what you
want is to "exec" the C program that does the chdir(), and then have
that C program "exec" another shell.  You _might_ lose some of the
context in which the original shell was running.  The environment
should be preserved, but some internal shell state (options, aliases,
whatever) might be lost -- it depends on the shell.

--
Grant Edwards                                 |Yow!  Somewhere in Tenafly,
Rosemount Inc.                                |New Jersey, a chiropractor is
                                              |viewing ``Leave it to

 
 
 

1. How to change directories from within a c-program???

Hello,

Does anyone outthere know what the proper way
is to change a directory from a c-program

When I use the system() command the
directory is only changed during the
lifespan of the running program.

I want the directory to still be changed
after the execution of the program.

Is there any way/command to do this?

Thanx in advance,
Roland

2. Missing libbzip2.so.0...

3. Changing directories within a bash shell script

4. Problems with instaling packgages' 3.1FreeBSD

5. How to change the directory from within a sh proc?

6. Dedicated mail server

7. Physically changing directories from within a (ksh) script.

8. What is a SYN attack?

9. Changing Directories within a shell

10. determining current directory within a program

11. getting changing ethernet address within a program

12. changing env variables from within C program

13. 2nd Try: How to change dir from within a c program ???