1、popen()
popen():進程I/O函數,與pclose函數一起使用。
1.1函數定義:
FILE * popen ( const char * command , const char * type );
int pclose ( FILE * stream );
1.2包含頭文件:
#include <stdio.h>
1.3函數說明:
popen() 函數通過創建一個管道,調用 fork 產生一個子進程,執行一個 shell 以運行命令來開啓一個進程。這個進程必須由 pclose() 函數關閉,而不是 fclose() 函數。pclose() 函數關閉標準 I/O 流,等待命令執行結束,然後返回 shell 的終止狀態。如果 shell 不能被執行,則 pclose() 返回的終止狀態與 shell 已執行 exit 一樣。
type 參數只能是讀或者寫中的一種,得到的返回值(標準 I/O 流)也具有和 type 相應的只讀或只寫類型。如果 type 是 "r" 則文件指針連接到 command 的標準輸出;如果 type 是 "w" 則文件指針連接到 command 的標準輸入。
command 參數是一個指向以 NULL 結束的 shell 命令字符串的指針。這行命令將被傳到 bin/sh 並使用-c 標誌,shell 將執行這個命令。
popen 的返回值是個標準 I/O 流,必須由 pclose 來終止。前面提到這個流是單向的。所以向這個流寫內容相當於寫入該命令的標準輸入;命令的標準輸出和調用 popen 的進程相同。與之相反的,從流中讀數據相當於讀取命令的標準輸出;命令的標準輸入和調用 popen 的進程相同。
1.4函數返回值
如果調用 fork() 或 pipe() 失敗,或者不能分配內存將返回NULL,否則返回標準 I/O 流。
1.5 函數錯誤狀態
popen 沒有爲內存分配失敗設置 errno 值。
如果調用 fork() 或 pipe() 時出現錯誤,errno 被設爲相應的錯誤類型。
如果 type 參數不合法,errno將返回EINVAL。
1.6 實例1
C代碼調用 Shell的 ls 命令 ,打印當前路徑文件夾下的所有文件名:
#include<stdio.h>
#define _LINE_LENGTH 300
int main(void)
{
FILE *file;
char line[_LINE_LENGTH];
file = popen("ls", "r");
if (NULL != file)
{
while (fgets(line, _LINE_LENGTH, file) != NULL)
{
printf("line=%s\n", line);
}
}
else
{
return 1;
}
pclose(file);
return 0;
}
輸出結果:
line=ls_command.c
line=ls_command.o
line=popen.c
line=popen.o
2、從popen()返回的流中讀取命令執行結果的字符串,字符串'\0'結尾符號前面還有一個'\n' 換行符號的問題
#include<stdio.h>
#include<string.h>
#define _LINE_LENGTH 256
#define COMMAND "sudo dmidecode -t2 | grep 'Product Name' | awk '{print $3}'"
int main(void)
{
FILE *file;
char line[_LINE_LENGTH];
file = popen(COMMAND, "r");
int Length = -1;
char *p = NULL;
int count = 0;
if (NULL != file)
{
while (fgets(line, _LINE_LENGTH, file) != NULL)
{
// line is end with '\n''\0'
printf("line=%s", line);
Length = strlen(line);
printf("Length = %d\n",Length);
printf("line[Length - 1] = %d:%c",line[Length - 1],line[Length - 1]);
printf("line[Length] = %d:%c",line[Length],line[Length]);
if(line[Length] == '\0')
{
printf("line is end with \'\\0\'\n");
}
line[Length - 1] = '\0';\\ replace line[Length - 1] = '\n' with '\0'
if(strcmp(line,"LNVNB161216") == 0)
{
printf("The Product Name is Matched!\n");
}
}
}
else
{
return 1;
}
pclose(file);
return 0;
}
輸出結果:
line=LNVNB161216
Length = 12
line[Length - 1] = 10:
line[Length] = 0:line is end with '\0'
The Product Name is Matched!
從上面的結果可以看出,從popen()返回的文件流指針file中讀取的命令執行結果字符串,每一個行字符串是以ASCII的換行符'\n'結尾的,ASCII的換行符號(LF),ASCII值爲10。
求執行結果一行字符串的長度strlen函數放回的長度值是包含換行符號的。
參考
[1]https://baike.baidu.com/item/popen/5301781?fr=aladdin
[2]https://baike.baidu.com/item/ASCII/309296?fromtitle=ascii%E7%A0%81&fromid=99077&fr=aladdin