Results 1 to 9 of 9
I am trying to implement CD command using C language.but the directory is not changing....
This is the code that I have written..... Plz help
#include <unistd.h>
#include<stdio.h>
int main(int ...
- 05-02-2009 #1Just Joined!
- Join Date
- May 2009
- Posts
- 3
Help req. to implement CD command in C
I am trying to implement CD command using C language.but the directory is not changing....
This is the code that I have written..... Plz help
#include <unistd.h>
#include<stdio.h>
int main(int argc, char** argv)
{
if (argc == 1) // no args
{
const char* home = getenv("HOME");
chdir(home ? home : "."); // in case HOME is not defined
}
else // arg given (could it be a path?)
{
}
char *directory = "/tr02/mar09ft1/t401579/nik";
int ret;
ret = chdir (directory);
if (ret ==0) // to check that chdir() is working or not//
printf("good");
else
printf("bad");
}
- 05-02-2009 #2
This chdir command only affects the application started by the shell, i.e. your program, which becomes a separate process. It has no effect on the calling application, the shell.
The shell has the 'cd' command built-in. It is no separate program but more like a control command.
Btw. you seem to have put the curly braces under "else" incorrectly.Debian GNU/Linux -- You know you want it.
- 05-02-2009 #3Just Joined!
- Join Date
- May 2009
- Posts
- 3
Thanks for the reply
I got what u exactly want to say....
n the else n brackets mistake i have already corrected....
But plz suggest some other alternative to implement CD command .....
- 05-03-2009 #4Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Precisely! The current directory is totally context dependent. For example, if you start a shell within a shell, cd to another directory, then exit that sub-shell, you will find yourself back in the directory you were in before. This is exactly analogous to executing a change directory function in your C program. When it exits, your shell doesn't know that you changed directory at all. So, your exercise is useful if you are writing a shell program, or within the program scope you want to do things in the now-current directory, but it is useless for the calling shell itself.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 05-03-2009 #5Just Joined!
- Join Date
- May 2009
- Posts
- 3
reply
I totally agree with you guyz.... but it is a part of my project....
n anyhow I have to implement it....
so give me any suggestions... I want thee job done....anyhow.....
Plz suggest
- 05-03-2009 #6Debian GNU/Linux -- You know you want it.
- 05-03-2009 #7Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 05-04-2009 #8Debian GNU/Linux -- You know you want it.
- 05-04-2009 #9Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote
