system替代函数

// 使用popen代替system函数
int my_system(const char * cmd) 
{ 
    FILE * fp; 
    int res;if (cmd == NULL) { 
        printf("my_system cmd is NULL!\n");
        return -1;
    } 

    if ((fp = popen(cmd, "r") ) == NULL) { 
        perror("popen");
        printf("popen error: %s/n", strerror(errno)); 
        return -1; 
    } 

    if ((res = pclose(fp)) == -1) { 
        printf("close popen file pointer fp error!\n"); 
     return res;
    } else if (res == 0) {
        return res;
    } 
    // printf("popen res is :%d\n", res); 

  return res; }

 

// 将SIGCHLD设置为SIG_DFL处理方式,这种写法可能也存在问题,能不用还是别用了。
int
my_system(const char *cmd_line) { int ret = 0; sighandler_t old_handler; old_handler = signal(SIGCHLD, SIG_DFL); ret = system(cmd_line); signal(SIGCHLD, old_handler); return ret; }

 

posted @ 2021-02-02 15:46  insistYuan  阅读(727)  评论(0编辑  收藏  举报