1
2
3
4
5
6
7 package osinfo
8
9 import (
10 "bytes"
11 "strings"
12 "unsafe"
13 )
14
15
16 func Version() (string, error) {
17 var uts utsname
18 if err := uname(&uts); err != nil {
19 return "", err
20 }
21
22 var sb strings.Builder
23
24 writeCStr := func(b []byte) {
25 if i := bytes.IndexByte(b, '\000'); i >= 0 {
26 b = b[:i]
27 }
28 sb.Write(b)
29 }
30
31
32
33
34 s := uts.Sysname[:]
35 writeCStr(*(*[]byte)(unsafe.Pointer(&s)))
36 sb.WriteByte(' ')
37 s = uts.Release[:]
38 writeCStr(*(*[]byte)(unsafe.Pointer(&s)))
39 sb.WriteByte(' ')
40 s = uts.Version[:]
41 writeCStr(*(*[]byte)(unsafe.Pointer(&s)))
42 sb.WriteByte(' ')
43 s = uts.Machine[:]
44 writeCStr(*(*[]byte)(unsafe.Pointer(&s)))
45
46 return sb.String(), nil
47 }
48
View as plain text