#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <iostream>
#include <string>
class process_utility {
public:
static void get_process_bin_path(int pid, std::string &bin_path) {
char buf[512] = "";
snprintf(buf, sizeof(buf), "ls -l /proc/%d/exe", pid);
FILE *fp = popen(buf, "r");
if (!fp) {
return;
}
memset(buf, 0, sizeof(buf));
if (nullptr == fgets(buf, sizeof(buf) - 1, fp)) {
fclose(fp);
return;
}
int len = strlen(buf);
int i = len - 1;
if ('\n' == buf[i]) {
buf[i] = 0;
}
for (;i >= 0;i--) {
if (buf[i] == '>') {
break;
}
}
if (i >= 0) {
bin_path.assign(buf + i + 2);
}
fclose(fp);
fp = nullptr;
}
};
int main() {
int pid = 1079;
std::string bin_path;
process_utility::get_process_bin_path(pid, bin_path);
std::cout << bin_path << std::endl;
return 0;
}