或者也许采用老式方法:编写 C 代码来检查它是如何被调用的(原始命令行参数中的第 0 个参数)并根据该名称调用正确的 C 函数。需要将此类 C 程序编译为单个可执行文件,然后创建指向基本应用程序的符号链接,其中符号链接是感兴趣的函数的名称。除了将此处的工件(可执行文件和符号链接)安装到 $PATH 中的目录中之外,不需要 shell 代码。
例子。如果以下代码的名称为 toybox.c,并且 ~/bin 存在且位于用户的 $PATH 中,请使用类似以下内容的代码:
$ cc -o ~/bin/toybox toybox.c
$ ln -s toybox ~/bin/fn1
$ ln -s toybox ~/bin/fn2
$ ln -s toybox ~/bin/fn3
简单的测试 - 仅表明脚手架已就位。
$ fn1
fn1 invoked - no arguments.
$ fn3 1 2 'a b c'
fn3 invoked - arguments:
1 - '1'
2 - '2'
3 - 'a b c'
toybox.c 的源代码可能如下所示:
#include <string.h>
#include <libgen.h>
#include <stdio.h>
struct name2func {
const char *name;
int (*func)(int ac, char *const av[]);
};
void
fn_debug(const char *fn, int ac, char *const av[])
{
int n;
printf("%s invoked - ", fn);
if (ac <= 0) {
printf("no arguments.\n");
} else {
printf("arguments:\n");
for (n = 0; n < ac; n++) {
printf(" %d - '%s'\n", n + 1, av[n]);
}
}
}
int
fn1(int ac, char *const av[])
{
fn_debug("fn1", ac, av);
/* some C code for function 1. */
return 0;
}
int
fn2(int ac, char *const av[])
{
fn_debug("fn2", ac, av);
/* some C code for function 2. */
return 0;
}
int
fn3(int ac, char *const av[])
{
fn_debug("fn3", ac, av);
/* some C code for function 3. */
return 0;
}
/*
* Establish a crude symbol table after function definitions: size of
* the name2func array (i.e., its number of elements) is available via the
* sizeof builtin.
*/
struct name2func n2f[] = {
{ "fn1", fn1 },
{ "fn2", fn2 },
{ "fn3", fn3 }
};
int
dispatch(const char *func_name, int ac, char *const av[])
{
size_t n;
/* linear search ok for small # of funcs */
for (n = 0; n < sizeof n2f / sizeof n2f[0]; n++) {
if (strcmp(func_name, n2f[n].name) == 0) {
return (*n2f[n].func)(ac, av);
}
}
fprintf(stderr, "%s: unsupported\n", func_name);
return 1;
}
int
main(int argc, char *const argv[])
{
/*
* using POSIX basename(3) to create, say, "fn1" from
* a full-path invocation like "/my/odd/dir/fn1".
*/
char *fnbase = basename(argv[0]);
if (fnbase == 0) {
perror("basename");
return 1;
}
return dispatch(fnbase, argc - 1, argv + 1);
}