This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
/
uname.go
182 lines (150 loc) · 3.83 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"flag"
"fmt"
"log"
"os"
"strconv"
)
import (
"github.com/sternix/commands/lib/sysctl"
)
const (
MFLAG = 1 << iota
NFLAG
PFLAG
RFLAG
SFLAG
VFLAG
IFLAG
KFLAG
)
var (
allFlag = flag.Bool("a", false, "Behave as though the options -m, -n, -r, -s, and -v were specified.")
identFlag = flag.Bool("i", false, "Write the kernel ident to standard output.")
platformFlag = flag.Bool("m", false, "Write the type of the current hardware platform to standard output.")
hostnameFlag = flag.Bool("n", false, "Write the name of the system to standard output.")
osnameFlag = flag.Bool("o", false, "This is a synonym for the -s option, for compatibility with other systems.")
archFlag = flag.Bool("p", false, "Write the type of the machine processor architecture to standard output.")
releaseFlag = flag.Bool("r", false, "Write the current release level of the operating system to standard output.")
sysnameFlag = flag.Bool("s", false, "Write the name of the operating system implementation to standard output.")
versionFlag = flag.Bool("v", false, "Write the version level of this release of the operating system to standard output.")
kernversFlag = flag.Bool("K", false, "Write the FreeBSD version of the kernel.")
)
func init() {
log.SetFlags(0)
log.SetOutput(os.Stderr)
}
func main() {
flag.Usage = usage
flag.Parse()
var flags int
if *allFlag {
flags |= (MFLAG | NFLAG | RFLAG | SFLAG | VFLAG)
}
if *identFlag {
flags |= IFLAG
}
if *osnameFlag || *sysnameFlag {
flags |= SFLAG
}
if *platformFlag {
flags |= MFLAG
}
if *hostnameFlag {
flags |= NFLAG
}
if *archFlag {
flags |= PFLAG
}
if *releaseFlag {
flags |= RFLAG
}
if *versionFlag {
flags |= VFLAG
}
if *kernversFlag {
flags |= KFLAG
}
if flags == 0 {
flags |= SFLAG
}
printUname(flags)
}
var isPrinted bool
func printFlag(flags, flg int, envVar, sysctlName string, fn func(string, string) string) {
if flags&flg == flg {
if isPrinted {
fmt.Print(" ")
}
fmt.Print(fn(envVar, sysctlName))
isPrinted = true
}
}
func printUname(flags int) {
printFlag(flags, SFLAG, "s", "kern.ostype", getValue)
printFlag(flags, NFLAG, "n", "kern.hostname", getValue)
printFlag(flags, RFLAG, "r", "kern.osrelease", getValue)
printFlag(flags, VFLAG, "v", "kern.version", getVersion)
printFlag(flags, MFLAG, "m", "hw.machine", getValue)
printFlag(flags, PFLAG, "p", "hw.machine_arch", getValue)
printFlag(flags, IFLAG, "i", "kern.ident", getValue)
printFlag(flags, KFLAG, "K", "kern.osreldate", getIntValue)
fmt.Println()
}
func isEnvSet(flg string) (bool, string) {
if val := os.Getenv("UNAME_" + flg); val != "" {
return true, val
} else {
return false, ""
}
}
func getSysctl(sysctlName string) string {
if val, err := sysctl.ByName(sysctlName); err != nil {
log.Fatalf("%s - %v\n", sysctlName, err)
return "" //NOTREACHED
} else {
return val
}
}
func getSysctlUint32AsString(sysctlName string) string {
if ret, err := sysctl.Uint32(sysctlName); err != nil {
log.Fatalf("%s - %v\n", sysctlName, err)
return "" //NOTREACHED
} else {
return strconv.FormatUint(uint64(ret), 10)
}
}
func getValue(envVar, sysctlName string) string {
if yes, envVal := isEnvSet(envVar); yes {
return envVal
} else {
return getSysctl(sysctlName)
}
}
func getIntValue(envVar, sysctlName string) string {
if yes, envVal := isEnvSet(envVar); yes {
return envVal
} else {
return getSysctlUint32AsString(sysctlName)
}
}
func getVersion(envVar, sysctlName string) string {
if yes, envVal := isEnvSet(envVar); yes {
return envVal
} else {
version, err := sysctl.Raw(sysctlName)
if err != nil {
log.Fatalf("%s - %v\n", sysctlName, err)
}
for i, r := range version {
if r == '\n' || r == '\t' {
version[i] = ' '
}
}
return string(version)
}
}
func usage() {
log.Fatalln("usage: uname [-aiKmnoprsUv]")
}