1 |
Douglas Thrift |
246 |
// Executable Name |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
|
|
#include <iostream> |
8 |
|
|
#include <string> |
9 |
|
|
#include <sstream> |
10 |
|
|
#include <cstdio> |
11 |
|
|
|
12 |
|
|
extern "C" |
13 |
|
|
{ |
14 |
|
|
#include <sys/param.h> |
15 |
|
|
#include <unistd.h> |
16 |
|
|
#include <sys/types.h> |
17 |
|
|
#include <sys/stat.h> |
18 |
|
|
} |
19 |
|
|
|
20 |
|
|
#include <cstdlib> |
21 |
|
|
|
22 |
|
|
int main(int argc, char* argv[]) |
23 |
|
|
{ |
24 |
|
|
std::string program(argv[0]); |
25 |
|
|
|
26 |
|
|
if (program.find('/') == std::string::npos) |
27 |
|
|
{ |
28 |
|
|
std::istringstream path(::getenv("PATH") != NULL ? ::getenv("PATH") |
29 |
|
|
: std::string()); |
30 |
|
|
|
31 |
|
|
do |
32 |
|
|
{ |
33 |
|
|
std::string candidate; |
34 |
|
|
|
35 |
|
|
std::getline(path, candidate, ':'); |
36 |
|
|
|
37 |
|
|
candidate += '/' + program; |
38 |
|
|
|
39 |
|
|
if (::access(candidate.c_str(), X_OK) == 0) |
40 |
|
|
{ |
41 |
|
|
struct ::stat file; |
42 |
|
|
|
43 |
|
|
if (::stat(candidate.c_str(), &file) == 0 |
44 |
|
|
&& S_ISREG(file.st_mode) && (::getuid() != 0 |
45 |
|
|
|| (file.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0)) |
46 |
|
|
{ |
47 |
|
|
::lstat(candidate.c_str(), &file); |
48 |
|
|
|
49 |
|
|
if (S_ISLNK(file.st_mode)) |
50 |
|
|
{ |
51 |
|
|
char buffer[1024]; |
52 |
|
|
int length(::readlink(candidate.c_str(), buffer, 1024)); |
53 |
|
|
|
54 |
|
|
if (length == -1) |
55 |
|
|
{ |
56 |
|
|
::perror(argv[0]); |
57 |
|
|
|
58 |
|
|
return 1; |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
program.assign(buffer, length); |
62 |
|
|
} |
63 |
|
|
else program = candidate; |
64 |
|
|
|
65 |
|
|
break; |
66 |
|
|
} |
67 |
|
|
} |
68 |
|
|
} |
69 |
|
|
while (path.good()); |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
char executable[PATH_MAX]; |
73 |
|
|
|
74 |
|
|
if (::realpath(program.c_str(), executable) == NULL) |
75 |
|
|
{ |
76 |
|
|
::perror(argv[0]); |
77 |
|
|
|
78 |
|
|
return 1; |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
std::cout << "executable = " << executable << '\n'; |
82 |
|
|
|
83 |
|
|
return 0; |
84 |
|
|
} |