Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

Commit

Permalink
uname: Added proper -o, --operating-system switch. Fixed internal/flag
Browse files Browse the repository at this point in the history
  • Loading branch information
ericlagergren committed Mar 7, 2016
1 parent 30afe3c commit fab43bc
Show file tree
Hide file tree
Showing 20 changed files with 256 additions and 162,374 deletions.
162,193 changes: 0 additions & 162,193 deletions .tags

This file was deleted.

7 changes: 3 additions & 4 deletions README.md
Expand Up @@ -58,13 +58,13 @@ but for the most part these tools should have nearly the same speed,
with Go being slightly slower.

```
eric@archbox ~/gopath/src/github.com/EricLagergren/go-coreutils/wc $ time ./wc_go -lwmc one_gigabyte_file.txt
eric@archbox $ time ./wc_go -lwmc one_gigabyte_file.txt
32386258 146084896 1182425560 1183778772 one_gigabyte_file.txt
real 0m25.206s
user 0m24.900s
sys 0m0.313s
eric@archbox ~/gopath/src/github.com/EricLagergren/go-coreutils/wc $ time wc_c -lwmc one_gigabyte_file.txt
eric@archbox $ time wc_c -lwmc one_gigabyte_file.txt
32386258 146084896 1182425560 1183778772 one_gigabyte_file.txt
real 0m22.841s
Expand Down Expand Up @@ -92,12 +92,11 @@ Go version is technically more correct, while the C version is faster.
Our implementation of `xxd` is actually much faster than the native `xxd`
implementation found on most *nix machines -- try it out!

## REQUIRES:
### REQUIRES:

(Depends on platform and command...)
- go get github.com/EricLagergren/ostypes
- go get golang.org/x/sys/unix
- go get github.com/ogier/pflag
- go get github.com/EricLagergren/go-gnulib/ttyname
- go get github.com/EricLagergren/go-gnulib/sysinfo
- go get github.com/EricLagergren/go-gnulib/posix
Expand Down
10 changes: 8 additions & 2 deletions internal/flag/flag.go
Expand Up @@ -514,6 +514,12 @@ var Usage = func() {
Bugs()
}

func DBE() {
PrintDefaults()
Bugs()
os.Exit(0)
}

var Version = func() {
fmt.Fprintf(os.Stdout, `%s (Go coreutils) %s
Copyright (c) 2016 Eric Lagergren
Expand All @@ -526,7 +532,7 @@ There is NO WARRANTY, to the extent permitted by law.

var Bugs = func() {
fmt.Printf(`
Report %s bugs to eric@ericlagergren.com.com
Report %s bugs to eric@ericlagergren.com
Go coreutils home page: <https://www.github.com/EricLagergren/go-coreutils/>
`, Program)
}
Expand Down Expand Up @@ -673,7 +679,7 @@ func VarP(value Value, name, shorthand, usage string) {
func (f *FlagSet) failf(format string, a ...interface{}) error {
err := fmt.Errorf(format, a...)
fmt.Fprintln(f.out(), err)
f.usage()
fmt.Fprintf(f.out(), "Try '%s --help' for more information.\n", f.name)
return err
}

Expand Down
9 changes: 9 additions & 0 deletions uname/README.md
@@ -0,0 +1,9 @@
The -o, --operating-system switch should work just fine for most
standard OSs.

If you need to set a custom value, please install the tool as such:

`go install --tags=ostypes -ldflags '-X main.hostOS=FOO'`

If you do not know the value you need to set, please run `go generate` prior
to installing this command.
3 changes: 3 additions & 0 deletions uname/generate.go
@@ -0,0 +1,3 @@
package main

//go:generate go run ostypes.go
5 changes: 3 additions & 2 deletions uname/host_os.go
@@ -1,4 +1,5 @@
// +build ostypes

package main

// HostOS will be overwritten with the go:generate command.
const HostOS = Unknown
var hostOS = unknown
5 changes: 5 additions & 0 deletions uname/host_os_darwin.go
@@ -0,0 +1,5 @@
// +build darwin,!ostypes

package main

const hostOS = "Darwin"
5 changes: 5 additions & 0 deletions uname/host_os_dragonfly.go
@@ -0,0 +1,5 @@
// +build dragonfly,!ostypes

package main

const hostOS = "Dragonfly"
5 changes: 5 additions & 0 deletions uname/host_os_freebsd.go
@@ -0,0 +1,5 @@
// +build linux,!ostypes

package main

const hostOS = "GNU/Linux"
5 changes: 5 additions & 0 deletions uname/host_os_linux.go
@@ -0,0 +1,5 @@
// +build linux,!ostypes

package main

const hostOS = "GNU/Linux"
5 changes: 5 additions & 0 deletions uname/host_os_netbsd.go
@@ -0,0 +1,5 @@
// +build netbsd,!ostypes

package main

const hostOS = "NetBSD"
5 changes: 5 additions & 0 deletions uname/host_os_openbsd.go
@@ -0,0 +1,5 @@
// +build openbsd,!ostypes

package main

const hostOS = "OpenBSD"
5 changes: 5 additions & 0 deletions uname/host_os_plan9.go
@@ -0,0 +1,5 @@
// +build plan9,!ostypes

package main

const hostOS = "Plan 9"
5 changes: 5 additions & 0 deletions uname/host_os_solaris.go
@@ -0,0 +1,5 @@
// +build solaris,!ostypes

package main

const hostOS = "Solaris"
5 changes: 5 additions & 0 deletions uname/host_os_windows.go
@@ -0,0 +1,5 @@
// +build windows,!ostypes

package main

const hostOS = "Windows NT"
116 changes: 116 additions & 0 deletions uname/ostypes.go
@@ -0,0 +1,116 @@
// +build ignore

package main

import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)

