diff options
Diffstat (limited to 'debuggerd/libdebuggerd/utility.cpp')
-rw-r--r-- | debuggerd/libdebuggerd/utility.cpp | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/debuggerd/libdebuggerd/utility.cpp b/debuggerd/libdebuggerd/utility.cpp index 744cd72af..22fde5ea4 100644 --- a/debuggerd/libdebuggerd/utility.cpp +++ b/debuggerd/libdebuggerd/utility.cpp @@ -28,6 +28,7 @@ #include <string> #include <android-base/stringprintf.h> +#include <android-base/unique_fd.h> #include <backtrace/Backtrace.h> #include <log/log.h> @@ -202,3 +203,20 @@ void dump_memory(log_t* log, Backtrace* backtrace, uintptr_t addr, const char* f _LOG(log, logtype::MEMORY, "%s %s\n", logline.c_str(), ascii.c_str()); } } + +void read_with_default(const char* path, char* buf, size_t len, const char* default_value) { + android::base::unique_fd fd(open(path, O_RDONLY)); + if (fd != -1) { + int rc = TEMP_FAILURE_RETRY(read(fd.get(), buf, len - 1)); + if (rc != -1) { + buf[rc] = '\0'; + + // Trim trailing newlines. + if (rc > 0 && buf[rc - 1] == '\n') { + buf[rc - 1] = '\0'; + } + return; + } + } + strcpy(buf, default_value); +} |