Find the answer to your Linux question:
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 ...
  1. #1
    Just 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");
    }

  2. #2
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    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.

  3. #3
    Just 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 .....

  4. #4
    Linux Guru Rubberman's Avatar
    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
    Quote Originally Posted by GNU-Fan View Post
    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.
    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!

  5. #5
    Just 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

  6. #6
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    Quote Originally Posted by nikhil_jassi View Post
    I totally agree with you guyz.... but it is a part of my project....
    n anyhow I have to implement it....
    What exactly do you want to achieve?
    Debian GNU/Linux -- You know you want it.

  7. #7
    Linux Guru Rubberman's Avatar
    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
    Quote Originally Posted by GNU-Fan View Post
    What exactly do you want to achieve?
    Quote Originally Posted by nikhil_jassi View Post
    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
    Well, if you want the new directory visible outside of the C program you implemented it in, there is actually no way to do so, as far as I am aware.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  8. #8
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    Quote Originally Posted by Rubberman View Post
    Well, if you want the new directory visible outside of the C program you implemented it in, there is actually no way to do so, as far as I am aware.
    The shell interpreters could be modified so that they become aware of this.
    But the question whether this would be feasible remains as long as the actual goal of nikhil_jassi is unclear.
    Debian GNU/Linux -- You know you want it.

  9. #9
    Linux Guru Rubberman's Avatar
    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
    Quote Originally Posted by GNU-Fan View Post
    The shell interpreters could be modified so that they become aware of this.
    But the question whether this would be feasible remains as long as the actual goal of nikhil_jassi is unclear.
    Exactly. Since the system C libraries have a chdir() function to do what he says he wants for use within a particular program, I can only assume that this is some sort of class exercise...
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...