aboutsummaryrefslogtreecommitdiff
path: root/src/path.cc
blob: 620c464b6c49f8a8ffc3292e9c38c467eb3e972d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

#include "path.h"

namespace ov4
{

using namespace std;

string find_cmd(char* s)
{
    string tmp = s;
    return find_cmd(tmp);
}

string find_cmd(string s)
{
    if (s.size() == 0) return "/";
    if (s[0] == '/') return s;

    string path = getenv("PATH");
    size_t i = 0, j = 0, l = path.size();
    while (true)
    {
        for (; j < l; j++)
            if (path[j] == ':')
                break;
        
        string cmd = path.substr(i, j-i) + '/' +s;
        if (access(cmd.c_str(), F_OK) == 0)
            return cmd;

        if (j == l)
            return "/";

        j++;
        i=j;

    }
}


}