学习c难以理解的功能

时间:2013-09-11 16:49:01

标签: c

好吧,让我们直言不讳。目前我正在与Zed的Shaw Learn C进行艰苦的战斗,而且我非常困难于他的练习26.事情是,我无法理解一个var arg函数中发生了什么 - Shell_exec。有人可以向我解释一下这个for循环是如何工作的以及它们与var arg系统有什么关系?

*File shell.c*

include "shell.h"
include "dbg.h"
include <stdarg.h>

int Shell_exec(Shell template, ...)
      {
    apr_pool_t *p = NULL;
    int rc = -1;
    apr_status_t rv = APR_SUCCESS;
    va_list argp;
    const char *key = NULL;
    const char *arg = NULL;
    int i = 0;

    rv = apr_pool_create(&p, NULL);
    check(rv == APR_SUCCESS, "Failed to create pool.");

    va_start(argp, template);

    for(key = va_arg(argp, const char *);
        key != NULL;
        key = va_arg(argp, const char *))
    {
        arg = va_arg(argp, const char *);

        for(i = 0; template.args[i] != NULL; i++) {
            if(strcmp(template.args[i], key) == 0) {
                template.args[i] = arg;
                break; // found it
            }
        }
    }

    rc = Shell_run(p, &template);
    apr_pool_destroy(p);
    va_end(argp);
    return rc;

error:
    if(p) {
        apr_pool_destroy(p);
    }
    return rc;
}

int Shell_run(apr_pool_t *p, Shell *cmd)
{
    apr_procattr_t *attr;
    apr_status_t rv;
    apr_proc_t newproc;

    rv = apr_procattr_create(&attr, p);
    check(rv == APR_SUCCESS, "Failed to create proc attr.");

    rv = apr_procattr_io_set(attr, APR_NO_PIPE, APR_NO_PIPE,
            APR_NO_PIPE);
    check(rv == APR_SUCCESS, "Failed to set IO of command.");

    rv = apr_procattr_dir_set(attr, cmd->dir);
    check(rv == APR_SUCCESS, "Failed to set root to %s", cmd->dir);

    rv = apr_procattr_cmdtype_set(attr, APR_PROGRAM_PATH);
    check(rv == APR_SUCCESS, "Failed to set cmd type.");

    rv = apr_proc_create(&newproc, cmd->exe, cmd->args, NULL, attr, p);
    check(rv == APR_SUCCESS, "Failed to run command.");

    rv = apr_proc_wait(&newproc, &cmd->exit_code, &cmd->exit_why, APR_WAIT);
    check(rv == APR_CHILD_DONE, "Failed to wait.");

    check(cmd->exit_code == 0, "%s exited badly.", cmd->exe);
    check(cmd->exit_why == APR_PROC_EXIT, "%s was killed or crashed", cmd->exe);

    return 0;

error:
    return -1;
}

Shell CLEANUP_SH = {
    .exe = "rm",
    .dir = "/tmp",
    .args = {"rm", "-rf", "/tmp/pkg-build", "/tmp/pkg-src.tar.gz",
        "/tmp/pkg-src.tar.bz2", "/tmp/DEPENDS", NULL}
};

Shell GIT_SH = {
    .dir = "/tmp",
    .exe = "git",
    .args = {"git", "clone", "URL", "pkg-build", NULL}
};

Shell TAR_SH = {
    .dir = "/tmp/pkg-build",
    .exe = "tar",
    .args = {"tar", "-xzf", "FILE", "--strip-components", "1", NULL}
};

Shell CURL_SH = {
    .dir = "/tmp",
    .exe = "curl",
    .args = {"curl", "-L", "-o", "TARGET", "URL", NULL}
};

Shell CONFIGURE_SH = {
    .exe = "./configure",
    .dir = "/tmp/pkg-build",
    .args = {"configure", "OPTS", NULL},
};

Shell MAKE_SH = {
    .exe = "make",
    .dir = "/tmp/pkg-build",
    .args = {"make", "OPTS", NULL}
};

Shell INSTALL_SH = {
    .exe = "sudo",
    .dir = "/tmp/pkg-build",
    .args = {"sudo", "make", "TARGET", NULL}
};
}

1 个答案:

答案 0 :(得分:0)

通常,当你调用Shell_exec时,你会这样称呼它:

Shell_exec(foo, bar, baz);

然后你做

va_start(argp, template);

告诉计算机“好吧,从FOO开始(或在函数内部'模板')将其他所有内容放入argp变量并以NULL结束argp变量”

所以,在argp中,它看起来像这样:

argp == [&bar, &baz, NULL]

当它到达for循环时,va_arg的作用是它从argp中删除第一个条目然后转到bar的位置。但是,它实际上并不知道什么是bar,所以你必须告诉va_arg地址中包含什么类型的变量,所以argp现在是

argp == [&baz, NULL]
key == bar

但是,你注意到循环本身还有另一个va_arg!这消耗了argp的另一个变量!

argp == [NULL]
key == bar
arg == baz

循环的下一次迭代结束它,因为

argp == []
key == NULL

请注意,由于for循环使用Shell_exec中的两个参数,因此在调用Shell_exec时必须始终具有ODD数量的参数(一个模板参数,然后始终是偶数)。

它背后的实际想法是,当你调用Shell_exec时,你可以这样称呼它:

Shell_exec(TAR_SH, "TARGET", "./install/file/location"); 

所以你看一下你的TAR_SH结构,注意有默认参数“TARGET”,然后用“./install/file/location”替换它。重复多个要替换的参数,然后将生成的修改后的TAR_SH发送给其他函数。

希望这会有所帮助。