🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

execlp and cd and

Started by
5 comments, last by Deliverance 16 years, 7 months ago
I'm trying to execute cd within my program as so:

if (execlp("cd","cd","..", NULL)==-1)
    perror("Error: ");

But I'm getting the error: ": No such file or directory" What's wrong there?
Advertisement
I don't think that will work for you. I think 'cd' is a special bash command, not an executable program. Consider that even if 'cd' was such a program, execlp replaces your process with another, so your program is either over (if you didn't fork()) or remains unchanged (if you did fork(), as parents and children are independant).

Try using chdir(), it appears to be more what you are looking for.
Okay I used chdir but it didn't work as i expected. I'm trying to implement a protocol that would let me use cd and ls on a specified computer in the network.
What i did was I connected with putty to the master computer at my faculty and i made a little program called test.o that does this:
chdir("..");

Putty said that I'm in the follosing direcory: ~/proiecte and after ./test.o this directory wasn't changed to ~/
Why is that?
Quote: Original post by Deliverance
Okay I used chdir but it didn't work as i expected. I'm trying to implement a protocol that would let me use cd and ls on a specified computer in the network.
What i did was I connected with putty to the master computer at my faculty and i made a little program called test.o that does this:
*** Source Snippet Removed ***
Putty said that I'm in the follosing direcory: ~/proiecte and after ./test.o this directory wasn't changed to ~/
Why is that?


I think I see. The problem is that you cannot change (to my knowledge) the current working directory of a parent process. Remember, chdir only changes the current processes working directory. If your program then terminates, you are returned to bash which retains its older working directory.

I'm confused though... surely you are already using ssh to contact the remote computer? Why do you need another program, when you can just type 'cd'?

I think I would need a higher level description of what you are trying to do. It appears you are trying to duplicate some of the functionality of ssh and bash.
I have an assignment for school to implement the ls and cd command in the network. :D
Quote: Original post by Deliverance
I have an assignment for school to implement the ls and cd command in the network. :D


Well, the only thing I can say is that you need to either use a persistent process to keep its current working directory, or you can keep the working directory in a file, reading it when the program is started and writing it when it changes.
Okay, I'll do that.

This topic is closed to new replies.

Advertisement