Source file src/cmd/internal/osinfo/os_uname.go

     1  // Copyright 2022 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build aix || linux || solaris
     6  
     7  package osinfo
     8  
     9  import (
    10  	"bytes"
    11  	"strings"
    12  	"unsafe"
    13  )
    14  
    15  // Version returns the OS version name/number.
    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  	// We need some absurd conversions because syscall.Utsname
    32  	// sometimes uses []uint8 and sometimes []int8.
    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