// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package os

import (
	
	
	
)

var getwdCache struct {
	sync.Mutex
	dir string
}

// Getwd returns an absolute path name corresponding to the
// current directory. If the current directory can be
// reached via multiple paths (due to symbolic links),
// Getwd may return any one of them.
//
// On Unix platforms, if the environment variable PWD
// provides an absolute name, and it is a name of the
// current directory, it is returned.
func () ( string,  error) {
	if runtime.GOOS == "windows" || runtime.GOOS == "plan9" {
		// Use syscall.Getwd directly for
		//   - plan9: see reasons in CL 89575;
		//   - windows: syscall implementation is sufficient,
		//     and we should not rely on $PWD.
		,  = syscall.Getwd()
		return , NewSyscallError("getwd", )
	}

	// Clumsy but widespread kludge:
	// if $PWD is set and matches ".", use it.
	var  FileInfo
	 = Getenv("PWD")
	if len() > 0 && [0] == '/' {
		,  = statNolog(".")
		if  != nil {
			return "", 
		}
		,  := statNolog()
		if  == nil && SameFile(, ) {
			return , nil
		}
		// If err is ENAMETOOLONG here, the syscall.Getwd below will
		// fail with the same error, too, but let's give it a try
		// anyway as the fallback code is much slower.
	}

	// If the operating system provides a Getwd call, use it.
	if syscall.ImplementsGetwd {
		,  = ignoringEINTR2(syscall.Getwd)
		// Linux returns ENAMETOOLONG if the result is too long.
		// Some BSD systems appear to return EINVAL.
		// FreeBSD systems appear to use ENOMEM
		// Solaris appears to use ERANGE.
		if  != syscall.ENAMETOOLONG &&  != syscall.EINVAL &&  != errERANGE &&  != errENOMEM {
			return , NewSyscallError("getwd", )
		}
	}

	// We're trying to find our way back to ".".
	if  == nil {
		,  = statNolog(".")
		if  != nil {
			return "", 
		}
	}
	// Apply same kludge but to cached dir instead of $PWD.
	getwdCache.Lock()
	 = getwdCache.dir
	getwdCache.Unlock()
	if len() > 0 {
		,  := statNolog()
		if  == nil && SameFile(, ) {
			return , nil
		}
	}

	// Root is a special case because it has no parent
	// and ends in a slash.
	,  := statNolog("/")
	if  != nil {
		// Can't stat root - no hope.
		return "", 
	}
	if SameFile(, ) {
		return "/", nil
	}

	// General algorithm: find name in parent
	// and then find name of parent. Each iteration
	// adds /name to the beginning of dir.
	 = ""
	for  := ".."; ;  = "../" +  {
		if len() >= 1024 { // Sanity check
			return "", NewSyscallError("getwd", syscall.ENAMETOOLONG)
		}
		,  := openDirNolog()
		if  != nil {
			return "", 
		}

		for {
			,  := .Readdirnames(100)
			if  != nil {
				.Close()
				// Readdirnames can return io.EOF or other error.
				// In any case, we're here because syscall.Getwd
				// is not implemented or failed with ENAMETOOLONG,
				// so return the most sensible error.
				if syscall.ImplementsGetwd {
					return "", NewSyscallError("getwd", syscall.ENAMETOOLONG)
				}
				return "", NewSyscallError("getwd", errENOSYS)
			}
			for ,  := range  {
				,  := lstatNolog( + "/" + )
				if SameFile(, ) {
					 = "/" +  + 
					goto 
				}
			}
		}

	:
		,  := .Stat()
		.Close()
		if  != nil {
			return "", 
		}
		if SameFile(, ) {
			break
		}
		// Set up for next round.
		 = 
	}

	// Save answer as hint to avoid the expensive path next time.
	getwdCache.Lock()
	getwdCache.dir = 
	getwdCache.Unlock()

	return , nil
}