gitlab.com/apertussolutions/u-root@v7.0.0+incompatible/cmds/core/pwd/pwd.go (about)

     1  // Copyright 2012-2017 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  // Print name of current directory.
     6  //
     7  // Synopsis:
     8  //     pwd [-LP]
     9  //
    10  // Options:
    11  //     -P: don't follow symlinks
    12  //
    13  // Author:
    14  //     created by Beletti (rhiguita@gmail.com)
    15  package main
    16  
    17  import (
    18  	"flag"
    19  	"fmt"
    20  	"log"
    21  	"os"
    22  	"path/filepath"
    23  )
    24  
    25  var (
    26  	// This is the default. Setting it to false doesn't do anything in GNU
    27  	// or zsh pwd, because you just can't even set it to false.
    28  	_ = flag.Bool("L", true, "don't follow any symlinks")
    29  
    30  	physical = flag.Bool("P", false, "follow all symlinks (avoid all symlinks)")
    31  	cmd      = "pwd [-LP]"
    32  )
    33  
    34  func init() {
    35  	defUsage := flag.Usage
    36  	flag.Usage = func() {
    37  		os.Args[0] = cmd
    38  		defUsage()
    39  	}
    40  }
    41  
    42  func pwd(followSymlinks bool) (string, error) {
    43  	path, err := os.Getwd()
    44  	if err == nil && followSymlinks {
    45  		path, err = filepath.EvalSymlinks(path)
    46  	}
    47  	return path, err
    48  }
    49  
    50  func main() {
    51  	flag.Parse()
    52  
    53  	path, err := pwd(*physical)
    54  	if err != nil {
    55  		log.Fatalf("%v", err)
    56  	}
    57  	fmt.Println(path)
    58  }