Advertisement
NLinker

Check WSL1 vs WSL2

Nov 15th, 2022 (edited)
784
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | Source Code | 0 0
  1. // or [ $(grep -oE 'gcc version ([0-9]+)' /proc/version | awk '{print $3}') -gt 5 ] && \
  2. //    echo "WSL2" || echo "WSL1"
  3.  
  4. // g++ -std=c++17 wslver.cpp -o wslver
  5. #include <fstream>
  6. #include <iostream>
  7. #include <array>
  8. #include <string>
  9. #include <charconv>
  10. #include <regex>
  11. using namespace std::string_literals;
  12.  
  13. int main(int argc, const char *argv[])
  14. {
  15.     std::ifstream ifs;
  16.     ifs.open("/proc/version");
  17.     constexpr size_t bufsz = 128;
  18.     std::array<char, bufsz> buf;
  19.     ifs.getline(buf.data(), bufsz, '\n');
  20.     std::string proc_ver(buf.data(), buf.size());
  21.     std::regex rx("Linux version [0-9]+\\.[0-9]+\\.[0-9]+.*[Mm]icrosoft.*gcc version ([0-9]+)\\.[0-9]+\\.[0-9]+");
  22.     std::smatch match;
  23.     auto gcc_major = (std::regex_search(proc_ver, match, rx) && match.size() == 2) ? match[1] : "5"s;
  24.     int maj = 0;
  25.     std::from_chars(gcc_major.data(), gcc_major.data() + gcc_major.size(), maj);
  26.     std::cout << ((maj < 8) ? "1\n" : "2\n");
  27.     return 0;
  28. }
  29.  
  30. Biswa96 and craiglo
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement