if (execlp("cd","cd","..", NULL)==-1)
perror("Error: ");
execlp and cd and
I'm trying to execute cd within my program as so:
But I'm getting the error: ": No such file or directory"
What's wrong there?
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.
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:
Putty said that I'm in the follosing direcory: ~/proiecte and after ./test.o this directory wasn't changed to ~/
Why is that?
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement