LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to use "cd" command within a exec call? (https://www.linuxquestions.org/questions/programming-9/how-to-use-cd-command-within-a-exec-call-545765/)

haydari 04-13-2007 09:57 AM

how to use "cd" command within a exec call?
 
I need to be able to change directories in my shell terminal program.

But since the "cd" is not a program itself, I can't call it with execve.

I also tried execl and execlp functions but it didn't work. Do you have any idea about how can I accomplish this?

Guttorm 04-13-2007 10:13 AM

Hi

I'ts C? I think what you're looking for is the "chdir" function.

theNbomr 04-13-2007 11:16 AM

To explain the behavior that the original poster is seeing, and why his approach will never work, the exec() call spawns a new process, and any actions performed within the context of that process are completely independent of the parent process. If it were any other way, then all processes would have the same working directory, as all processes are descendants of the original init process. The same effect applies to modification of environment variables, which is a similar thing that people trip over. bash uses the source command to work around this.
Hope this clears things up.

--- rod.

kshkid 04-13-2007 01:30 PM

Quote:

the exec() call spawns a new process
No, not really

if exec is able to spawn a new process then why we would be in a need to have fork system call


fork system call would create a new process while
exec system call would overlay the process image of a process with the code and data of a new process copied

theNbomr 04-13-2007 04:34 PM

Sorry, you're right. However, since exec() never returns, the original process becomes effectively terminated. I was thinking of the system() function, which does a fork internally.
On re-reading the original post, I answered a question not even asked. My double-oops. The first reply answered the whole question. mea culpa.
--- rod.


All times are GMT -5. The time now is 07:07 PM.