C++ popen在sunos5上工作不正常
我有一个使用C++ popen在sunos5上工作不正常,c++,C++,我有一个使用which命令查找emacs路径的程序 如果它没有找到emacs,那么我在$PATH变量中找到emacs。 让我的系统有emacs 下面的程序给出了正确的输出,但它是sourcing.cshrc文件,我不知道为什么 /* getenv example: getting path */ #include <stdio.h> #include <stdlib.h> #include <iostream> #include <sys/stat.h&
which命令查找emacs路径的程序
如果它没有找到emacs,那么我在$PATH
变量中找到emacs。
让我的系统有emacs
下面的程序给出了正确的输出,但它是sourcing.cshrc文件,我不知道为什么
/* getenv example: getting path */
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <sys/stat.h>
using namespace std;
int main ()
{
FILE *fp;
int status;
char path[256];
const char *command = "which emacs 2>&1";
/* Open the command for reading. */
fp = popen(command, "r");
if (fp == NULL) {
printf("Failed to run command\n" );
exit(0);
}
string path1;
/* Read the output a line at a time - output it. */
while (fgets(path, sizeof(path)-1, fp) != NULL) {
path1 += path;
}
cout<<"orignal path after which command = "<<path1<<endl;
/* close */
bool found = true;
std::string search="which:";
char *tmp;
tmp = strstr(path1.c_str(),search.c_str());
if (tmp != NULL)
{
found = false;
}
else
{
found = true;
}
if (found){
cout<<"Found Emacs"<<endl;
cout<<"path = "<<path1;
string path2;
for (int i=0; i < path1.length()-1; i++)
{
path2 += path1[i];
}
//path1[path1.length()-1]= " ";
path2 += " -i";
cout<<"final path = "<<path2<<endl;}
else
cout<<"Not found Emacs"<<endl;
pclose(fp);
return 0;
}
/*getenv示例:获取路径*/
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
文件*fp;
智力状态;
字符路径[256];
const char*command=“哪个emacs 2>&1”;
/*打开命令进行读取*/
fp=popen(命令,“r”);
如果(fp==NULL){
printf(“无法运行命令\n”);
出口(0);
}
字符串路径1;
/*一次读取一行输出-输出它*/
while(fgets(path,sizeof(path)-1,fp)!=NULL){
路径1+=路径;
}
cout正如您所说,您正在$PATH变量中查找emacs,所有路径变量都位于~/.cshrc或~/.bashrc中,具体取决于您是否分别使用csh或bash。您可以看到您使用的是哪个shell命令ps。可能您在每个操作系统中使用了不同的shell。SunOS
which
command是一个csh
脚本,在搜索csh别名时,它会生成.cshrc
文件,这是从csh外部无法完成的。linuxwhich
命令是一个posix shell脚本。它们是不同的命令,以不同的方式运行
沿着
路径
环境变量进行搜索比依赖which命令更有意义。which命令可以变成某些shell上内置的shell(例如zsh
),并根据操作系统以不同的方式运行(我认为mac使用二进制).它在Linux上工作,但在SunosI上不工作。我在SunoS和Linux上使用tcsh,但我只在SunoS上遇到这个问题。