It s difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
对于一个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);
我希望将变量 "directory" 设置为我要调用的 Unix 命令的正确目录。
So, in essence, I would like to be able to use something like char directory[MAX_CANON] = which(cmd);
这样,which函数就会返回我要调用的命令的目录。
如果您觉得这不是一个好的解决方案,请推荐其他方法。
谢谢 (Xiè xiè)
编辑:如果必要我可以编写大量的if-else语句,根据cmd参数设置目录vbl。