search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

Linux C 呼叫外部命令,並讀取相關執行結果 – jashliao部落格

Linux C/C++ 呼叫外部命令,並讀取相關執行結果


GITHUB: https://github.com/jash-git/Jash_LinuxC/tree/master/Linux C呼叫外部命令_並讀取相關執行結果


https://github.com/jash-git/Jash_LinuxC/tree/master/Linux_C popen(取得PID和取得呼叫外部程式的結果)


code:

/*
透過 Linux C 呼叫外部命令,並讀取相關執行結果
*/
#include 
int main(int argc, char *argv[])
{
	FILE *pp;
	char cbuffer[1280];
	pp = popen("ifconfig ","r");
	
	cbuffer[0]='\0';
	while (fgets(cbuffer,sizeof(cbuffer),pp) != NULL)
	{
		printf("%s",cbuffer);
		cbuffer[0]='\0';
	}
	pclose(pp);

	printf("\n");
	return 0;
}
/*
	http://webcache.googleusercontent.com/search?q=cache:6_N6So_JZgMJ:blog.163.com/ky_147258/blog/static/14288004920118685640778/+&cd=9&hl=zh-TW&ct=clnk&lr=lang_zh-CN|lang_zh-TW
	linux 獲取進程ID以及popen用法等
	掃描ps命令的輸出: 
*/
/*
pidof :查找運行程式的進程ID/通過進程名查看進程ID

語 法
pidof[必要參數][選擇參數][程式]
功 能
pidof 命令:可以顯示正在運行的進程ID
參數
必要參數
-s 單次選中
-c 只返回進程ID
-x 同時返回腳本

選擇參數
-o
範例2: 每次只返回一個進程號 

root@snail-hnlinux:~# pidof sshd -s
31327
*/
#include   
#include   
#include   
   
#define PS_COMMAND "ps -ax"  
#define SCAN(buf) (sscanf(buf,"%d %s %s %s %s",pid,s[0],s[1],s[2],cmd) == 5)  
   
/* Returns number of pid matching process name, or -1 on error */  
int lookup_pid(char *name, pid_t *pids, int n)  
{  
  FILE *fp;  
  char buf[256], cmd[200], s[3][32];  
  pid_t *pid;  
   
  if ((fp = popen(PS_COMMAND, "r")) == NULL)  
    return -1;  
   
  pid = pids;  
  while (n > 0 && fgets(buf, sizeof(buf), fp))  
    if (SCAN(buf) && strstr(cmd, name)) /* improve on this simple name check */  
      pid++, n--;  
   
  fclose(fp);  
  return pid - pids;  
}  
   
int main(int argc, char **argv)  
{  
  char *process;  
  pid_t pids[8];  
  int n, i;  
   
  if (argc != 2) {  
    printf("Usage: %s process_name  ", argv[0]);  
    return 1;  
  }  
  process = argv[1];  
   
  if ((n = lookup_pid(process, pids, 8)) == -1) {  
    printf("Error reading pid of %s  ", process);  
    return 2;  
  }  
   
  printf("%d %s  ", n, process);  
  for (i = 0; i < n; i++)  
    printf("%d  ", pids[ i]);  
  return 0;  
}
#include 
#include 
#include 
#include 
#include 

#define READ 0
#define WRITE 1

pid_t
popen2(const char *command, int *infp, int *outfp)
{
    int p_stdin[2], p_stdout[2];
    pid_t pid;

    if (pipe(p_stdin) != 0 || pipe(p_stdout) != 0)
        return -1;

    pid = fork();

    if (pid < 0)
        return pid;
    else if (pid == 0)
    {
        close(p_stdin[WRITE]);
        dup2(p_stdin[READ], READ);
        close(p_stdout[READ]);
        dup2(p_stdout[WRITE], WRITE);

        execl("/bin/sh", "sh", "-c", command, NULL);
        perror("execl");
        exit(1);
    }

    if (infp == NULL)
        close(p_stdin[WRITE]);
    else
        *infp = p_stdin[WRITE];

    if (outfp == NULL)
        close(p_stdout[READ]);
    else
        *outfp = p_stdout[READ];

    return pid;
}

int main(int argc, char **argv)
{
    int infp, outfp;
    char buf[128];

    if (popen2("sort", &infp, &outfp) <= 0)
    {
        printf("Unable to exec sort\n");
        exit(1);
    }

    write(infp, "Z\n", 2);
    write(infp, "D\n", 2);
    write(infp, "A\n", 2);
    write(infp, "C\n", 2);
    close(infp);

    *buf = '\0';
    read(outfp, buf, 128);

    printf("buf = '%s'\n", buf);

    return 0;
}



熱門推薦

本文由 jashliaoeuwordpress 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