S
Siemel Naran
Hi. I'm writing a command shell that reads commands from standard input.
At this point I have the command in a std::string. Now I want to execute
this command in the shell. From the Borland newsgroups I learned that there
is a function in stdlib.h called system.
int system(const char *command);
First question, is the system command ANSI compliant. Because I include
<cstdlib> and write std::system(command.c_str()); it looks like an ANSI C++
function. But the documentation says it's in POSIX and Win32 only, not in
ANSI C and ANSI C++.
Second, and more important, the system command does not appear to respond to
the cd command. The call std::system("cd ..") runs without error, but does
not change the working directory, as can be evidenced by calling getcwd of
dir.h before and after the call to system. The reason is that the system
command runs within a new child process, and within that the process that
directory does probably change. After all, if one runs system("exit"), one
wants the child process to exit, not the parent process.
So how can I get the system command to change the directory of the parent
process? Do I have to parse the command, and look for "cd xyz" and change
the directory to "xyz" using chdir of dir.h? Or is there some way to create
a system object as a local variable, so that it can know it's state (ie.
it's directory, environment variables, etc)? If this is the wrong
newsgroup, can someone please let me know the right newsgroup?
Thanks.
At this point I have the command in a std::string. Now I want to execute
this command in the shell. From the Borland newsgroups I learned that there
is a function in stdlib.h called system.
int system(const char *command);
First question, is the system command ANSI compliant. Because I include
<cstdlib> and write std::system(command.c_str()); it looks like an ANSI C++
function. But the documentation says it's in POSIX and Win32 only, not in
ANSI C and ANSI C++.
Second, and more important, the system command does not appear to respond to
the cd command. The call std::system("cd ..") runs without error, but does
not change the working directory, as can be evidenced by calling getcwd of
dir.h before and after the call to system. The reason is that the system
command runs within a new child process, and within that the process that
directory does probably change. After all, if one runs system("exit"), one
wants the child process to exit, not the parent process.
So how can I get the system command to change the directory of the parent
process? Do I have to parse the command, and look for "cd xyz" and change
the directory to "xyz" using chdir of dir.h? Or is there some way to create
a system object as a local variable, so that it can know it's state (ie.
it's directory, environment variables, etc)? If this is the wrong
newsgroup, can someone please let me know the right newsgroup?
Thanks.