<分区>
对于 C 语言的项目,我们需要在 Unix 服务器上构建一个 shell。
它需要能够执行 Unix 原生的功能,而不是 bash(或任何其他 Unix shell)。
我正在编写一种方法,希望将对命令的调用泛化。进入一般功能:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
#include <limits.h>
void execCmd(char* cmd,char* options)
{
char directory[MAX_CANON] = "/bin/"; //currently is just set to /bin
char* toExec = strcat(directory,cmd);
我想将变量“目录”设置为我正在调用的 Unix 命令的正确目录。
所以,本质上,我希望能够使用类似 字符目录[MAX_CANON] = which(cmd);
这样 which 函数将返回我尝试调用的命令的目录。
如果您觉得这不是一个好的解决方案,请推荐其他方式。
谢谢
编辑:我想最坏的情况是我可以做大量的 if-else 语句,根据 cmd 参数设置“目录”vbl。