0

我想在 C 程序中运行 shell 命令并获得标准输出。

我在这个函数中做到了:

int run_shell_cmd_nout(const char* cmd)
{
    FILE *fp;
    char out[4096]     = {0};
    char str[256]       = {0};
    char full_cmd[1024] = {0};
    int result = 0;

    // Compose full shell command
    if (!sprintf(full_cmd, "/system/bin/%s", cmd))
    {
        printf("Failed to compose full shell command\n");
        return -1;
    }

    // Open the command for reading.
    fp = popen(full_cmd, "r");
    if (fp == NULL)
    {
        printf("Failed to run command\n");
        return -1;
    }

    // Read the output a line at a time - output it.
    while(!feof(fp))
    {
         if(fgets(str, 256, fp) != NULL)
         {
             result = -1;
             strcat(out, str);
         }
    }

    pclose(fp);

    if (result != 0)
    {
        printf("%s\n", out);
        return -1;
    }

    return 0;
}

但它不适用于 insmod。

调用insmod时有什么方法可以拦截所有输出吗?

4

0 回答 0