|
@@ -5,6 +5,7 @@ import "base:intrinsics"
|
|
import "core:strconv"
|
|
import "core:strconv"
|
|
import "core:strings"
|
|
import "core:strings"
|
|
import "core:sys/linux"
|
|
import "core:sys/linux"
|
|
|
|
+import "core:fmt"
|
|
|
|
|
|
@(private)
|
|
@(private)
|
|
version_string_buf: [1024]u8
|
|
version_string_buf: [1024]u8
|
|
@@ -16,9 +17,13 @@ init_os_version :: proc () {
|
|
b := strings.builder_from_bytes(version_string_buf[:])
|
|
b := strings.builder_from_bytes(version_string_buf[:])
|
|
|
|
|
|
// Try to parse `/etc/os-release` for `PRETTY_NAME="Ubuntu 20.04.3 LTS`
|
|
// Try to parse `/etc/os-release` for `PRETTY_NAME="Ubuntu 20.04.3 LTS`
|
|
- {
|
|
|
|
|
|
+ pretty_parse: {
|
|
fd, errno := linux.open("/etc/os-release", {})
|
|
fd, errno := linux.open("/etc/os-release", {})
|
|
- assert(errno == .NONE, "Failed to read /etc/os-release")
|
|
|
|
|
|
+ if errno != .NONE {
|
|
|
|
+ strings.write_string(&b, "Unknown Linux Distro")
|
|
|
|
+ break pretty_parse
|
|
|
|
+ }
|
|
|
|
+
|
|
defer {
|
|
defer {
|
|
cerrno := linux.close(fd)
|
|
cerrno := linux.close(fd)
|
|
assert(cerrno == .NONE, "Failed to close the file descriptor")
|
|
assert(cerrno == .NONE, "Failed to close the file descriptor")
|
|
@@ -26,7 +31,10 @@ init_os_version :: proc () {
|
|
|
|
|
|
os_release_buf: [2048]u8
|
|
os_release_buf: [2048]u8
|
|
n, read_errno := linux.read(fd, os_release_buf[:])
|
|
n, read_errno := linux.read(fd, os_release_buf[:])
|
|
- assert(read_errno == .NONE, "Failed to read data from /etc/os-release")
|
|
|
|
|
|
+ if read_errno != .NONE {
|
|
|
|
+ strings.write_string(&b, "Unknown Linux Distro")
|
|
|
|
+ break pretty_parse
|
|
|
|
+ }
|
|
release := string(os_release_buf[:n])
|
|
release := string(os_release_buf[:n])
|
|
|
|
|
|
// Search the line in the file until we find "PRETTY_NAME="
|
|
// Search the line in the file until we find "PRETTY_NAME="
|
|
@@ -59,7 +67,7 @@ init_os_version :: proc () {
|
|
os_version.as_string = strings.to_string(b)
|
|
os_version.as_string = strings.to_string(b)
|
|
|
|
|
|
// Parse the Linux version out of the release string
|
|
// Parse the Linux version out of the release string
|
|
- {
|
|
|
|
|
|
+ version_loop: {
|
|
version_num, _, version_suffix := strings.partition(release_str, "-")
|
|
version_num, _, version_suffix := strings.partition(release_str, "-")
|
|
os_version.version = version_suffix
|
|
os_version.version = version_suffix
|
|
|
|
|
|
@@ -72,11 +80,11 @@ init_os_version :: proc () {
|
|
case 0: dst = &os_version.major
|
|
case 0: dst = &os_version.major
|
|
case 1: dst = &os_version.minor
|
|
case 1: dst = &os_version.minor
|
|
case 2: dst = &os_version.patch
|
|
case 2: dst = &os_version.patch
|
|
- case: break
|
|
|
|
|
|
+ case: break version_loop
|
|
}
|
|
}
|
|
|
|
|
|
num, ok := strconv.parse_int(part)
|
|
num, ok := strconv.parse_int(part)
|
|
- if !ok { break }
|
|
|
|
|
|
+ if !ok { break version_loop }
|
|
|
|
|
|
dst^ = num
|
|
dst^ = num
|
|
}
|
|
}
|