[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

chdir not working?

Posted on 2006-04-06
20
Medium Priority
?
1,519 Views
Last Modified: 2012-08-13
I have the following code:

cout << cmd;
int i;
i = chdir(&cmd[0]);
printf("%d", i);

now the cout line produce:
/home/sakuya/Documents/University/159.101/work/a6/

but the directory isnt changed in the console window... Am I doing something wrong?

thanks in advanced
0
Comment
Question by:sakuya_su
  • 8
  • 6
  • 3
  • +3
20 Comments
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16390003
post your complete code
0
 
LVL 10

Author Comment

by:sakuya_su
ID: 16390055
cmd = "/home/sakuya/Documents/University/";
            cmd += "159.101/work/";
            cmd += argv[2];
            cmd += "/";


And

if ( strcmp(argv[3], "in") == 0 )
      {
            //CD in to the directory only
            //printf("%s", run);
            cout << "-----------change Dir-------------" << endl;
            //run = &cmd[0];
            //printf("%s", run);
            int i;
            i = chdir(&cmd[0]);
            printf("%d", i);
      }
0
 
LVL 12

Assisted Solution

by:rajeev_devin
rajeev_devin earned 150 total points
ID: 16390059
>> but the directory isnt changed in the console window
chdir changed the current directory of the process from where it is called.
Remember that shell is a different process. And you program is a different process.

Try this:
if(chdir("/home")) // It is actually changing the directory
      printf("Unable to locate the directory");
else
      system( "ls"); // This can be varified by listing the content of that directory
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Author Comment

by:sakuya_su
ID: 16390078
ah i see whats happening now,

is there a way to change the current directory of the console window?
0
 

Expert Comment

by:M_Kuriachen
ID: 16390999
"
ah i see whats happening now,

is there a way to change the current directory of the console window?
"

SetCurrentDirectory() ;
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16391928
>> SetCurrentDirectory() ;
First, of all SetCurrentDirectory() is a windows API. So, it cannot be used in Linux.
Second, It changes the current directory of the process from where it is called. It won't change the current directory of console.
0
 
LVL 2

Expert Comment

by:Sammo
ID: 16393273
system("cd [PATH]");

Should do it?
0
 
LVL 10

Author Comment

by:sakuya_su
ID: 16395875
no system("cd PATH"); does not work

to  M_, i need to the code to be able to work under Linux.

rajeev_devin, do you have any idea how to do this?

Thanks for all your help
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 450 total points
ID: 16396481
Everything I've been able to find on this issue indicates that there is no way to change the directory for the parent process (CMD.EXE or whatever Unix Shell you are using -- that's displaying the prompt in the console window).

You might need to resort to trickery... If you program is started by a batch file, then the program could write a CHDIR command to the end of the batch.  Then when it exits, the shell looks to see the next command in the batch, and executes it to change its CWD.

Or the initial batch file's final command could be to execute a batch with a specific name -- and your program rewrites that batch file during its execution.

-- Dan
0
 
LVL 10

Author Comment

by:sakuya_su
ID: 16396523
heh yeah, i think i'll just create a batch with a CD command in it
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16397669
>> heh yeah, i think i'll just create a batch with a CD command in it
That won't do either.
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16397673
By the way in which platform you are working ?
0
 
LVL 10

Author Comment

by:sakuya_su
ID: 16398110
Linux
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16399486
In linux can you do something like "DLL Hooking in Windows" ?
0
 
LVL 11

Assisted Solution

by:dbkruger
dbkruger earned 150 total points
ID: 16399563
The kernel call is chdir in Unix, but of course changing the child's current directory is not what you want.

The cd command in a shell doesn't launch a subprocess, it is an in-shell command that modifies the current process.
If you wrote a shell script containing:

cd foo

and ran it with:

sh myscript.sh

it STILL wouldn't work, because execution launches a subprocess, where you change the current directory, which then goes away, leaving the console unchanged.

The way to execute a shell in-process is (in bash)
. myscript.sh

The dot says "execute in this process" and the current directory is changed.
You could use exec to execute a program in process, but when the program is over, so would be the process, which is probably not what you want.

0
 
LVL 10

Author Comment

by:sakuya_su
ID: 16399666
i'll try the . *.sh tomorrow, thanks,

to rajeev, no, there are no dll hooks in linux
0
 
LVL 49

Assisted Solution

by:DanRollins
DanRollins earned 450 total points
ID: 16402588
It WILL work if you follow my suggestion:

Don't run your program directly from the command line.  Instead run a two-line batch file.  That batch file starts your program, waits until it is done, and then executes a second batch file (let's call it, "DoChdir.bat") .

In the program, don't run a batch file and don't do any CD comments.  Just have your program create (overwrite) the one-line file named "DoChdir.bat".  Have your program write a CHDIR command to the file.

When your program exits, the original batch file will continue running.  Its final command will be to execute "DoChdir.bat"  and the single command in "DoChdir.bat" will be to CD to the directory specified when your program wrote the "DoChdir.bat" file.

=-=-=-=-=-=-=-=
I have been assuming that your program needa to do multiple things, and needs to change the CWD for the command processsor as the final step.  HOWEVER, if *all* that your program is intended to do is to set the CWD to a particular value for the shell process, then there are other, easier, ways to do that.  Just ask.
0
 
LVL 10

Author Comment

by:sakuya_su
ID: 16404959
which other methods are u refering to?
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 16405127
If all you want to do in your program is change the CWD to something specific, then there is no reason to write a C++ program to do it.  Just use a batch file (or, in Unix, a shell script).

     REM QCD.BAT (Quick Change Dir)
     REM Batch file to do everything without writing a C++ program
     REM Syntax:   QCD string
     REM Example: QCD a6
     CD /home/sakuya/Documents/University/159.101/work/%1

The above lines will save you lots of typing.  The %1 in the CD command is replaced by whatever you type at the end of the QCD command line.  You can remove the REM lines if you want.
0
 
LVL 10

Author Comment

by:sakuya_su
ID: 16474214
well it seems no one has produced a very useable suggestion, but the comments here got me thinking and I have then coded my program arround this area.

so I'll split point to all who made a contribution.

thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question