-----Popen functions for advanced Programming in the UNIX environment

Source: Internet
Author: User
Tags gopher

header Files : #include <stdio.h>


Function Description:

FILE * Popen (const char * command, const char * type); int pclose (file * stream);

Description: (Refer to Advanced Programming for UNIX environments)

the Popen () function creates a child process by creating a pipe, calling fork, and executes a shell to run the command to open a process. This process must be closed by the Pclose () function instead of the fclose () function. The Pclose () function closes the standard I/O stream, waits for the command execution to end, and then returns the Shell's terminating state. If the shell cannot be executed, the terminating state returned by Pclose () is the same as the shell has executed exit. the type parameter can only be read or write, and the resulting return value (standard I/O stream) also has a read-only or write-only type corresponding to type. If type is "R" then the file pointer is connected to the standard output of command, and if type is "W" then the file pointer is connected to the standard input of command. the command parameter is a pointer to a NULL-terminated shell command string. This line of command will be passed to bin/sh and use the-c flag and the shell will execute the command. The return value of the Popen is a standard I/O stream that must be terminated by Pclose. As mentioned earlier, this flow is unidirectional. So writing to this stream is equivalent to writing the standard input to the command, and the standard output of the command is the same as the process of calling Popen. In contrast, reading the data from the stream is equivalent to reading the standard output of the command, and the standard input for the command is the same as the process of calling Popen.

Example one:

#include <sys/types.h>  #include <unistd.h>  #include <stdlib.h>  #include <stdio.h >  #include <string.h>int main ()  {      FILE   *fpin;      FILE   *fpout;    Char   buf[1024];          memset (buf, ' Fpin ', sizeof (BUF));//Initialize the BUF so that it is not written as garbled into the file    Popen = ls-l ("R");//The output of the "ls-l" command is read through the pipe ("r" parameter) to fi le* stream    fpout = fopen ("Test_popen.txt", "w+");//Create a new writable file    fread (buf, sizeof (char), sizeof (BUF)  , Fpin);  Read the data stream just file* fpin into buf    fwrite (buf, 1, sizeof (BUF), fpout);//writes data from BUF to file* fpout corresponding stream, also writes to file        Pclose (Fpin);      Fclose (fpout);        

[Email protected] popen]$ gcc popen.c
[Email protected] popen]$./a.out
[email protected] popen]$ cat Test_popen.txt
Total 24
-rwxrwxr-x 1 uglychen uglychen 5680 June, 14:04 a.out
-rw-rw-r--1 uglychen uglychen 771 June, 14:03 POPEN.C
-rw-rw-r--1 uglychen uglychen 0 June 14:05 Test_popen.txt


Example two: popen2.c

 #include <sys/types.h> #include <unistd.h> #include <stdlib.h> # Include <stdio.h> #include <string.h>//#include <sys/wait.h> #define PAGER "${pager:-more}"/* Enviro        Nment variable, or default */#define MAXLINE 4096int Main (int argc, char *argv[]) {char line[maxline];        FILE *fpin, *fpout;        if (argc! = 2) printf ("Usage:a.out <pathname>");        if (Fpin = fopen (Argv[1], "r")) = = NULL) printf ("Can ' t open%s", argv[1]);        if (Fpout = Popen (PAGER, "w")) = = NULL) printf ("Popen error"); /* Copy argv[1] to pager * * while (fgets (line, MAXLINE, fpin)! = NULL) {if (Fputs (line, fpout) = = E        of) printf ("Fputs error to pipe");        } if (Ferror (Fpin)) printf ("Fgets error");        if (Pclose (fpout) = =-1) printf ("Pclose error"); Exit (0);} 

[Email protected] popen]$gcc popen2.c-o popen2
[[email protected] popen]$ ls
A.out popen2 popen2.c popen.c test_popen.txt
[Email protected] popen]$./popen2
Segmentation fault
[Email protected] popen]$./POPEN2/ETC/PASSWD
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Adm:x:3:4:adm:/var/adm:/sbin/nologin
Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
Sync:x:5:0:sync:/sbin:/bin/sync
Shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
Halt:x:7:0:halt:/sbin:/sbin/halt
Mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
News:x:9:13:news:/etc/news:
Uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
Operator:x:11:0:operator:/root:/sbin/nologin
Games:x:12:100:games:/usr/games:/sbin/nologin
Gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
Ftp:x:14:50:ftp User:/var/ftp:/sbin/nologin
Nobody:x:99:99:nobody:/:/sbin/nologin
NSCD:X:28:28:NSCD Daemon:/:/sbin/nologin
Vcsa:x:69:69:virtual Console Memory Owner:/dev:/sbin/nologin
Pcap:x:77:77::/var/arpwatch:/sbin/nologin
Ntp:x:38:38::/etc/ntp:/sbin/nologin
Dbus:x:81:81:system message Bus:/:/sbin/nologin
Avahi:x:70:70:avahi Daemon:/:/sbin/nologin
Rpc:x:32:32:portmapper RPC User:/:/sbin/nologin
Mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin
Smmsp:x:51:51::/var/spool/mqueue:/sbin/nologin
Apache:x:48:48:apache:/var/www:/sbin/nologin
Hsqldb:x:96:96::/var/lib/hsqldb:/sbin/nologin
Sshd:x:74:74:privilege-separated Ssh:/var/empty/sshd:/sbin/nologin
Rpcuser:x:29:29:rpc Service User:/var/lib/nfs:/sbin/nologin
Nfsnobody:x:65534:65534:anonymous NFS User:/var/lib/nfs:/sbin/nologin
Xfs:x:43:43:x Font Server:/etc/x11/fs:/sbin/nologin
Haldaemon:x:68:68:hal Daemon:/:/sbin/nologin
Avahi-autoipd:x:100:101:avahi-autoipd:/var/lib/avahi-autoipd:/sbin/nologin
Gdm:x:42:42::/var/gdm:/sbin/nologin
Uglychen:x:500:501:uglychen:/home/uglychen:/bin/bash
Chen1:x:501:502::/home/chen1:/bin/bash


Example three:






-----Popen functions for advanced Programming in the UNIX environment

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.