Skip to content
Snippets Groups Projects

Improve LinuxVersion

Merged Florian Schmaus requested to merge flow/emper:linux-version into master
2 files
+ 22
11
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 20
4
@@ -2,11 +2,12 @@
// Copyright © 2021 Florian Fischer
#include "lib/LinuxVersion.hpp"
#include <sys/utsname.h>
#include <cerrno>
#include <cstdlib>
#include "Common.hpp"
#include "Debug.hpp"
static auto checked_strtol(const std::string& s) -> long {
static const int DECIMAL = 10;
@@ -23,15 +24,30 @@ static auto checked_strtol(const std::string& s) -> long {
DIE_MSG("strtol found no digits in " << s);
}
if (endptr != startptr + s.size()) {
LOGW("LinuxVersion compare ignored non digits in " << s)
}
// TODO: We could report remaining bytes of the string by returning
// std::string(endptr) together with 'res' (potentially using
// std::pair). Not sure if we even need to check if "endptr !=
// startptr + s.size()", or if we could always cosntruct
// std::string(endptr), potentially constructing the empty string.
return res;
}
namespace emper::lib {
std::string LinuxVersion::globalVersion;
LinuxVersion::LinuxVersion() {
if (globalVersion.empty()) {
struct utsname buf;
if (uname(&buf)) {
DIE_MSG_ERRNO("Failed to invoke uname()");
}
globalVersion = std::string(buf.release);
}
version = globalVersion;
}
// Returns 1 if s is smaller, -1 if this is smaller, 0 if equal
auto LinuxVersion::compare(const std::string& s) -> int {
size_t dot_pos, dot_pos2;
Loading