• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1532
  • Last Modified:

chdir not working?

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
sakuya_su
Asked:
sakuya_su
  • 8
  • 6
  • 3
  • +3
4 Solutions
 
rajeev_devinCommented:
post your complete code
0
 
sakuya_suAuthor Commented:
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
 
rajeev_devinCommented:
>> 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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
sakuya_suAuthor Commented:
ah i see whats happening now,

is there a way to change the current directory of the console window?
0
 
M_KuriachenCommented:
"
ah i see whats happening now,

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

SetCurrentDirectory() ;
0
 
rajeev_devinCommented:
>> 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
 
SammoCommented:
system("cd [PATH]");

Should do it?
0
 
sakuya_suAuthor Commented:
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
 
DanRollinsCommented:
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
 
sakuya_suAuthor Commented:
heh yeah, i think i'll just create a batch with a CD command in it
0
 
rajeev_devinCommented:
>> heh yeah, i think i'll just create a batch with a CD command in it
That won't do either.
0
 
rajeev_devinCommented:
By the way in which platform you are working ?
0
 
sakuya_suAuthor Commented:
Linux
0
 
rajeev_devinCommented:
In linux can you do something like "DLL Hooking in Windows" ?
0
 
dbkrugerCommented:
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
 
sakuya_suAuthor Commented:
i'll try the . *.sh tomorrow, thanks,

to rajeev, no, there are no dll hooks in linux
0
 
DanRollinsCommented:
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
 
sakuya_suAuthor Commented:
which other methods are u refering to?
0
 
DanRollinsCommented:
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
 
sakuya_suAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 8
  • 6
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now