How to call shell commands in C Language

Source: Internet
Author: User

1,System (execute shell command)

fork, execve, waitpid, popen
header file # include
define the INT system (const char * string) function.
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. After the command is executed, the original called process is returned along with
. When system () is called, The sigchld signal is put on hold temporarily. The SIGINT and sigquit signals are ignored.
return value: If system () fails to be called/bin/sh, 127 is returned, and other causes of failure are returned-
1. If the string parameter is a null pointer, a non-zero value is returned. If the system () call
succeeds, the return value after the shell command is executed is returned. However, the returned value may also be System () the error 127 returned when/bin/sh fails to be called. Therefore, it is best to check
errno to confirm that the execution is successful.
do not use system (), system () when writing Programs with SUID/SGID permissions () yes
inherit environment variables. Using environment variables may cause system security problems.

Example:

# Include<Stdlib. h>
Main ()
{
System ("ls-Al/Etc/Passwd/Etc/Shadow ");
}



2,Popen (create pipeline I/O)

Related functions: pipe, mkfifo, pclose, fork, system, and fopen
Header file # include <stdio. h>
Define the function file * popen (const char * command, const char * type );
Function Description: popen () calls fork () to generate sub-processes, and then calls/bin/sh-C from sub-processes.
Command. The type parameter can be read by using "r", "W"
Indicates writing. According to this type value, popen () creates a standard pipe to connect to the sub-process
Output device or standard input device, and then return a file pointer. Then the process will be available
This file pointer is used to read the output device of a sub-process or to write data to a sub-process.
Enter the device. In addition, all functions that use file * operations can also
In addition to fclose.
If the returned value is successful, the file pointer is returned. Otherwise, null is returned. The error cause is stored in errno.
ErrorCodeThe einval parameter type is invalid.
Note: Avoid using popen () and popen () when writing programs with SUID/SGID permissions ()
It inherits environment variables and may cause system security problems.

 
 

Example:

 
 
 
# Include <stdio. h> main () {file * FP; char buffer [80]; FP = popen ("cat/etc/passwd", "R"); fgets (buffer, sizeof (buffer), FP); printf ("% s", buffer); pclose (FP );}
 
 

Run root: X: 0 0: Root:/root:/bin/bash.



3. Use vfork () to create a sub-process and then call the exec function family.

 # Include  <  Unistd. h  >  
Main ()
{
Char * Argv [] = {"Ls "," - Al "," / Etc / Passwd ",( Char * )};

If (Vfork () = = 0 )
{
Execv (" / Bin / Ls ", argv );
} Else {
Printf ("This Is The parent process \ n ");
}
}

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.