programmatically change directory in UNIX

Discussion in 'C Programming' started by Varun Yagain, Jul 24, 2003.

  1. Varun Yagain

    Varun Yagain Guest

    hi,
    please do let me know how i can change the directory through my
    program and retain it too after i exit; if there's a way to do it.
    regards,
    varun yagain.
    Varun Yagain, Jul 24, 2003
    #1
    1. Advertising

  2. Varun Yagain

    Tobias Oed Guest

    Varun Yagain wrote:

    > hi,
    > please do let me know how i can change the directory through my
    > program and retain it too after i exit; if there's a way to do it.
    > regards,
    > varun yagain.


    This question is not apropriate for clc. comp.unix.programmer is a better
    place.

    <OT>
    You can't. That's why cd has to be shell builtin and not an external
    command.
    <\OT>

    Tobias

    --
    unix http://www.faqs.org/faqs/by-newsgroup/comp/comp.unix.programmer.html
    clc http://www.eskimo.com/~scs/C-faq/top.html
    fclc (french): http://www.isty-info.uvsq.fr/~rumeau/fclc/
    Tobias Oed, Jul 24, 2003
    #2
    1. Advertising

  3. Varun Yagain

    Lew Pitcher Guest

    On 24 Jul 2003 12:37:00 -0700, (Varun Yagain) wrote:

    >hi,
    >please do let me know how i can change the directory through my
    >program and retain it too after i exit; if there's a way to do it.
    >regards,


    First off, this is off-topic for C.L.C

    2nd, look at the chdir(2) function for the Unix way to "change directories"

    3rd. You aren't going to be able to retain the new directory after you exit.
    That's not how Unix works.


    --
    Lew Pitcher
    IT Consultant, Enterprise Technology Solutions
    Toronto Dominion Bank Financial Group

    (Opinions expressed are my own, not my employers')
    Lew Pitcher, Jul 24, 2003
    #3
  4. Varun Yagain

    Dan Pop Guest

    In <> (Varun Yagain) writes:

    >please do let me know how i can change the directory through my
    >program and retain it too after i exit; if there's a way to do it.


    The current working directory is a property of the program. It is
    not passed back to the "parent" process, so this piece of information
    is normally lost when the program terminates.

    It is always possible to do something about it, just don't expect it
    to happen by magic:

    fangorn:~/tmp 1921> pwd
    /afs/ifh.de/user/d/danpop/tmp
    fangorn:~/tmp 1922> cat test.c
    #include <stdio.h>
    #include <unistd.h>

    char buff[FILENAME_MAX];

    int main()
    {
    chdir("/usr/bin");
    if (getcwd(buff, sizeof buff) != NULL) puts(buff);
    else puts("unknown");
    return 0;
    }
    fangorn:~/tmp 1923> gcc test.c
    fangorn:~/tmp 1924> cd `./a.out`
    fangorn:/usr/bin 1925> pwd
    /usr/bin

    Of course, this approach works only as long as you don't use stdout for
    any other purposes...

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Jul 25, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Danny
    Replies:
    1
    Views:
    436
    Natty Gur
    Nov 3, 2003
  2. Matt
    Replies:
    1
    Views:
    757
    Sherif ElMetainy
    Nov 10, 2003
  3. Replies:
    12
    Views:
    1,614
    Dave Thompson
    Jan 10, 2005
  4. Replies:
    18
    Views:
    607
    Dave Thompson
    Jan 10, 2005
  5. Robert Wallace

    my own perl "dos->unix"/"unix->dos"

    Robert Wallace, Jan 21, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    266
    Michele Dondi
    Jan 22, 2004
Loading...

Share This Page