popen在sunos5上无法正常工作
popen not working correctly on sunos5
我有使用which command
查找emacs路径的程序如果它没有找到emacs,那么我在$PATH
变量中找到emacs。让我的系统拥有emacs然后下面的程序给出了正确的输出,但它正在寻找.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 commandn" );
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;
}
SunOS
which
命令是一个csh
脚本,它在搜索csh别名时为.cshrc
文件提供源代码,而这在csh外部是无法做到的。linux which
命令是一个posix shell脚本。它们是不同的命令,以不同的方式操作。
沿着PATH
环境变量进行搜索比依赖哪个命令更有意义。which命令可以变成某些shell(例如zsh
)上内置的shell,并根据操作系统以不同的方式运行(我认为mac使用二进制)。
正如您所说,您在$PATH变量中找到emacs,所有路径变量都位于~/.cshrc或~/.bashrc中,具体取决于您分别使用csh还是bash。您可以使用命令ps查看您使用的shell。也许您在每个操作系统中使用的shell不同。
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- ExtractIconEx:可以工作,但偶尔会崩溃
- C++中的memset函数工作不正常
- 当我在第一个循环中使用"auto"时,它工作正常,但是使用"int"它会给出错误,为什么?
- 为什么STD ::计数将常数传递给Lambda,而不是在弦上工作时而不是字符
- C++程序已停止工作-求解常微分方程