C++ readlink wrapper
// This is C++11-only because it assumes that std::strings can | |
// be accessed and mutated as a contiguous sequence of chars. | |
std::string sys::readlink (const char* pathname) | |
{ | |
std::string buffer(64, '\0'); | |
ssize_t len; | |
while ((len = ::readlink(pathname, &buffer[0], buffer.size())) == static_cast<ssize_t>(buffer.size())) { | |
// buffer may have been truncated - grow and try again | |
buffer.resize(buffer.size() * 2); | |
} | |
if (len == -1) { | |
throw error("readlink", errno); | |
} | |
buffer.resize(len); | |
return buffer; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment