Use of system functions in C language in Linux

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.