popen在sunos5上无法正常工作

popen not working correctly on sunos5

本文关键字:常工作 工作 sunos5 popen      更新时间:2023-10-16

我有使用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不同。