Last active
October 8, 2015 13:47
-
-
Save AGWA/248a0b1582f49126545e to your computer and use it in GitHub Desktop.
C++ readlink wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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