C language library functions
Http://www.fzs8.net/C_Function/2007-06-13/c_5890.html
Use of the system () function in C language in Windows and Linux
System () functions in Windows
Function Name: System
Function: issue a DOS command
Usage: INT system (char * command );
The system function has been included in the standard C library and can be called directly.
Program example:
# Include & lt; stdlib. H & gt;
# Include & lt; stdio. H & gt;
I
System () functions in Windows
Function Name: System
Function: issue a DOS command
Usage: INT system (char * command );
The system function has been included in the standard C library and can be called directly.
Program example:
# Include <stdlib. h>
# Include <stdio. h>
Int main (void)
{
Printf ("about to spawn command.com and run a DOS command ");
System ("dir ");
Return 0;
}
System () functions in Linux
(Execute shell commands)
Related functions
Fork, execve, waitpid, popen
Header file
# I nclude <stdlib. h>
Define functions
INT system (const char * string );
Function Description
System () calls fork () to generate sub-processes. The sub-process calls/bin/sh-C string to execute the command represented by the string parameter, this command> after the command is executed, the original called process is returned. The sigchld signal is temporarily shelved during system () calls. The SIGINT and sigquit
The signal is ignored.
Return Value
=-1: an error occurs.
= 0: The call is successful but no sub-process exists.
> 0: ID of the child process that successfully exits
If system () fails to call/bin/sh, 127 is returned, and-1 is returned for other causes of failure. If the string parameter is a null pointer, a non-zero value is returned. If system () is successfully called, the return value after the shell command is executed is returned. However, the returned value may also be 127 returned when system () fails to call/bin/sh, therefore, it is best to check errno again to confirm the execution is successful.
Additional instructions
Do not use system () when writing programs with SUID/SGID permissions. System () inherits environment variables, which may cause system security problems.
Example
# I nclude <stdlib. h>
Main ()
{
System ("LS-Al/etc/passwd/etc/shadow ");
}
Execution result:
-RW-r -- 1
Root 705 Sep 3 13: 52/etc/passwd
-R --------- 1 Root 572 Sep 2 15: 34/etc/Shado
Example 2:
Char TMP [];
Sprintf (TMP, "/bin/Mount-T vfat % S/mnt/USB", Dev );
System (TMP );
Dev is/dev/sda1.