c - 如何在 C 中找到 Unix system() 函数路径

标签 c unix

我正在做一个关于 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/

相关文章:

c - C中指针变量的数组初始值设定项

linux - unix - 文件中的列数

unix - 将某些文件扩展名移动到 UNIX 中的不同目录

bash - 命令存在于路径上,但我得到 "No such file or directory"而不是 "command not found"

c - umask() 什么时候有用?

c - 如何在 XLib 中创建半透明的白色窗口

将具有动态数组的结构转换为双指针

c - 如何从 C 代码中删除所有调试 printf 语句

c - 无法在 CentOS 上编译 C++ 程序

bash - for循环找出unix中目录是否存在