我正在做一个关于 shell 的项目,我想要给我 system() 函数使用的路径的代码。
例如,当我输入命令时
输入目录
回复会是
dir 是外部命令(/bin/dir)
这是我达到的,但它不起作用
else if(strcmp(arg3[0],"type")==0) //if type command
{
if(strcmp(arg3[1],"cat")==0 || strcmp(arg3[1],"rm")==0 || strcmp(arg3[1],"rmdir")==0 || strcmp(arg3[1],"ls")==0 || strcmp(arg3[1],"cp")==0 || strcmp(arg3[1],"mv")==0 || strcmp(arg3[1],"exit")==0 || strcmp(arg3[1],"sleep")==0 || strcmp(arg3[1],"type")==0|| strcmp(arg3[1],"history") ==0)
{
printf("%s is a Rshell builtin\n", arg3[1]);
}
else
{
printf("%s is an external command\n", arg3[1]);
char * pPath;
pPath = getenv ("PATH");
if (pPath!=NULL)
printf ("The current path is: %s",pPath);
}
}
最佳答案
听起来您正在寻找 which
命令:
$ which ls
/bin/ls
关于c - 如何在 C 中找到 Unix system() 函数路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2983865/