system("")

Started by
13 comments, last by sevak 19 years, 11 months ago
can someone explain what the system("") function is for in C++ because I use system("PAUSE") to stop programs from just closing when you open them but today I saw a program with system("Hello"). I read a little about it in thinking in C++ but I still don''t get it. Please explain what this function is used for.

www.computertutorials.org
computertutorials.org-all your computer needs...
Advertisement
system spawns an executable with the specified name, relying on the user path to resolve exactly which binary is invoked. system("pause") calls the pause executable - but it doesn't exist on Unix systems! Furthermore, if a malicious individual is able to place a binary named pause anywhere closer on the path, such as in the same directory as the spawning binary, that unknown binary will be executed instead, enabling the intruder perform any action desired.

Now that you know what it does, you should be able to figure out that it's bad practice. Most people use it because the Dev-C++ environment doesn't delay closing console applications; either execute the binary manually (ugly), add any blocking operation to the end of your application (bad), or switch IDEs (good).

Seriously.

[Edit: Typo.]

[edited by - Oluseyi on May 20, 2004 1:12:10 AM]
quote:Original post by Oluseyi
system spawns an executable with the specified name

That''s not accurate. system() runs a shell command, like you''d type in the Unix terminal or DOS console. So I believe you can run system("wc | ls") in Unix, for example, or system("cd\games") in DOS.

Unix does not have any commands built into the shell, so technically every system() call would end up running a program... but it''s not like fork/exec.

~CGameProgrammer( );

Screenshots of your games or desktop captures -- Upload up to four 1600x1200 screenshots of your projects, registration optional. View all existing ones in the archives..
~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
Just a note that you can''t use system("cd \\games"); to change directory of your process; the system runs the shell as a sub-process which has its own working directory, and will not affect yours.

Note that if you''d even wanted to do that (which wouldn''t work), you would have to escape the backslash as above.

Slarty

quote:Original post by CGameProgrammer
Unix does not have any commands built into the shell, so technically every system() call would end up running a program... but it''s not like fork/exec.

Uhm, hm... ever looked at man sh? There''s a whole section dedicated to "Builtin commands". alias, for example, is built into the shell.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Whatever OS you''re on, system() spawns a shell and runs the command on there. How the shell implements this is up to it.

On Windows the shell is cmd.exe and has a lot of the old DOS commands implemented in it, like copy, rename, delete etc. But not every command exists in this form, so it has to spawn a subprocess for it.

So in fact, your program runs the shell, which then MAY run another program in turn, if the command is not a built-in.

Mark
For Windows at least, system("something") does exactly what would happen if you opened an MS-DOS prompt in the same directory as where your .exe is and typed something there. Try openning a DOS prompt and typing pause for example, see what happens.

For Unix, you can start a shell process and use the -c option to specify a command that includes wildcards, piping, etc. For example, system("sh -c \"cat *.cpp | grep // > comments.txt\""); would take all lines that start with // from all your C++ files and put them in comments.txt. (Amazingly useful, of course..)

[edited by - Matei on May 20, 2004 8:20:15 AM]

[edited by - Matei on May 20, 2004 8:21:07 AM]

[edited by - Matei on May 20, 2004 8:23:37 AM]
quote:Now that you know what it does, you should be able to figure out that it''s bad practice. Most people use it because the Dev-C++ environment doesn''t delay closing console applications; either execute the binary manually (ugly), add any blocking operation to the end of your application (bad), or switch IDEs (good).


Console apps automatically close on exit under all WinNT based systems. Dev does it, VC++ does it, running it manually does it. (Under Windows anyway)

Just out of intrest, what would you recommend as an alternative IDE? (Thats not a sarky comment, I am actually interested now )
quote:Original post by Oluseyi
either execute the binary manually (ugly), add any blocking operation to the end of your application (bad), or switch IDEs (good).


You forgot "make a batch file/shell script" (better).

e.g. whatever.batmyprogram.exepause 


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quote:Original post by zoggo
Console apps automatically close on exit under all WinNT based systems. Dev does it, VC++ does it, running it manually does it. (Under Windows anyway)
You missed the point; perhaps I was too subtle. VC++ terminates the app, but keeps the console window open until you press a key. Running it manually, you''re already at the CLI, so the window isn''t closed until you exit (I wasn''t suggesting using Start -> Run...).

quote:Just out of intrest, what would you recommend as an alternative IDE? (Thats not a sarky comment, I am actually interested now )
If Dev-C++ hasn''t seen it fit to address this obviously significant concern, given the number of questions about it, in five versions, maybe it''s time to move on to something else. MinGW Developer Studio handles it as gracefully as MSVC; an IDE that isn''t simultaneously making development easier and promoting good practice is a bad IDE.

Besides, I just don''t like Dev-C++. It feels... shoddy.

This topic is closed to new replies.

Advertisement