// or [ $(grep -oE 'gcc version ([0-9]+)' /proc/version | awk '{print $3}') -gt 5 ] && \
// echo "WSL2" || echo "WSL1"
// g++ -std=c++17 wslver.cpp -o wslver
#include <fstream>
#include <iostream>
#include <array>
#include <string>
#include <charconv>
#include <regex>
using namespace std::string_literals;
int main(int argc, const char *argv[])
{
std::ifstream ifs;
ifs.open("/proc/version");
constexpr size_t bufsz = 128;
std::array<char, bufsz> buf;
ifs.getline(buf.data(), bufsz, '\n');
std::string proc_ver(buf.data(), buf.size());
std::regex rx("Linux version [0-9]+\\.[0-9]+\\.[0-9]+.*[Mm]icrosoft.*gcc version ([0-9]+)\\.[0-9]+\\.[0-9]+");
std::smatch match;
auto gcc_major = (std::regex_search(proc_ver, match, rx) && match.size() == 2) ? match[1] : "5"s;
int maj = 0;
std::from_chars(gcc_major.data(), gcc_major.data() + gcc_major.size(), maj);
std::cout << ((maj < 8) ? "1\n" : "2\n");
return 0;
}
Biswa96 and craiglo
Comments