我正在做一个关于shell的项目,我希望代码能给出system()函数使用的路径。
例如,当我输入命令时
类型dir
答复是
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

07-26 00:22