"Reprint" How to invoke the shell command in C language

Source: Internet
Author: User

Reprinted from: http://blog.csdn.net/chdhust/article/details/7951576 How to invoke shell commands in C language

In the Linux operating system, many shell commands are very simple to use, and the program implementations of these shell commands have been implemented at the bottom. Sometimes it is necessary to invoke the shell command in the program, so that you do not have to manually enter the shell command on the console, the following is an example of three functions to explain how to invoke a shell command in the C language.

1. System (execute shell command)

Correlation function Fork,execve,waitpid,popen
Table header file #include <stdlib.h>
Defines the function int system (const char * string);
The function Description System () calls fork () to produce a child process that calls/bin/sh-c
String to execute the command represented by the argument string string, which, when executed, follows the
The process that returned the original call. During call to System (), the SIGCHLD signal is temporarily
On hold, the SIGINT and sigquit signals are ignored.
The return value returns 127 if System () fails when calling/bin/sh, and other reasons for failure return-
1. If the argument string is a null pointer (null), a value other than 0 is returned. If the system () tune
With success, the return value is returned after the shell command is executed, but this return value also has
It is possible for system () to call the 127 returned by/bin/sh failure, so it is better to check
errno to confirm the success of the execution.
Additional instructions do not use System () when writing programs with Suid/sgid permissions, and System () will
Inherit environment variables, which can cause system security problems through environment variables.

Example:

#include <stdlib.h>
Main ()
{
System ("Ls-al/etc/passwd/etc/shadow");
}

2, Popen (Establish pipeline I/O)

Correlation function Pipe,mkfifo,pclose,fork,system,fopen
Table header file #include <stdio.h>
Defines the function FILE * popen (const char * command,const char * type);
The function Description Popen () invokes fork () to produce the child process and then calls the/bin/sh-c from the child process
To execute the command of the parameter command. The parameter type can be read with "R", "W"
Represents the write. In accordance with this type value, Popen () establishes the standard output of the pipe connection to the child process.
The device or standard input device, and then returns a file pointer. Subsequent processes can facilitate
This file pointer is used to read the output device of the child process or to write to the child process's standard input
into the device. In addition, all functions that use the file pointer (file*) operation can also make
In addition to Fclose ().
The return value returns a file pointer if successful, otherwise returns NULL, and the reason for the error is stored in errno.
The error code einval parameter type is not valid.
Note When writing programs with Suid/sgid permissions, try to avoid using popen (), Popen ()
will inherit environment variables and may cause system security problems through environment variables.

Example:

?
#include<stdio.h>main(){FILE * fp;charbuffer[80];fp=popen(“cat /etc/passwd”,”r”);fgets(buffer,sizeof(buffer),fp);printf(“%s”,buffer);pclose(fp);}

Executive root:x:0 0:root:/root:/bin/bash

3. Use Vfork () to create a new child 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");
}
}

"Reprint" How to invoke the shell command in C language

Related Article

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.