// globs are stolen from GNU coreutils' host-os.m4 file.
// see GNU-LICENSE.txt
var globs = map[string]string{
"winnt*": "Windows NT",
"vos*": "VOS",
"sysv*": "Unix System V",
"superux*": "SUPER-UX",
"sunos*": "SunOS",
"stop*": "STOP",
"sco*": "SCO Unix",
"riscos*": "RISC OS",
"riscix*": "RISCiX",
"qnx*": "QNX",
"pw32*": "PW32",
"ptx*": "ptx",
"plan9*": "Plan 9",
"osf*": "Tru64",
"os2*": "OS/2",
"openbsd*": "OpenBSD",
"nsk*": "NonStop Kernel",
"nonstopux*": "NonStop-UX",
"netbsd*-gnu*": "GNU/NetBSD",
"netbsd*": "NetBSD",
"mirbsd*": "MirBSD",
"knetbsd*-gnu": "GNU/kNetBSD",
"kfreebsd*-gnu": "GNU/kFreeBSD",
"msdosdjgpp*": "DJGPP",
"mpeix*": "MPE/iX",
"mint*": "MiNT",
"mingw*": "MinGW",
"lynxos*": "LynxOS",
"linux*": "GNU/Linux",
"hpux*": "HP-UX",
"hiux*": "HI-UX",
"gnu*": "GNU",
"freebsd*": "FreeBSD",
"dgux*": "DG/UX",
"bsdi*": "BSD/OS",
"bsd*": "BSD",
"beos*": "BeOS",
"aux*": "A/UX",
"atheos*": "AtheOS",
"amigaos*": "Amiga OS",
"aix*": "AIX",
}

func match(pattern, name string) bool {
ok, err := filepath.Match(pattern, name)
return ok && err == nil
}

func main() {
const pkg = "main"

// Shell locals...
out, err := exec.Command("sh", "-c", "echo $OSTYPE").Output()
if err != nil {
panic(err)
}

if len(out) > 0 && out[len(out)-1] == '\n' {
out = out[:len(out)-1]
}

file, err := os.Create("host_os.go")
if err != nil {
panic(err)
}
defer file.Close()

rawHostOS := string(out)

fmt.Fprintln(file, "// +build ostypes")
fmt.Fprintf(file, "// generated by ostypes. DO NOT EDIT.\n\n")
fmt.Fprintf(file, "package %s\n\nconst hostOS = ", pkg)

// This is what uname does.
if rawHostOS == "" {
fmt.Fprintf(file, "%q\n\n", "unknown")
return
}

var realHostOS string
for glob, val := range globs {
if match(glob, rawHostOS) {
realHostOS = val
break
}
}

// No matches found, so apply the default heuristic.
if realHostOS == "" {
if match("[A-Za-z]*", rawHostOS) {
realHostOS := strings.ToUpper(string(rawHostOS[0]))
if len(rawHostOS) > 1 {
realHostOS += rawHostOS[1:]
}
} else {
realHostOS = rawHostOS
}
}

fmt.Fprintf(file, "%q\n\n", realHostOS)
}

0 comments on commit fab43bc

Please sign in to comment.