0

I have program to find emacs path using which command if it does not find emacs then i find emacs in $PATH variable. Lets my system have emacs then below program is giving output correct but it is sourcing .cshrc file I dont know why?

/* 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;
}
4

2 回答 2

0

正如您所说,您在 $PATH 变量中找到 emacs,所有路径变量都位于 ~/.cshrc 或 ~/.bashrc 中,具体取决于您是分别使用 csh 还是 bash。您可以使用命令 ps 查看正在使用的 shell。也许您在每个操作系统中使用不同的 shell。

于 2013-02-13T06:37:14.367 回答
0

SunOS which命令是一个csh脚本,.cshrc它会在搜索 csh 别名时获取您的文件,而它无法从外部 csh 执行此操作。linuxwhich命令是一个 posix shell 脚本。它们是不同的命令,以不同的方式运行。

沿着PATH环境变量搜索比依赖 which 命令更有意义。which 命令可以变成内置在某些 shell(例如zsh)上的 shell,并根据操作系统以不同的方式运行(我认为 mac 使用二进制文件)。

于 2013-02-13T07:35:47.817 回答