LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
楼主: sybaselu

这几种函数有何区别?

[复制链接]
 楼主| 发表于 2004-7-30 13:24:46 | 显示全部楼层
1.busybox:www.busybox.net(做小系统用的linux shell必备命令), 他这样写必有他的巧妙之处!可是我找不出他们间的相互联系!而这点很重要!他们间必须相连!不妨下载看看!很经典!
2. extern的用法:在被调用的函数前:
              extern void function_1(parameter table){
               定义体
              }
                 在调用函数的编译单位中:
               extern void function_2(这里给不给参数?)
发表于 2004-7-30 13:29:52 | 显示全部楼层
最初由 sybaselu 发表
1.busybox:www.busybox.net(做小系统用的linux shell必备命令), 他这样写必有他的巧妙之处!可是我找不出他们间的相互联系!而这点很重要!他们间必须相连!不妨下载看看!很经典!
2. extern的用法:在被调用的函数前:
              extern void function_1(parameter table){
               定义体
              }
                 在调用函数的编译单位中:
               extern void function_2(这里给不给参数?)


1.busybox只是一个小型shell,用在嵌入式领域较多。关于main函数,请引用具体代码来讲解,这样会更有说服力的。
2.声明函数,它的参数列表只要给出类型即可,名字可以不给出。
 楼主| 发表于 2004-7-30 13:53:27 | 显示全部楼层
这是shell mv命令
/* vi: set sw=4 ts=4: */
/*
* Mini mkdir implementation for busybox
*
* Copyright (C) 2001 Matt Kraai <kraai@alumni.carnegiemellon.edu>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/

/* BB_AUDIT SUSv3 compliant */
/* http://www.opengroup.org/onlinep ... tilities/mkdir.html */

/* Mar 16, 2003      Manuel Novoa III   (mjn3@codepoet.org)
*
* Fixed broken permission setting when -p was used; especially in
* conjunction with -m.
*/


#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <stdlib.h>
#include <getopt.h>
#include "busybox.h"
#include "libcoreutils/coreutils.h"

static const struct option mv_long_options[] = {
        { "interactive", 0, NULL, 'i' },
        { "force", 0, NULL, 'f' },
        { 0, 0, 0, 0 }
};

#define OPT_FILEUTILS_FORCE       1
#define OPT_FILEUTILS_INTERACTIVE 2

static const char fmt[] = "cannot overwrite %sdirectory with %sdirectory";

extern int mv_main(int argc, char **argv)
{
        struct stat dest_stat;
        const char *last;
        const char *dest;
        unsigned long flags;
        int dest_exists;
        int status = 0;

        bb_applet_long_options = mv_long_options;
        bb_opt_complementaly = "f-i:i-f";
        flags = bb_getopt_ulflags(argc, argv, "fi");
        if (optind + 2 > argc) {
                bb_show_usage();
        }

        last = argv[argc - 1];
        argv += optind;

        if (optind + 2 == argc) {
                if ((dest_exists = cp_mv_stat(last, &dest_stat)) < 0) {
                        return 1;
                }

                if (!(dest_exists & 2)) {
                        dest = last;
                        goto DO_MOVE;
                }
        }

        do {
                dest = concat_path_file(last, bb_get_last_path_component(*argv));

                if ((dest_exists = cp_mv_stat(dest, &dest_stat)) < 0) {
                        goto RET_1;
                }

DO_MOVE:

                if (dest_exists && !(flags & OPT_FILEUTILS_FORCE) &&
                        ((access(dest, W_OK) < 0 && isatty(0)) ||
                        (flags & OPT_FILEUTILS_INTERACTIVE))) {
                        if (fprintf(stderr, "mv: overwrite `%s'? ", dest) < 0) {
                                goto RET_1;        /* Ouch! fprintf failed! */
                        }
                        if (!bb_ask_confirmation()) {
                                goto RET_0;
                        }
                }
                if (rename(*argv, dest) < 0) {
                        struct stat source_stat;
                        int source_exists;

                        if (errno != EXDEV) {
                                bb_perror_msg("unable to rename `%s'", *argv);
                        }
                        else if ((source_exists = cp_mv_stat(*argv, &source_stat)) >= 0) {
                                if (dest_exists) {
                                        if (dest_exists == 3) {
                                                if (source_exists != 3) {
                                                        bb_error_msg(fmt, "", "non-");
                                                        goto RET_1;
                                                }
                                        } else {
                                                if (source_exists == 3) {
                                                        bb_error_msg(fmt, "non-", "");
                                                        goto RET_1;
                                                }
                                        }
                                        if (unlink(dest) < 0) {
                                                bb_perror_msg("cannot remove `%s'", dest);
                                                goto RET_1;
                                        }
                                }
                                if ((copy_file(*argv, dest,
                                        FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS) >= 0) &&
                                        (remove_file(*argv, FILEUTILS_RECUR | FILEUTILS_FORCE) >= 0)) {
                                        goto RET_0;
                                }
                        }
RET_1:
                        status = 1;
                }
RET_0:
                if (dest != last) {
                        free((void *) dest);
                }
        } while (*++argv != last);

        return (status);
}
这是shell rm 命令


#include <unistd.h>
#include "busybox.h"

extern int rm_main(int argc, char **argv)
{
        int status = 0;
        int flags = 0;
        unsigned long opt;

        bb_opt_complementaly = "f-i:i-f";
        opt = bb_getopt_ulflags(argc, argv, "fiRr");
        if(opt & 1)
                                flags |= FILEUTILS_FORCE;
        if(opt & 2)
                flags |= FILEUTILS_INTERACTIVE;
        if(opt & 12)
                flags |= FILEUTILS_RECUR;

        if (*(argv += optind) != NULL) {
                do {
                        const char *base = bb_get_last_path_component(*argv);

                        if ((base[0] == '.') && (!base[1] || ((base[1] == '.') && !base[2]))) {
                                bb_error_msg("cannot remove `.' or `..'");
                        } else if (remove_file(*argv, flags) >= 0) {
                                continue;
                        }
                        status = 1;
                } while (*++argv);
        } else if (!(flags & FILEUTILS_FORCE)) {
                bb_show_usage();
        }

        return status;
}

这是shell rmdir命令:

#include <stdlib.h>
#include <unistd.h>
#include <libgen.h>
#include "busybox.h"

extern int rmdir_main(int argc, char **argv)
{
        int status = EXIT_SUCCESS;
        int flags;
        int do_dot;
        char *path;

        flags = bb_getopt_ulflags(argc, argv, "p");

        argv += optind;

        if (!*argv) {
                bb_show_usage();
        }

        do {
                path = *argv;

                /* Record if the first char was a '.' so we can use dirname later. */
                do_dot = (*path == '.');

                do {
                        if (rmdir(path) < 0) {
                                bb_perror_msg("`%s'", path);        /* Match gnu rmdir msg. */
                                status = EXIT_FAILURE;
                        } else if (flags) {
                                /* Note: path was not empty or null since rmdir succeeded. */
                                path = dirname(path);
                                /* Path is now just the parent component.  Note that dirname
                                 * returns "." if there are no parents.  We must distinguish
                                 * this from the case of the original path starting with '.'.
                 */
                                if (do_dot || (*path != '.') || path[1]) {
                                        continue;
                                }
                        }
                        break;
                } while (1);

        } while (*++argv);

        return status;
}
这是shell mkdir 命令:

#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include "busybox.h"

static const struct option mkdir_long_options[] = {
        { "mode", 1, NULL, 'm' },
        { "parents", 0, NULL, 'p' },
        { 0, 0, 0, 0 }
};

extern int mkdir_main (int argc, char **argv)
{
        mode_t mode = (mode_t)(-1);
        int status = EXIT_SUCCESS;
        int flags = 0;
        unsigned long opt;
        char *smode;

        bb_applet_long_options = mkdir_long_options;
        opt = bb_getopt_ulflags(argc, argv, "m:p", &smode);
        if(opt & 1) {
                        mode = 0777;
                if (!bb_parse_mode (smode, &mode)) {
                        bb_error_msg_and_die ("invalid mode `%s'", smode);
                }
        }
        if(opt & 2)
                flags |= FILEUTILS_RECUR;

        if (optind == argc) {
                bb_show_usage();
        }

        argv += optind;

        do {
                if (bb_make_directory(*argv, mode, flags)) {
                        status = EXIT_FAILURE;
                }
        } while (*++argv);

        return status;
}
 楼主| 发表于 2004-7-30 13:59:59 | 显示全部楼层
这里是busybox的include 目录下的头文件applets.h!
/*
* applets.h - a listing of all busybox applets.
*
* If you write a new applet, you need to add an entry to this list to make
* busybox aware of it.
*
* It is CRUCIAL that this listing be kept in ascii order, otherwise the binary
* search lookup contributed by Gaute B Strokkenes stops working. If you value
* your kneecaps, you'll be sure to *make sure* that any changes made to this
* file result in the listing remaining in ascii order. You have been warned.
*/

#undef APPLET
#undef APPLET_ODDNAME
#undef APPLET_NOUSAGE


#if defined(PROTOTYPES)
  #define APPLET(a,b,c,d) extern int b(int argc, char **argv);
  #define APPLET_NOUSAGE(a,b,c,d) extern int b(int argc, char **argv);
  #define APPLET_ODDNAME(a,b,c,d,e) extern int b(int argc, char **argv);
  extern const char usage_messages[];
#elif defined(MAKE_USAGE)
  #ifdef CONFIG_FEATURE_VERBOSE_USAGE
    #define APPLET(a,b,c,d) a##_trivial_usage "\n\n" a##_full_usage "\0"
    #define APPLET_NOUSAGE(a,b,c,d) "\b\0"
    #define APPLET_ODDNAME(a,b,c,d,e) e##_trivial_usage "\n\n" e##_full_usage "\0"
  #else
    #define APPLET(a,b,c,d) a##_trivial_usage "\0"
    #define APPLET_NOUSAGE(a,b,c,d) "\b\0"
    #define APPLET_ODDNAME(a,b,c,d,e) e##_trivial_usage "\0"
  #endif
#elif defined(MAKE_LINKS)
#  define APPLET(a,b,c,d) LINK c a
#  define APPLET_NOUSAGE(a,b,c,d) LINK c a
#  define APPLET_ODDNAME(a,b,c,d,e) LINK c a
#else
  const struct BB_applet applets[] = {
  #define APPLET(a,b,c,d) {#a,b,c,d},
  #define APPLET_NOUSAGE(a,b,c,d) {a,b,c,d},
  #define APPLET_ODDNAME(a,b,c,d,e) {a,b,c,d},
#endif

#ifdef CONFIG_INSTALL_NO_USR
#define _BB_DIR_USR_BIN _BB_DIR_BIN
#define _BB_DIR_USR_SBIN _BB_DIR_SBIN
#endif



#ifdef CONFIG_TEST
        APPLET_NOUSAGE("[", test_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_ADDGROUP
        APPLET(addgroup, addgroup_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_ADDUSER
        APPLET(adduser, adduser_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_ADJTIMEX
        APPLET(adjtimex, adjtimex_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_AR
        APPLET(ar, ar_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_ARPING
        APPLET(arping, arping_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_ASH
        APPLET_NOUSAGE("ash", ash_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_AWK
        APPLET(awk, awk_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_BASENAME
        APPLET(basename, basename_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_BUNZIP2
        APPLET(bunzip2, bunzip2_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
        APPLET_NOUSAGE("busybox", busybox_main, _BB_DIR_BIN, _BB_SUID_MAYBE)
#ifdef CONFIG_BUNZIP2
        APPLET(bzcat, bunzip2_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_CAL
        APPLET(cal, cal_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_CAT
        APPLET(cat, cat_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_CHGRP
        APPLET(chgrp, chgrp_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_CHMOD
        APPLET(chmod, chmod_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_CHOWN
        APPLET(chown, chown_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_CHROOT
        APPLET(chroot, chroot_main, _BB_DIR_USR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_CHVT
        APPLET(chvt, chvt_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_CLEAR
        APPLET(clear, clear_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_CMP
        APPLET(cmp, cmp_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_CP
        APPLET(cp, cp_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_CPIO
        APPLET(cpio, cpio_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_CROND
        APPLET(crond, crond_main, _BB_DIR_USR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_CRONTAB
        APPLET(crontab, crontab_main, _BB_DIR_USR_BIN, _BB_SUID_ALWAYS)
#endif
#ifdef CONFIG_CUT
        APPLET(cut, cut_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_DATE
        APPLET(date, date_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_DC
        APPLET(dc, dc_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_DD
        APPLET(dd, dd_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_DEALLOCVT
        APPLET(deallocvt, deallocvt_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_DELGROUP
        APPLET(delgroup, delgroup_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_DELUSER
        APPLET(deluser, deluser_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_DEVFSD
        APPLET(devfsd, devfsd_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_DF
        APPLET(df, df_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_DIRNAME
        APPLET(dirname, dirname_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_DMESG
        APPLET(dmesg, dmesg_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_DOS2UNIX
        APPLET(dos2unix, dos2unix_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_DPKG
        APPLET(dpkg, dpkg_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_DPKG_DEB
        APPLET_ODDNAME("dpkg-deb", dpkg_deb_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER, dpkg_deb)
#endif
#ifdef CONFIG_DU
        APPLET(du, du_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_DUMPKMAP
        APPLET(dumpkmap, dumpkmap_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_DUMPLEASES
        APPLET(dumpleases, dumpleases_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_ECHO
        APPLET(echo, echo_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#if defined(CONFIG_FEATURE_GREP_EGREP_ALIAS)
        APPLET_NOUSAGE("egrep", grep_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_ENV
        APPLET(env, env_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_EXPR
        APPLET(expr, expr_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_FALSE
        APPLET(false, false_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_FBSET
        APPLET(fbset, fbset_main, _BB_DIR_USR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_FDFLUSH
        APPLET(fdflush, fdflush_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_FDFORMAT
        APPLET(fdformat, fdformat_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_FDISK
        APPLET(fdisk, fdisk_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#if defined(CONFIG_FEATURE_GREP_FGREP_ALIAS)
        APPLET_NOUSAGE("fgrep", grep_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_FIND
        APPLET(find, find_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_FOLD
        APPLET(fold, fold_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_FREE
        APPLET(free, free_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_FREERAMDISK
        APPLET(freeramdisk, freeramdisk_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_FSCK_MINIX
        APPLET_ODDNAME("fsck.minix", fsck_minix_main, _BB_DIR_SBIN, _BB_SUID_NEVER, fsck_minix)
#endif
#ifdef CONFIG_FTPGET
        APPLET(ftpget, ftpgetput_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_FTPPUT
        APPLET(ftpput, ftpgetput_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_GETOPT
        APPLET(getopt, getopt_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_GETTY
        APPLET(getty, getty_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_GREP
        APPLET(grep, grep_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_GUNZIP
        APPLET(gunzip, gunzip_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_GZIP
        APPLET(gzip, gzip_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_HALT
        APPLET(halt, halt_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_HDPARM
        APPLET(hdparm, hdparm_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_HEAD
        APPLET(head, head_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_HEXDUMP
        APPLET(hexdump, hexdump_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_HOSTID
        APPLET(hostid, hostid_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_HOSTNAME
        APPLET(hostname, hostname_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_HTTPD
        APPLET(httpd, httpd_main, _BB_DIR_USR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_HUSH
        APPLET_NOUSAGE("hush", hush_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_HWCLOCK
        APPLET(hwclock, hwclock_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_ID
        APPLET(id, id_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_IFCONFIG
        APPLET(ifconfig, ifconfig_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_IFUPDOWN
        APPLET(ifdown, ifupdown_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_IFUPDOWN
        APPLET(ifup, ifupdown_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_INETD
        APPLET(inetd, inetd_main, _BB_DIR_USR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_INIT
        APPLET(init, init_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_INSMOD
        APPLET(insmod, insmod_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_INSTALL
        APPLET(install, install_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_IP
        APPLET(ip, ip_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_IPADDR
        APPLET(ipaddr, ipaddr_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_IPCALC
        APPLET(ipcalc, ipcalc_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_IPLINK
        APPLET(iplink, iplink_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_IPROUTE
        APPLET(iproute, iproute_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_IPTUNNEL
        APPLET(iptunnel, iptunnel_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_KILL
        APPLET(kill, kill_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_KILLALL
        APPLET(killall, kill_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_KLOGD
        APPLET(klogd, klogd_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_LASH
        APPLET(lash, lash_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_LAST
        APPLET(last, last_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_LENGTH
        APPLET(length, length_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_FEATURE_INITRD
        APPLET_NOUSAGE("linuxrc", init_main, _BB_DIR_ROOT, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_LN
        APPLET(ln, ln_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_LOADFONT
        APPLET(loadfont, loadfont_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_LOADKMAP
        APPLET(loadkmap, loadkmap_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_LOGGER
        APPLET(logger, logger_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_LOGIN
        APPLET(login, login_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_LOGNAME
        APPLET(logname, logname_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_LOGREAD
        APPLET(logread, logread_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_LOSETUP
        APPLET(losetup, losetup_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_LS
        APPLET(ls, ls_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_LSMOD
        APPLET(lsmod, lsmod_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_MAKEDEVS
        APPLET(makedevs, makedevs_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_MD5SUM
        APPLET(md5sum, md5sum_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_MESG
        APPLET(mesg, mesg_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_MKDIR
        APPLET(mkdir, mkdir_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_MKFIFO
        APPLET(mkfifo, mkfifo_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_MKFS_MINIX
        APPLET_ODDNAME("mkfs.minix", mkfs_minix_main, _BB_DIR_SBIN, _BB_SUID_NEVER, mkfs_minix)
#endif
#ifdef CONFIG_MKNOD
        APPLET(mknod, mknod_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_MKSWAP
        APPLET(mkswap, mkswap_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_MKTEMP
        APPLET(mktemp, mktemp_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_MODPROBE
        APPLET(modprobe, modprobe_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_MORE
        APPLET(more, more_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_MOUNT
        APPLET(mount, mount_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_MSH
        APPLET_NOUSAGE("msh", msh_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_MT
        APPLET(mt, mt_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_MV
        APPLET(mv, mv_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_NAMEIF
        APPLET(nameif, nameif_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_NC
        APPLET(nc, nc_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_NETSTAT
        APPLET(netstat, netstat_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_NSLOOKUP
        APPLET(nslookup, nslookup_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_OD
        APPLET(od, od_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_OPENVT
        APPLET(openvt, openvt_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_PASSWD
        APPLET(passwd, passwd_main, _BB_DIR_USR_BIN, _BB_SUID_ALWAYS)
#endif
#ifdef CONFIG_PATCH
        APPLET(patch, patch_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_PIDOF
        APPLET(pidof, pidof_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_PING
        APPLET(ping, ping_main, _BB_DIR_BIN, _BB_SUID_MAYBE)
#endif
#ifdef CONFIG_PING6
        APPLET(ping6, ping6_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_PIPE_PROGRESS
        APPLET_NOUSAGE("pipe_progress", pipe_progress_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_PIVOT_ROOT
        APPLET(pivot_root, pivot_root_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_POWEROFF
        APPLET(poweroff, poweroff_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_PRINTF
        APPLET(printf, printf_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_PS
        APPLET(ps, ps_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_PWD
        APPLET(pwd, pwd_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_RDATE
        APPLET(rdate, rdate_main, _BB_DIR_USR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_READLINK
        APPLET(readlink, readlink_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_REALPATH
        APPLET(realpath, realpath_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_REBOOT
        APPLET(reboot, reboot_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_RENICE
        APPLET(renice, renice_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_RESET
        APPLET(reset, reset_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_RM
        APPLET(rm, rm_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_RMDIR
        APPLET(rmdir, rmdir_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_RMMOD
        APPLET(rmmod, rmmod_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_ROUTE
        APPLET(route, route_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_RPM
        APPLET(rpm, rpm_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_RPM2CPIO
        APPLET(rpm2cpio, rpm2cpio_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_RUN_PARTS
        APPLET_ODDNAME("run-parts", run_parts_main, _BB_DIR_BIN, _BB_SUID_NEVER, run_parts)
#endif
#ifdef CONFIG_RX
        APPLET(rx, rx_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_SED
        APPLET(sed, sed_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_SEQ
        APPLET(seq, seq_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_SETKEYCODES
        APPLET(setkeycodes, setkeycodes_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#if defined(CONFIG_FEATURE_SH_IS_ASH) && defined(CONFIG_ASH)
        APPLET_NOUSAGE("sh", ash_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#elif defined(CONFIG_FEATURE_SH_IS_HUSH) && defined(CONFIG_HUSH)
        APPLET_NOUSAGE("sh", hush_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#elif defined(CONFIG_FEATURE_SH_IS_LASH) && defined(CONFIG_LASH)
        APPLET_NOUSAGE("sh", lash_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#elif defined(CONFIG_FEATURE_SH_IS_MSH) && defined(CONFIG_MSH)
        APPLET_NOUSAGE("sh", msh_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_SHA1SUM
        APPLET(sha1sum, sha1sum_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_SLEEP
        APPLET(sleep, sleep_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_SORT
        APPLET(sort, sort_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_START_STOP_DAEMON
    APPLET_ODDNAME("start-stop-daemon", start_stop_daemon_main, _BB_DIR_SBIN, _BB_SUID_NEVER, start_stop_daemon)
#endif
#ifdef CONFIG_STRINGS
        APPLET(strings, strings_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_STTY
        APPLET(stty, stty_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_SU
        APPLET(su, su_main, _BB_DIR_BIN, _BB_SUID_ALWAYS)
#endif
#ifdef CONFIG_SULOGIN
        APPLET(sulogin, sulogin_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_SWAPONOFF
        APPLET(swapoff, swap_on_off_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_SWAPONOFF
        APPLET(swapon, swap_on_off_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_SYNC
        APPLET(sync, sync_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_SYSCTL
        APPLET(sysctl, sysctl_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_SYSLOGD
        APPLET(syslogd, syslogd_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_TAIL
        APPLET(tail, tail_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_TAR
        APPLET(tar, tar_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_TEE
        APPLET(tee, tee_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_TELNET
        APPLET(telnet, telnet_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_TELNETD
        APPLET(telnetd, telnetd_main, _BB_DIR_USR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_TEST
        APPLET(test, test_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_TFTP
        APPLET(tftp, tftp_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_TIME
        APPLET(time, time_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_TOP
        APPLET(top, top_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_TOUCH
        APPLET(touch, touch_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_TR
        APPLET(tr, tr_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_TRACEROUTE
        APPLET(traceroute, traceroute_main, _BB_DIR_USR_BIN, _BB_SUID_MAYBE)
#endif
#ifdef CONFIG_TRUE
        APPLET(true, true_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_TTY
        APPLET(tty, tty_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_UDHCPC
        APPLET(udhcpc, udhcpc_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_UDHCPD
        APPLET(udhcpd, udhcpd_main, _BB_DIR_USR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_UMOUNT
        APPLET(umount, umount_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_UNAME
        APPLET(uname, uname_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_UNCOMPRESS
        APPLET(uncompress, uncompress_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_UNIQ
        APPLET(uniq, uniq_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_UNIX2DOS
        APPLET(unix2dos, dos2unix_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_UNZIP
        APPLET(unzip, unzip_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_UPTIME
        APPLET(uptime, uptime_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_USLEEP
        APPLET(usleep, usleep_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_UUDECODE
        APPLET(uudecode, uudecode_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_UUENCODE
        APPLET(uuencode, uuencode_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_VCONFIG
        APPLET(vconfig, vconfig_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_VI
        APPLET(vi, vi_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_VLOCK
        APPLET(vlock, vlock_main, _BB_DIR_USR_BIN, _BB_SUID_ALWAYS)
#endif
#ifdef CONFIG_WATCH
        APPLET(watch, watch_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_WATCHDOG
        APPLET(watchdog, watchdog_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_WC
        APPLET(wc, wc_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_WGET
        APPLET(wget, wget_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_WHICH
        APPLET(which, which_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_WHO
        APPLET(who, who_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_WHOAMI
        APPLET(whoami, whoami_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_XARGS
        APPLET(xargs, xargs_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_YES
        APPLET(yes, yes_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
#ifdef CONFIG_GUNZIP
        APPLET(zcat, gunzip_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif

#if !defined(PROTOTYPES) && !defined(MAKE_USAGE)
        { 0,NULL,0 }
};

#endif
这里是busybox的include 目录下的头文件busybox.h!
#ifndef        _BB_INTERNAL_H_
#define        _BB_INTERNAL_H_    1

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/stat.h>
#include <sys/types.h>

#if __GNU_LIBRARY__ < 5
#ifndef __dietlibc__
#error "Sorry, libc5 is not supported"
#endif
#endif

#ifndef BB_EXTRA_VERSION
#define BB_BANNER "BusyBox v" BB_VER " (" BB_BT ")"
#else
#define BB_BANNER "BusyBox v" BB_VER " (" BB_EXTRA_VERSION ")"
#endif

#ifdef DMALLOC
#include <dmalloc.h>
#endif

#include <features.h>

/* Pull in the utility routines from libbb */
#include "libbb.h"

enum Location {
        _BB_DIR_ROOT = 0,
        _BB_DIR_BIN,
        _BB_DIR_SBIN,
        _BB_DIR_USR_BIN,
        _BB_DIR_USR_SBIN
};

enum SUIDRoot {
        _BB_SUID_NEVER = 0,
        _BB_SUID_MAYBE,
        _BB_SUID_ALWAYS
};

struct BB_applet {
        const char *name;
        int (*main) (int argc, char **argv);
        enum Location location:4;
        enum SUIDRoot need_suid:4;
};

/* From busybox.c */
extern const struct BB_applet applets[];

/* Automagically pull in all the applet function prototypes and
* applet usage strings.  These are all of the form:
*                extern int foo_main(int argc, char **argv);
*                extern const char foo_usage[];
* These are all autogenerated from the set of currently defined applets.
*/
#define PROTOTYPES
#include "applets.h"
#undef PROTOTYPES

#ifdef CONFIG_FEATURE_BUFFERS_GO_ON_STACK
#define RESERVE_CONFIG_BUFFER(buffer,len)           char buffer[len]
#define RESERVE_CONFIG_UBUFFER(buffer,len) unsigned char buffer[len]
#define RELEASE_CONFIG_BUFFER(buffer)      ((void)0)
#else
#ifdef CONFIG_FEATURE_BUFFERS_GO_IN_BSS
#define RESERVE_CONFIG_BUFFER(buffer,len)  static          char buffer[len]
#define RESERVE_CONFIG_UBUFFER(buffer,len) static unsigned char buffer[len]
#define RELEASE_CONFIG_BUFFER(buffer)      ((void)0)
#else
#define RESERVE_CONFIG_BUFFER(buffer,len)           char *buffer=xmalloc(len)
#define RESERVE_CONFIG_UBUFFER(buffer,len) unsigned char *buffer=xmalloc(len)
#define RELEASE_CONFIG_BUFFER(buffer)      free (buffer)
#endif
#endif


#ifndef RB_POWER_OFF
/* Stop system and switch power off if possible.  */
#define RB_POWER_OFF   0x4321fedc
#endif

/* Try to pull in PATH_MAX */
#include <limits.h>

/* for PATH_MAX on systems that don't have it in limits.h */
#include <sys/param.h>
#ifndef PATH_MAX
#define  PATH_MAX         256
#endif

#endif                                                        /* _BB_INTERNAL_H_ */



还有几个头文件不重要,不列出来!
发表于 2004-7-30 14:04:44 | 显示全部楼层
不必所有都贴出来。截取精选即可。
还有,必须加以注释,加以缩进。

那这样说明了什么呢?我觉得楼主表述的意思很不明确。往往问题是越问越离题,就一事论一事,说白了才议另一事,这样的议论才有效率嘛。
 楼主| 发表于 2004-7-30 14:45:15 | 显示全部楼层
不好意思!惭愧!惭愧!。。。。。。。。。。。
我再整理一便, 将shell command的头文件保持, 去掉xxx_main()实现代码,只保留函数原型!
这是shell mv命令


#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <stdlib.h>
#include <getopt.h>
#include "busybox.h"
#include "libcoreutils/coreutils.h"

static const struct option mv_long_options[] = {
        { "interactive", 0, NULL, 'i' },
        { "force", 0, NULL, 'f' },
        { 0, 0, 0, 0 }
};

#define OPT_FILEUTILS_FORCE       1
#define OPT_FILEUTILS_INTERACTIVE 2

static const char fmt[] = "cannot overwrite %sdirectory with %sdirectory";

extern int mv_main(int argc, char **argv)
{
       
}


这是shell rm 命令


#include <unistd.h>
#include "busybox.h"

extern int rm_main(int argc, char **argv)
{
       
       
}

这是shell rmdir命令:

#include <stdlib.h>
#include <unistd.h>
#include <libgen.h>
#include "busybox.h"

extern int rmdir_main(int argc, char **argv)
{
       
       
}
这是shell mkdir 命令:

#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include "busybox.h"

static const struct option mkdir_long_options[] = {
        { "mode", 1, NULL, 'm' },
        { "parents", 0, NULL, 'p' },
        { 0, 0, 0, 0 }
};

extern int mkdir_main (int argc, char **argv)
{
       
}
 楼主| 发表于 2004-7-30 14:59:19 | 显示全部楼层
下面我在将busybox中的include下面的两个主要的头文件列出!
这里是busybox的include 目录下的头文件applets.h!


#undef APPLET
#undef APPLET_ODDNAME
#undef APPLET_NOUSAGE
#if defined(PROTOTYPES)
  #define APPLET(a,b,c,d) extern int b(int argc, char **argv);
  #define APPLET_NOUSAGE(a,b,c,d) extern int b(int argc, char **argv);
  #define APPLET_ODDNAME(a,b,c,d,e) extern int b(int argc, char **argv);
  extern const char usage_messages[];
#elif defined(MAKE_USAGE)
  #ifdef CONFIG_FEATURE_VERBOSE_USAGE
    #define APPLET(a,b,c,d) a##_trivial_usage "\n\n" a##_full_usage "\0"
    #define APPLET_NOUSAGE(a,b,c,d) "\b\0"
    #define APPLET_ODDNAME(a,b,c,d,e) e##_trivial_usage "\n\n" e##_full_usage "\0"
  #else
    #define APPLET(a,b,c,d) a##_trivial_usage "\0"
    #define APPLET_NOUSAGE(a,b,c,d) "\b\0"
    #define APPLET_ODDNAME(a,b,c,d,e) e##_trivial_usage "\0"
  #endif
#elif defined(MAKE_LINKS)
#  define APPLET(a,b,c,d) LINK c a
#  define APPLET_NOUSAGE(a,b,c,d) LINK c a
#  define APPLET_ODDNAME(a,b,c,d,e) LINK c a
#else
  const struct BB_applet applets[] = {
  #define APPLET(a,b,c,d) {#a,b,c,d},
  #define APPLET_NOUSAGE(a,b,c,d) {a,b,c,d},
  #define APPLET_ODDNAME(a,b,c,d,e) {a,b,c,d},
#endif

#ifdef CONFIG_INSTALL_NO_USR
#define _BB_DIR_USR_BIN _BB_DIR_BIN
#define _BB_DIR_USR_SBIN _BB_DIR_SBIN
#endif
...................
...................
...................
#ifdef CONFIG_MKDIR
        APPLET(mkdir, mkdir_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif


#ifdef CONFIG_MV
        APPLET(mv, mv_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif



#ifdef CONFIG_RM
        APPLET(rm, rm_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif


#ifdef CONFIG_RMDIR
        APPLET(rmdir, rmdir_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
..................
..................
..................
(只列出几个上面我贴出来的shell command相关的东东!)


这里是busybox的include 目录下的头文件busybox.h!
#ifndef        _BB_INTERNAL_H_
#define        _BB_INTERNAL_H_    1

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/stat.h>
#include <sys/types.h>

#if __GNU_LIBRARY__ < 5
#ifndef __dietlibc__
#error "Sorry, libc5 is not supported"
#endif
#endif

#ifndef BB_EXTRA_VERSION
#define BB_BANNER "BusyBox v" BB_VER " (" BB_BT ")"
#else
#define BB_BANNER "BusyBox v" BB_VER " (" BB_EXTRA_VERSION ")"
#endif

#ifdef DMALLOC
#include <dmalloc.h>
#endif

#include <features.h>

/* Pull in the utility routines from libbb */
#include "libbb.h"



/* From busybox.c */
extern const struct BB_applet applets[];

/* Automagically pull in all the applet function prototypes and
* applet usage strings.  These are all of the form:
*                extern int foo_main(int argc, char **argv);
*                extern const char foo_usage[];
* These are all autogenerated from the set of currently defined applets.
*/
#define PROTOTYPES
#include "applets.h"
#undef PROTOTYPES

#ifdef CONFIG_FEATURE_BUFFERS_GO_ON_STACK
#define RESERVE_CONFIG_BUFFER(buffer,len)           char buffer[len]
#define RESERVE_CONFIG_UBUFFER(buffer,len) unsigned char buffer[len]
#define RELEASE_CONFIG_BUFFER(buffer)      ((void)0)
#else
#ifdef CONFIG_FEATURE_BUFFERS_GO_IN_BSS
#define RESERVE_CONFIG_BUFFER(buffer,len)  static          char buffer[len]
#define RESERVE_CONFIG_UBUFFER(buffer,len) static unsigned char buffer[len]
#define RELEASE_CONFIG_BUFFER(buffer)      ((void)0)
#else
#define RESERVE_CONFIG_BUFFER(buffer,len)           char *buffer=xmalloc(len)
#define RESERVE_CONFIG_UBUFFER(buffer,len) unsigned char *buffer=xmalloc(len)
#define RELEASE_CONFIG_BUFFER(buffer)      free (buffer)
#endif
#endif


#ifndef RB_POWER_OFF
/* Stop system and switch power off if possible.  */
#define RB_POWER_OFF   0x4321fedc
#endif

/* Try to pull in PATH_MAX */
#include <limits.h>

/* for PATH_MAX on systems that don't have it in limits.h */
#include <sys/param.h>
#ifndef PATH_MAX
#define  PATH_MAX         256
#endif

#endif                                                        /* _BB_INTERNAL_H_ */



还有几个头文件不重要,不列出来!
1:在shell command的源码中,只有xxx_main(),如果只是这样那这些shell command 肯定是不能用的, 他必定于int main()有关!对吧?他如何于int main()发生关系呢?可能于/include/applet.h和busybox.h有关!
2:而且这里的xxx_main()都被作extern用,一定在那里被其他的函数调用!
发表于 2004-7-30 15:02:55 | 显示全部楼层
我没看过完整的源码,大胆猜测一下
xxx_main 可能被作为函数指针在程序的某处被引用
 楼主| 发表于 2004-7-30 15:43:53 | 显示全部楼层
是不是类似下面的一样,如果是那我趋向楼主说的!可是我看过源码,好像没有这样的函数指针!
typedef struct file_operation{
int (*seek)(struct inode *, struct file *)
int (*open)(struct inode *, struct file *)
int (*close)(struct inode *, struct file *)
int (*ioctl)(struct inode *, struct file *)
int (*write)(struct inode *, struct file *)
int (*read)(struct inode *, struct file *)
int (*fnasyc)(struct inode *, struct file *)
}
sturct file_operation xxxx={
       .open=     void_open();
       .read=     void_read();
       .write=    void_wirte();
         ................
}
楼主能否再大胆推测一下applets.h的头文件的作用?
发表于 2004-7-30 16:28:46 | 显示全部楼层
busybox是applet机制的命令引擎,根据输入执行命令(包括内部以及若干外部命令)。

xxx_main是某个外部命令的执行入口,只是函数而已。而busybox的main统领这些xxx_main。


  1. [color=green]/*applet/busybox.c*/[/color]
  2. [color=red][b]int main(int argc, char **argv)[/b][/color]
  3. {
  4.         const char *s;

  5.         bb_applet_name = argv[0];

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

  8.         for (s = bb_applet_name; *s != '\0';) {
  9.                 if (*s++ == '/')
  10.                         bb_applet_name = s;
  11.         }

  12. #ifdef CONFIG_LOCALE_SUPPORT
  13. #ifdef CONFIG_INIT
  14.         if(getpid()!=1)        /* Do not set locale for `init' */
  15. #endif
  16.         {
  17.                 setlocale(LC_ALL, "");
  18.         }
  19. #endif

  20.         [color=red][b]run_applet_by_name(bb_applet_name, argc, argv);[/b]][/color]
  21.         bb_error_msg_and_die("applet not found");
  22. }
复制代码

这里才是main!

  1. void
  2. run_applet_by_name (const char *name, int argc, char **argv)
  3. {
  4.       ...
  5.         [color=red][b]exit ((*(applet_using->main)) (argc, argv));[/b]][/color]
  6.       ...
  7. }
复制代码

调用main,相当于调用xxx_main!见下!

  1. find_applet_by_name (const char *name)
  2. {
  3.   return bsearch (name, applets, NUM_APPLETS, sizeof (struct BB_applet),
  4.                                   applet_name_compare);
  5. }
复制代码

这里的bsearch是快速查找,在applets数组中查找命令。


  1. #if defined(PROTOTYPES)
  2.   #define APPLET(a,b,c,d) extern int b(int argc, char **argv);
  3.   #define APPLET_NOUSAGE(a,b,c,d) extern int b(int argc, char **argv);
  4.   #define APPLET_ODDNAME(a,b,c,d,e) extern int b(int argc, char **argv);
  5.   extern const char usage_messages[];
  6. #elif defined(MAKE_USAGE)
  7.   #ifdef CONFIG_FEATURE_VERBOSE_USAGE
  8.     #define APPLET(a,b,c,d) a##_trivial_usage "\n\n" a##_full_usage "\0"
  9.     #define APPLET_NOUSAGE(a,b,c,d) "\b\0"
  10.     #define APPLET_ODDNAME(a,b,c,d,e) e##_trivial_usage "\n\n" e##_full_usage "\0"
  11.   #else
  12.     #define APPLET(a,b,c,d) a##_trivial_usage "\0"
  13.     #define APPLET_NOUSAGE(a,b,c,d) "\b\0"
  14.     #define APPLET_ODDNAME(a,b,c,d,e) e##_trivial_usage "\0"
  15.   #endif
  16. #elif defined(MAKE_LINKS)
  17. #  define APPLET(a,b,c,d) LINK c a
  18. #  define APPLET_NOUSAGE(a,b,c,d) LINK c a
  19. #  define APPLET_ODDNAME(a,b,c,d,e) LINK c a
  20. #else
  21.   [color=red][b]const struct BB_applet applets[] = {
  22.   #define APPLET(a,b,c,d) {#a,b,c,d},
  23.   #define APPLET_NOUSAGE(a,b,c,d) {a,b,c,d},
  24.   #define APPLET_ODDNAME(a,b,c,d,e) {a,b,c,d},
  25. #endif[/b][/color]
  26. ...
  27. [color=red][b]#ifdef CONFIG_ADDGROUP
  28.         APPLET(addgroup, addgroup_main, _BB_DIR_BIN, _BB_SUID_NEVER)
  29. #endif[/b][/color]
  30. ...
复制代码

通过宏把命令的源码文件的xxx_main入口赋值为applets数组的main成员!

我分析得很清晰,在我的脑海里,这个busybox的框架很清晰,不知道sybaselu感觉如何?我希望你能好好体会,我花了10分钟左右就看出了这些关系。
无论多大一个项目,它只有一个main!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表