简体   繁体   English

使用C ++在Linux中从环境变量中查找文件的路径名

[英]Finding Pathname for a file from Enviornment Variable in Linux using c++

So i am trying to create a Command Line Interpretor in Ubuntu using C/C++ It is part of our lab task for college The basic feature of the CLI is to take input from the user , parse it to find the command and its arguments , then look up the pathname for the command from the Environment Variable 因此,我正在尝试使用C / C ++在Ubuntu中创建命令行解释器。这是我们大学实验任务的一部分。CLI的基本功能是从用户那里获取输入,解析它以查找命令及其参数,然后从环境变量中查找命令的路径名
I have been able to parse the String , get the command and its arguments , I have also been able to read to the Path Environment Variables to get the directories of all the paths. 我已经能够解析String,获取命令及其参数,我还能够读取路径环境变量来获取所有路径的目录。 Now i must look through these directories to find where the file (command) lies , and then return the complete path so it can be sent to execve for execution in the child process i have to create a lookup function which takes the arguments array (the 0th index position contains the command name ) and the array of directories Here is an outline of the function that has been provided to us 现在我必须浏览这些目录以找到文件(命令)所在的位置,然后返回完整路径,以便可以将其发送给execve以便在子进程中执行,我必须创建一个使用参数数组的查找函数(第0个索引位置包含命令名称)和目录数组这是已提供给我们的函数的概述

// Search the directories identified by the dir argument to see
// if the argv[0] (the filename) appears there. Allocate a new
// string, place the full path name in it, then return the string.
//
char* lookupPath(char **argv, char **dir){
char* result;
char pName[MAX_PATH_LEN];
// check if is already an absolute path name
if( *argv[0] == '/' ) .....
// look in PATH directories, use access() to see if the
// file is in the dir
for( i = 0 ; i < MAX_PATHS ; i++ ) .....
// File name not found in any path variable
fprintf(stderr, "%s: command not found\n", argv[0]);
return NULL;
}

here argv is the array of arguments (0th index contains the command and the following index contains arguments) and dir[] contains an array of all the directories 此处argv是参数数组(第0个索引包含命令,以下索引包含参数),而dir []包含所有目录的数组

Now how em i suppose to traverse trough the directories to find the full path for the give command ? 现在我该如何遍历目录以查找Give命令的完整路径?

This more than I probably should have provided but there are enough things left you need to figure out that it isn't a slam dunk. 这超出了我本应提供的范围,但是您还需要确定足够的东西来确定它不是灌篮。

#include <unistd.h>

typedef std::vector<std::string> pathArray;

std::string lookupPath(const std::string pgm, pathArray &paths)
{
    int ret;

    for (int i = 0; i < paths.size(); ++i)
    {
       std::string temp = paths[i];

       temp = temp + "/" + pgm;

       // let execv determine if it is executable
       // or you can do that here if required
       if ((ret = access(temp.c_str(), F_OK)) == 0) 
           return temp;
    }

    return("");
}

int main(int argc, char *argv[])
{
    pathArray pa;

    pa.push_back("/bin");
    pa.push_back("/usr/bin"); //and the rest of PATH contents

    std::string pgm;
    std::string fullpath;

    pgm = "ls";

    if ((fullpath = lookupPath(pgm, pa)) != "")
    {
        //execv(fullpath.c_str(), ....);
        //etc...
    }
    else
    {  //could not find pgm
    }

    return(0);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM