Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > programmatically change directory in UNIX

Reply
Thread Tools

programmatically change directory in UNIX

 
 
Varun Yagain
Guest
Posts: n/a
 
      07-24-2003
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.
 
Reply With Quote
 
 
 
 
Tobias Oed
Guest
Posts: n/a
 
      07-24-2003
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-newsgrou...rogrammer.html
clc http://www.eskimo.com/~scs/C-faq/top.html
fclc (french): http://www.isty-info.uvsq.fr/~rumeau/fclc/
 
Reply With Quote
 
 
 
 
Lew Pitcher
Guest
Posts: n/a
 
      07-24-2003
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')
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      07-25-2003
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:
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
System.IO.Directory.GetDirectories() and System.IO.Directory.GetFiles() are not returning the specified directory Nathan Sokalski ASP .Net 2 09-06-2007 03:58 PM
A Paradise DNS address change? What change? There was no change. Tony Neville NZ Computing 7 09-22-2006 01:02 PM
compile C programs with UNIX system calls (= Unix Programs??) jrefactors@hotmail.com C Programming 18 01-10-2005 03:35 AM
compile C programs with UNIX system calls (= Unix Programs??) jrefactors@hotmail.com C++ 12 01-10-2005 03:35 AM
my own perl "dos->unix"/"unix->dos" Robert Wallace Perl Misc 7 01-21-2004 11:28 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57