在GNU Linux C编程中,要想进行系统命令的执行的话,只提供了system接口,但是此接口并不能得到命令执行后所输出的值,而只能够得到命令是否执行成功的结果。仅仅这样的功能还是不够的,有的时候是要必须通过命令的输出来判断下一步的结果或步骤的,那么怎么样能够得到system命令执行的结果呢?那就可以使用到popen函数和fgets函数进行命令的输出信息的获取了,实际例子如下:

注意:此接口只能够获取命令输出的最后一行的信息,若有多行输出信息将不能够全部获取到,此封装接口只适用于得到命令执行结果的最后一行的信息。

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

int super_system(const char * cmd, char *retmsg, int msg_len)
{
        FILE * fp;
        int res = -1;
        if (cmd == NULL || retmsg == NULL || msg_len < 0)
        {
                printf("Err: Fuc:%s system paramer invalid!\n", __func__);
                return 1;
        }
        if ((fp = popen(cmd, "r") ) == NULL)
        {
                perror("popen");
                printf("Err: Fuc:%s popen error: %s\n", __func__, strerror(errno));
                return 2;
        }
        else
        {
                memset(retmsg, 0, msg_len);
                while(fgets(retmsg, msg_len, fp));
                {
                        printf("Fuc: %s fgets buf is %s\n", __func__, retmsg);
                }
                if ( (res = pclose(fp)) == -1)
                {
                        printf("Fuc:%s close popen file pointer fp error!\n", __func__);
                        return 3;
                }
                //drop #012 from system result retmsg.
                retmsg[strlen(retmsg)-1] = '\0';
                return 0;
        }
}

int main()
{
    char *cmd = "whoami";
    char *cmd1 = "initctl list";
    char retmsg[1024] = {0};
    int ret = 0;
    ret  = super_system(cmd, retmsg, sizeof(retmsg));
    printf("system ret is %d retmsg is \n%s\n", ret, retmsg);
    return 0;
}

main函数中使用了whoami的命令,执行结果即是当前用户名。
执行结果:
linuxidc@ufo:~$ ./a.out 
Fuc: super_system fgets buf is linuxidc

system ret is 0 retmsg is 
linuxidc

/*********************************************************************
 * Author  : Samson
 * Date    : 03/13/2015
 * Test platform:
 *              3.13.0-24-generic
 *              GNU bash, 4.3.11(1)-release 
 * *******************************************************************/

可以返回执行结果的system函数加强版本的更多相关文章

  1. 能够返回运行结果的system函数加强版本号

    /*********************************************************************  * Author  : Samson  * Date   ...

  2. 对于linux下system()函数的深度理解(整理)

    原谅: http://blog.sina.com.cn/s/blog_8043547601017qk0.html 这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同 ...

  3. system函数的总结

    最近在看APUE第10章中关于system函数的POSIX.1的实现.关于POSIX.1要求system函数忽略SIGINT和SIGQUIT,并且阻塞信号SIGCHLD的论述,理解得不是很透彻,本文就 ...

  4. (转)system()函数

    [C/C++]Linux下system()函数引发的错误   今天,一个运行了近一年的程序突然挂掉了,问题定位到是system()函数出的问题,关于该函数的简单使用在我上篇文章做过介绍: http:/ ...

  5. 转:对于linux下system()函数的深度理解(整理)

    这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同的system()函数,直接在shell下输入system()函数中调用的命令也都一切正常.就没理这个bug,以为 ...

  6. system函数遇到的问题 - 程序死掉

    system函数遇到的问题 解决方案见最下边 http://blog.csdn.net/yangzhenzhen/article/details/51505176 这几天调程序(嵌入式linux),发 ...

  7. 【C/C++】Linux下system()函数引发的错误

    http://my.oschina.net/renhc/blog/54582 [C/C++]Linux下system()函数引发的错误 恋恋美食  恋恋美食 发布时间: 2012/04/21 11:3 ...

  8. system函数遇到的问题

     这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同的system()函数,直接在shell下输入system()函数中调用的命令也都一切正常.就没理这个bug,以 ...

  9. (笔记)Linux下system()函数的深度理解(整理)

    注:从其它地方转的非常好的一篇文章,值得深究! 这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同的system()函数,直接在shell下输入system()函数 ...

随机推荐

  1. 67. Add Binary

    public class Solution { public String addBinary(String a, String b) { char[] aa=a.toCharArray(); cha ...

  2. 2015GitWebRTC编译实录5

    2015.07.20 libaudio_encoder_interface/libaudio_decoder_interface 编译通过将encoder,decoder两个lib合并了,后面需要看看 ...

  3. ZOJ 1243 URLs

    /*In the early nineties, the World Wide Web (WWW) was invented. Nowadays, most people think that the ...

  4. 转:struts标签之select详解

    <html:select>生成HTML<select>元素 <html:option>:生成HTML<option>元素 <html:option ...

  5. install kinect driver for ARM---38

    原创博客:转载请标明出处:http://www.cnblogs.com/zxouxuewei/ The video describes connecting a Microsoft Kinect to ...

  6. CString用法总结

    概述:CString是MFC中提供的用于处理字符串的类,是一种很有用的数据类型. 它很大程度上简化了MFC中的许多操作,使得MFC在做字符串操作时方便了很多. 不管怎样,使用CString有很多的特殊 ...

  7. POJ 2186 Popular Cows(强连通)

                                                                  Popular Cows Time Limit: 2000MS   Memo ...

  8. hotspot

    http://openjdk.java.net/groups/hotspot/ Source code Bundles Download Mercurial respository (read-onl ...

  9. nginx配置-http和https

    #user nobody;worker_processes 1;error_log logs/error.log;#error_log logs/error.log notice;#error_log ...

  10. SecureCRT注册机使用方法

    SecureCRT_7.3注册机激活步骤如下: 1)准备工作 安装好SecureCRT软件, 下载并得到该注册机. 2)保持SecureCRT软件关闭(运行的话会提示你正在运行的,关闭就好). 3)将 ...