C语言获取Shell输出

int shellOut(const char *cmd, char *buff, int buffLen)
{
    if(cmd == NULL || buff == NULL)
    {
        return -1;
    }

    int i = 0, ch = -1;
    FILE *fStream = NULL;

    if( (fStream = popen(cmd, "r")) == NULL )
    {
        return -1;
    }
    i = 0;
    while( (ch = fgetc(fStream)) != EOF )
    {
        buff[i] = ch;
        i++;
        if(i >= buffLen)
        {
            break;
        }
    }
    pclose(fStream);

    return 0;
}

你可能感兴趣的:(c语言,Linux编程)