github.com/shaardie/u-root@v4.0.1-0.20190127173353-f24a1c26aa2e+incompatible/pkg/bb/cmd/main.go (about) 1 // Copyright 2018 the u-root 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 package main 6 7 import ( 8 "log" 9 "os" 10 "path/filepath" 11 12 "github.com/u-root/u-root/pkg/bb" 13 ) 14 15 func run() { 16 name := filepath.Base(os.Args[0]) 17 if err := bb.Run(name); err != nil { 18 log.Fatalf("%s: %v", name, err) 19 } 20 } 21 22 func main() { 23 arg1 := os.Args[0] 24 for s, err := os.Readlink(arg1); err == nil && filepath.Base(s) != "bb"; s, err = os.Readlink(arg1) { 25 arg1 = s 26 } 27 os.Args[0] = arg1 28 29 run() 30 } 31 32 func init() { 33 m := func() { 34 if len(os.Args) == 0 { 35 log.Fatal("Arg len is 0. This is impossible") 36 } 37 if len(os.Args) == 1 { 38 // This might be a symlink, and have been invoked by an sshd. 39 // Let's try this: readlink until we get a terminal link. 40 // If the final link is "", then forget it. 41 var arg1 string 42 for s, err := os.Readlink(os.Args[0]); err == nil && filepath.Base(s) != "bb"; s, err = os.Readlink(arg1) { 43 arg1 = s 44 } 45 if arg1 == "" { 46 log.Fatalf("os.Args is %v: you need to specify which command to invoke.", os.Args) 47 } 48 os.Args = append(os.Args, arg1) 49 } 50 // Use argv[1] as the name. 51 os.Args = os.Args[1:] 52 run() 53 } 54 bb.Register("bb", bb.Noop, m) 55 bb.RegisterDefault(bb.Noop, m) 56 }