forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pwd.go
58 lines (50 loc) · 1.09 KB
/
pwd.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
// Copyright 2012-2017 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Print name of current directory.
//
// Synopsis:
// pwd [-LP]
//
// Options:
// -P: don't follow symlinks
//
// Author:
// created by Beletti (rhiguita@gmail.com)
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
)
var (
// This is the default. Setting it to false doesn't do anything in GNU
// or zsh pwd, because you just can't even set it to false.
_ = flag.Bool("L", true, "don't follow any symlinks")
physical = flag.Bool("P", false, "follow all symlinks (avoid all symlinks)")
cmd = "pwd [-LP]"
)
func init() {
defUsage := flag.Usage
flag.Usage = func() {
os.Args[0] = cmd
defUsage()
}
}
func pwd(followSymlinks bool) (string, error) {
path, err := os.Getwd()
if err == nil && followSymlinks {
path, err = filepath.EvalSymlinks(path)
}
return path, err
}
func main() {
flag.Parse()
path, err := pwd(*physical)
if err != nil {
log.Fatalf("%v", err)
}
fmt.Println(path)
}