免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2442 | 回复: 3
打印 上一主题 下一主题

busybox的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-04-01 10:11 |只看该作者 |倒序浏览
busybox.c有这样一段代码:

int main(int argc, char **argv)
{
    const char *s;

    bb_applet_name = argv[0];

    if (bb_applet_name[0] == '-')
        bb_applet_name++;

    for (s = bb_applet_name; *s != '\0' {
        if (*s++ == '/')
            bb_applet_name = s;
    }

#ifdef CONFIG_LOCALE_SUPPORT
#ifdef CONFIG_INIT
    if(getpid()!=1) /* Do not set locale for `init' */
#endif
    {
        setlocale(LC_ALL, "";
    }
#endif

    run_applet_by_name(bb_applet_name, argc, argv);
    bb_error_msg_and_die("applet not found";
}

int busybox_main(int argc, char **argv)
{
    int col = 0, len, i;

#ifdef CONFIG_FEATURE_INSTALLER
    /*
     * This style of argument parsing doesn't scale well
     * in the event that busybox starts wanting more --options.
     * If someone has a cleaner approach, by all means implement it.
     */
    if (argc > 1 && (strcmp(argv[1], "--install" == 0)) {
        int use_symbolic_links = 0;
        int rc = 0;
        char *busybox;

        /* to use symlinks, or not to use symlinks... */
        if (argc > 2) {
            if ((strcmp(argv[2], "-s" == 0)) {
                use_symbolic_links = 1;
            }
        }

        /* link */
        busybox = busybox_fullpath();
        if (busybox) {
            install_links(busybox, use_symbolic_links);
            free(busybox);
        } else {
            rc = 1;
        }
        return rc;
    }
#endif /* CONFIG_FEATURE_INSTALLER */

    argc--;

    /* If we've already been here once, exit now */
    if (been_there_done_that == 1 || argc < 1) {
        const struct BB_applet *a = applets;
        int output_width = 60;

#ifdef CONFIG_FEATURE_AUTOWIDTH
        /* Obtain the terminal width.  */
        get_terminal_width_height(0, &output_width, NULL);
        /* leading tab and room to wrap */
        output_width -= 20;
#endif

        fprintf(stderr, "%s\n\n"
                "Usage: busybox [function] [arguments]...\n"
                "   or: [function] [arguments]...\n\n"
                "\tBusyBox is a multi-call binary that combines many common Unix\n"
                "\tutilities into a single executable.  Most people will create a\n"
                "\tlink to busybox for each function they wish to use, and BusyBox\n"
                "\twill act like whatever it was invoked as.\n"
                "\nCurrently defined functions:\n", bb_msg_full_version);

        while (a->name != 0) {
            col +=
                fprintf(stderr, "%s%s", ((col == 0) ? "\t" : ", ",
                        (a++)->name);
            if (col > output_width && a->name != 0) {
                fprintf(stderr, ",\n";
                col = 0;
            }
        }
        fprintf(stderr, "\n\n";
        exit(0);
    }
    /* Flag that we've been here already */
    been_there_done_that = 1;

    /* Move the command line down a notch */
    /* Preserve pointers so setproctitle() works consistently */
    len = argv[argc] + strlen(argv[argc]) - argv[1];
    memmove(argv[0], argv[1], len);
    memset(argv[0] + len, 0, argv[1] - argv[0]);

    /* Fix up the argv pointers */
    len = argv[1] - argv[0];
    memmove(argv, argv + 1, sizeof(char *) * (argc + 1));
    for (i = 0; i < argc; i++)
        argv[i] -= len;

    return (main(argc, argv));
}

大家看最后一行,为什么非主函数可以调用主函数main,实在不理解。请高手告之,谢谢!

论坛徽章:
0
2 [报告]
发表于 2006-04-01 10:22 |只看该作者
什么函数都可以调用 main,mian 也可以调用 main,当 main 调用 mian 时,就叫做递归。

论坛徽章:
0
3 [报告]
发表于 2006-04-01 10:48 |只看该作者
谢谢!,确实可以调用,以前c学的不精:总以为非主函数不能调用主函数。呵呵

论坛徽章:
0
4 [报告]
发表于 2006-04-02 22:29 |只看该作者
在汇编角度来看是很正常的事,c编译器怎么处理程序入口的,
哪位大哥可以说得更详细一点
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP