改进busybox查找命令和执行命令对应函数的方法
不知道为什么当初busybox在对cmd的字符串描述和cmd的执行函数非要分开来做,可能他们对面向对象的封装特性不是很熟悉,个人感觉,使用如下方法对cmd字符串和执行函数进行如下封装之后,效果会更加直观和易于维护[luther.gliethttp].
#include <stdlib.h>
#include <stdio.h>
#define ARRAY_SIZE(x) ((unsigned)(sizeof(x) / sizeof((x)[0])))
int ls_main(int argc, char **argv);
int pwd_main(int argc, char **argv);
struct __applet_st
{
const char *name;
int (*function)(int argc, char **argv);
};
//如下大段可以放在一个独立的文件中,比如 applets_container.c中,然后
//将该文件放在这里,直接展开即可,于是会更加简洁,事例如下:
struct __applet_st applets_container[] = {
{"[" , NULL},
{"[[" , NULL},
{"addgroup" , NULL},
{"adduser" , NULL},
{"adjtimex" , NULL},
{"ar" , NULL},
{"arp" , NULL},
{"arping" , NULL},
{"ash" , NULL},
{"awk" , NULL},
{"basename" , NULL},
{"brctl" , NULL},
{"bunzip2" , NULL},
{"bzcat" , NULL},
{"bzip2" , NULL},
{"cal" , NULL},
{"cat" , NULL},
{"catv" , NULL},
{"chat" , NULL},
{"chattr" , NULL},
{"chgrp" , NULL},
{"chmod" , NULL},
{"chown" , NULL},
{"chpasswd" , NULL},
{"chpst" , NULL},
{"chroot" , NULL},
{"chrt" , NULL},
{"chvt" , NULL},
{"cksum" , NULL},
{"clear" , NULL},
{"cmp" , NULL},
{"comm" , NULL},
{"cp" , NULL},
{"cpio" , NULL},
{"crond" , NULL},
{"crontab" , NULL},
{"cryptpw" , NULL},
{"cut" , NULL},
{"date" , NULL},
{"dc" , NULL},
{"dd" , NULL},
{"deallocvt" , NULL},
{"delgroup" , NULL},
{"deluser" , NULL},
{"depmod" , NULL},
{"df" , NULL},
{"dhcprelay" , NULL},
{"diff" , NULL},
{"dirname" , NULL},
{"dmesg" , NULL},
{"dnsd" , NULL},
{"dos2unix" , NULL},
{"du" , NULL},
{"dumpkmap" , NULL},
{"dumpleases" , NULL},
{"echo" , NULL},
{"ed" , NULL},
{"egrep" , NULL},
{"eject" , NULL},
{"env" , NULL},
{"envdir" , NULL},
{"envuidgid" , NULL},
{"ether-wake" , NULL},
{"expand" , NULL},
{"expr" , NULL},
{"fakeidentd" , NULL},
{"false" , NULL},
{"fbset" , NULL},
{"fbsplash" , NULL},
{"fdflush" , NULL},
{"fdformat" , NULL},
{"fdisk" , NULL},
{"fetchmail" , NULL},
{"fgrep" , NULL},
{"find" , NULL},
{"fold" , NULL},
{"free" , NULL},
{"freeramdisk" , NULL},
{"fsck" , NULL},
{"fsck.minix" , NULL},
{"ftpget" , NULL},
{"ftpput" , NULL},
{"fuser" , NULL},
{"getopt" , NULL},
{"getty" , NULL},
{"grep" , NULL},
{"gunzip" , NULL},
{"gzip" , NULL},
{"halt" , NULL},
{"hdparm" , NULL},
{"head" , NULL},
{"hexdump" , NULL},
{"hostid" , NULL},
{"hostname" , NULL},
{"httpd" , NULL},
{"hwclock" , NULL},
{"id" , NULL},
{"ifconfig" , NULL},
{"ifdown" , NULL},
{"ifenslave" , NULL},
{"ifup" , NULL},
{"inetd" , NULL},
{"init" , NULL},
{"inotifyd" , NULL},
{"insmod" , NULL},
{"install" , NULL},
{"ip" , NULL},
{"ipaddr" , NULL},
{"ipcalc" , NULL},
{"ipcrm" , NULL},
{"ipcs" , NULL},
{"iplink" , NULL},
{"iproute" , NULL},
{"iprule" , NULL},
{"iptunnel" , NULL},
{"kbd_mode" , NULL},
{"kill" , NULL},
{"killall" , NULL},
{"killall5" , NULL},
{"klogd" , NULL},
{"last" , NULL},
{"length" , NULL},
{"less" , NULL},
{"linux32" , NULL},
{"linux64" , NULL},
{"linuxrc" , NULL},
{"ln" , NULL},
{"loadfont" , NULL},
{"loadkmap" , NULL},
{"logger" , NULL},
{"login" , NULL},
{"logname" , NULL},
{"logread" , NULL},
{"losetup" , NULL},
{"lpd" , NULL},
{"lpq" , NULL},
{"lpr" , NULL},
{"ls" , ls_main},
{"lsattr" , NULL},
{"lsmod" , NULL},
{"lzmacat" , NULL},
{"makedevs" , NULL},
{"man" , NULL},
{"md5sum" , NULL},
{"mdev" , NULL},
{"mesg" , NULL},
{"microcom" , NULL},
{"mkdir" , NULL},
{"mkfifo" , NULL},
{"mkfs.minix" , NULL},
{"mknod" , NULL},
{"mkswap" , NULL},
{"mktemp" , NULL},
{"modprobe" , NULL},
{"more" , NULL},
{"mount" , NULL},
{"mountpoint" , NULL},
{"mt" , NULL},
{"mv" , NULL},
{"nameif" , NULL},
{"nc" , NULL},
{"netstat" , NULL},
{"nice" , NULL},
{"nmeter" , NULL},
{"nohup" , NULL},
{"nslookup" , NULL},
{"od" , NULL},
{"openvt" , NULL},
{"passwd" , NULL},
{"patch" , NULL},
{"pgrep" , NULL},
{"pidof" , NULL},
{"ping" , NULL},
{"ping6" , NULL},
{"pipe_progress" , NULL},
{"pivot_root" , NULL},
{"pkill" , NULL},
{"poweroff" , NULL},
{"printenv" , NULL},
{"printf" , NULL},
{"ps" , NULL},
{"pscan" , NULL},
{"pwd" , pwd_main},
{"raidautorun" , NULL},
{"rdate" , NULL},
{"rdev" , NULL},
{"readahead" , NULL},
{"readlink" , NULL},
{"readprofile" , NULL},
{"realpath" , NULL},
{"reboot" , NULL},
{"renice" , NULL},
{"reset" , NULL},
{"resize" , NULL},
{"rm" , NULL},
{"rmdir" , NULL},
{"rmmod" , NULL},
{"route" , NULL},
{"rpm" , NULL},
{"rpm2cpio" , NULL},
{"rtcwake" , NULL},
{"run-parts" , NULL},
{"runlevel" , NULL},
{"runsv" , NULL},
{"runsvdir" , NULL},
{"rx" , NULL},
{"script" , NULL},
{"sed" , NULL},
{"sendmail" , NULL},
{"seq" , NULL},
{"setarch" , NULL},
{"setconsole" , NULL},
{"setfont" , NULL},
{"setkeycodes" , NULL},
{"setlogcons" , NULL},
{"setsid" , NULL},
{"setuidgid" , NULL},
{"sh" , NULL},
{"sha1sum" , NULL},
{"showkey" , NULL},
{"slattach" , NULL},
{"sleep" , NULL},
{"softlimit" , NULL},
{"sort" , NULL},
{"split" , NULL},
{"start-stop-daemon" , NULL},
{"stat" , NULL},
{"strings" , NULL},
{"stty" , NULL},
{"su" , NULL},
{"sulogin" , NULL},
{"sum" , NULL},
{"sv" , NULL},
{"svlogd" , NULL},
{"swapoff" , NULL},
{"swapon" , NULL},
{"switch_root" , NULL},
{"sync" , NULL},
{"sysctl" , NULL},
{"syslogd" , NULL},
{"tac" , NULL},
{"tail" , NULL},
{"tar" , NULL},
{"taskset" , NULL},
{"tcpsvd" , NULL},
{"tee" , NULL},
{"telnet" , NULL},
{"telnetd" , NULL},
{"test" , NULL},
{"tftp" , NULL},
{"tftpd" , NULL},
{"time" , NULL},
{"top" , NULL},
{"touch" , NULL},
{"tr" , NULL},
{"traceroute" , NULL},
{"true" , NULL},
{"tty" , NULL},
{"ttysize" , NULL},
{"udhcpc" , NULL},
{"udhcpd" , NULL},
{"udpsvd" , NULL},
{"umount" , NULL},
{"uname" , NULL},
{"uncompress" , NULL},
{"unexpand" , NULL},
{"uniq" , NULL},
{"unix2dos" , NULL},
{"unlzma" , NULL},
{"unzip" , NULL},
{"uptime" , NULL},
{"usleep" , NULL},
{"uudecode" , NULL},
{"uuencode" , NULL},
{"vconfig" , NULL},
{"vi" , NULL},
{"vlock" , NULL},
{"watch" , NULL},
{"watchdog" , NULL},
{"wc" , NULL},
{"wget" , NULL},
{"which" , NULL},
{"who" , NULL},
{"whoami" , NULL},
{"xargs" , NULL},
{"yes" , NULL},
{"zcat" , NULL},
{"zcip" , NULL},
};
int ls_main(int argc, char **argv)
{
printf("luther:ls\n");
}
int pwd_main(int argc, char **argv)
{
printf("luther:pwd\n");
}
static int applet_name_compare(const void *name, const void *v)
{
struct __applet_st *p = (struct __applet_st *)v;
return strcmp(name, p->name);
}
int main(int argc, char *argv[])
{
struct __applet_st *p;
int i;
if(argc < 2)
{
printf("luther name2find0 name2find1 name2find2 ...\n");
return 0;
}
for(i = 1;i < argc;i++)
{
p = bsearch(argv[i], applets_container, ARRAY_SIZE(applets_container), sizeof(applets_container[0]), applet_name_compare);
if(p)
{
int index = p - applets_container;
printf("%-20s,found at %d\n", p->name, index);
if(p->function)
{
p->function(argc, argv);
}
}
else
{
printf("%-20s,not found,sorry\n", argv[i]);
}
}
return 0;
}
[luther@gp tmp]$ ./busybox find ls vi chmod insmod chown pwd find ,found at 74 ls ,found at 137 luther:ls vi ,found at 275 chmod ,found at 21 insmod ,found at 104 chown ,found at 22 pwd ,found at 182 luther:pwd zcip ,found at 287 zciq ,not found,sorry
|