1#include <errno.h>
2#include <fstream>
3#include <iostream>
4#include <regex>
5#include <string>
6#include <string.h>
7
8int main(int argc, char * argv[]) {
9    if (argc != 2) {
10        std::cerr << "usage: " << argv[0] << ": libname\n";
11        return -1;
12    }
13    std::regex reg(std::string("^([a-f0-9]+)\\-[0-9a-f]+\\s+.+\\s+(\\d+)\\s+.+\\s+\\d+\\s+") + std::string(argv[1]) + std::string("\\s*$"));
14
15    /* open /proc/self/maps */
16    std::string ln;
17    std::ifstream m_file("/proc/self/maps");
18    if (!m_file) {
19        std::cerr << "Unable to open /proc/self/maps " << strerror(errno) << "\n";
20        return -1;
21    }
22    while (getline(m_file, ln)) {
23        std::smatch sm;
24        if (std::regex_match (ln,sm, reg)) {
25            if (std::stoi(sm[2]) == 0) {
26                std::cout << sm[1];
27                return 0;
28            }
29        }
30    }
31    return -1;
32}
33