<436>shell
(1)數組
FILE *popen(const char *command, const char *type);函數
popen函數建立一個鏈接到另外一個進程的管道。ui
若type是「r」,則文件指針鏈接到command的標準輸出,可經過文件指針讀取command的輸出。this
若type是「w」,則文件指針鏈接到command的標準輸入,可經過文件指針向command發送數據。spa
int pclose(FILE *stream);
指針
//此例子爲用popen向分頁程序傳送文件。popen建立了鏈接到分頁程序的管道。 #include "apue.h" #include <sys/wait.h> #define PAGER "${PAGER:-more}" /* environment variable, or default */ int main(int argc, char *argv[]) { char line[MAXLINE]; FILE *fpin, *fpout; if (argc != 2) err_quit("usage: a.out <pathname>"); if ((fpin = fopen(argv[1], "r")) == NULL) err_sys("can't open %s", argv[1]); if ((fpout = popen(PAGER, "w")) == NULL) err_sys("popen error"); /* copy argv[1] to pager */ while (fgets(line, MAXLINE, fpin) != NULL) { if (fputs(line, fpout) == EOF) err_sys("fputs error to pipe"); } if (ferror(fpin)) err_sys("fgets error"); if (pclose(fpout) == -1) err_sys("pclose error"); exit(0); }
(2)code
#define PAGER "${PAGER:-more}" /* environment variable, or default */ if ((fpout = popen(PAGER, "w")) == NULL) err_sys("popen error");
若是shell變量PAGER已經定義,且其值非空,則使用其值,不然使用字符串more。進程
<437>popen函數和pclose函數的實現ip
#include "apue.h" #include <errno.h> #include <fcntl.h> #include <sys/wait.h> /* * Pointer to array allocated at run-time. */ static pid_t *childpid = NULL; /* * From our open_max(), {Prog openmax}. */ static int maxfd; FILE * popen(const char *cmdstring, const char *type) { int i; int pfd[2]; pid_t pid; FILE *fp; /* only allow "r" or "w" */ if ((type[0] != 'r' && type[0] != 'w') || type[1] != 0) { errno = EINVAL; return(NULL); } if (childpid == NULL) { /* first time through */ /* allocate zeroed out array for child pids */ maxfd = open_max(); if ((childpid = calloc(maxfd, sizeof(pid_t))) == NULL) return(NULL); } if (pipe(pfd) < 0) return(NULL); /* errno set by pipe() */ if (pfd[0] >= maxfd || pfd[1] >= maxfd) { close(pfd[0]); close(pfd[1]); errno = EMFILE; return(NULL); } if ((pid = fork()) < 0) { return(NULL); /* errno set by fork() */ } else if (pid == 0) { /* child */ if (*type == 'r') { close(pfd[0]); if (pfd[1] != STDOUT_FILENO) { dup2(pfd[1], STDOUT_FILENO); close(pfd[1]); } } else { close(pfd[1]); if (pfd[0] != STDIN_FILENO) { dup2(pfd[0], STDIN_FILENO); close(pfd[0]); } } /* close all descriptors in childpid[] */ for (i = 0; i < maxfd; i++) if (childpid[i] > 0) close(i); execl("/bin/sh", "sh", "-c", cmdstring, (char *)0); _exit(127); } /* parent continues... */ if (*type == 'r') { close(pfd[1]); if ((fp = fdopen(pfd[0], type)) == NULL) return(NULL); } else { close(pfd[0]); if ((fp = fdopen(pfd[1], type)) == NULL) return(NULL); } childpid[fileno(fp)] = pid; /* remember child pid for this fd */ return(fp); }
popen函數步驟。
(1)判斷行參type是否正確。
(2)若第一此調用popen函數則建立數組childpid。文件描述符作數組下標,數組元素保存子進程ID,數組大小爲最大打開文件數。
void *calloc(size_t nmemb, size_t size);
The calloc() function allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the allocated memory. The memory is set to zero. If nmemb or size is 0, then calloc() returns either NULL, or a unique pointer value that can later be successfully passed to free().
open_max()函數肯定文件描述符個數,即最大可打開文件數。
(3)建立管道,不使用值大於(或等於)open_max函數返回值的管道文件描述符。
(4)子進程中調用dup2,使其標準輸入或輸出成爲管道的讀或寫端。
(5)一個進程可能調用popen屢次,因此關閉那些之前調用popen打開的/如今仍然在子進程中打開着的I/O流。
(6)子進程中執行命令字符串。
(4)父進程調用fdopen函數產生popen函數的返回值。
FILE *fdopen(int fd, const char *mode);
The fdopen() function associates a stream with the existing file descriptor, fd.
(5)父進程保存子進程ID。
int fileno(FILE *stream);
The function fileno() examines the argument stream and returns its integer descriptor.
int pclose(FILE *fp) { int fd, stat; pid_t pid; if (childpid == NULL) { errno = EINVAL; return(-1); /* popen() has never been called */ } fd = fileno(fp); if (fd >= maxfd) { errno = EINVAL; return(-1); /* invalid file descriptor */ } if ((pid = childpid[fd]) == 0) { errno = EINVAL; return(-1); /* fp wasn't opened by popen() */ } childpid[fd] = 0; if (fclose(fp) == EOF) return(-1); while (waitpid(pid, &stat, 0) < 0) if (errno != EINTR) return(-1); /* error other than EINTR from waitpid() */ return(stat); /* return child's termination status */ }
<441>
(1)此程序演示在,應用程序和輸入之間插入一個程序以便對輸入進行變換處理。
//調用大小寫過濾程序讀取命令。 #include "apue.h" #include <sys/wait.h> int main(void) { char line[MAXLINE]; FILE *fpin; if ((fpin = popen("./myuclc", "r")) == NULL) err_sys("popen error"); for ( ; ; ) { fputs("prompt> ", stdout); fflush(stdout);//標準輸出一般是行緩衝,而提示符不包含換行符,因此在寫了提示以後,調用fflush if (fgets(line, MAXLINE, fpin) == NULL) /* read from pipe */ break; if (fputs(line, stdout) == EOF) err_sys("fputs error to pipe"); } if (pclose(fpin) == -1) err_sys("pclose error"); putchar('\n'); exit(0); }
(2)過濾程序,將大寫字符變換爲小寫字符。
#include <ctype.h> int main(void) { int c; while ((c = getchar()) != EOF) { if (isupper(c)) c = tolower(c); if (putchar(c) == EOF) err_sys("output error"); if (c == '\n') fflush(stdout); } exit(0); }