The Linux Popen () function is detailed

Source: Internet
Author: User

Table header File

#include <stdio.h>

Defining functions

FILE * Popen (const char * command,const char * type);

Function description

Popen () calls fork () to produce the child process and then calls/BIN/SH-C from the child process to execute the command of the parameter command. The parameter type can be read by using "R", and "W" represents the write. According to this type value, Popen () establishes a standard output device or standard input device that the pipeline connects to the child process, and then returns a file pointer. The process can then use this file pointer to read the output device of the child process or write to the standard input device of the child process. In addition, all functions that use the file pointer (file*) operation are also available, except for fclose ().

  • If type is r, then the calling process reads the standard output of command.
  • If Type is W, then the calling process writes to the command's standard input.

return value

Returns the file pointer if successful, otherwise returns NULL, the reason for the error is in errno.

Error code

The einval parameter type is not valid.

Precautions

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;char BUFFER[80];FP = Popen ("cat/etc/passwd", "R"), fgets (buffer, sizeof (buffer ), FP);p rintf ("%s", buffer);p close (FP);}

Perform

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

Reference:

Linux C Programming--popen functions

The Linux Popen () function is detailed

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.