-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathos_uname.go
47 lines (38 loc) · 995 Bytes
/
os_uname.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build aix || linux || solaris
package osinfo
import (
"bytes"
"strings"
"unsafe"
)
// Version returns the OS version name/number.
func Version() (string, error) {
var uts utsname
if err := uname(&uts); err != nil {
return "", err
}
var sb strings.Builder
writeCStr := func(b []byte) {
if i := bytes.IndexByte(b, '\000'); i >= 0 {
b = b[:i]
}
sb.Write(b)
}
// We need some absurd conversions because syscall.Utsname
// sometimes uses []uint8 and sometimes []int8.
s := uts.Sysname[:]
writeCStr(*(*[]byte)(unsafe.Pointer(&s)))
sb.WriteByte(' ')
s = uts.Release[:]
writeCStr(*(*[]byte)(unsafe.Pointer(&s)))
sb.WriteByte(' ')
s = uts.Version[:]
writeCStr(*(*[]byte)(unsafe.Pointer(&s)))
sb.WriteByte(' ')
s = uts.Machine[:]
writeCStr(*(*[]byte)(unsafe.Pointer(&s)))
return sb.String(), nil
}