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? |
Hi
I'ts C? I think what you're looking for is the "chdir" function. |
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. |
Quote:
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 |
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 09:33 PM. |