1 // Copyright 2013 The Go 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.
12 // envForDir returns a copy of the environment
13 // suitable for running in the given directory.
14 // The environment is the current process's environment
15 // but with an updated $PWD, so that an os.Getwd in the
16 // child will be faster.
17 func envForDir(dir string) []string {
19 // Internally we only use rooted paths, so dir is rooted.
20 // Even if dir is not rooted, no harm done.
21 return mergeEnvLists([]string{"PWD=" + dir}, env)
24 // mergeEnvLists merges the two environment lists such that
25 // variables with the same name in "in" replace those in "out".
26 func mergeEnvLists(in, out []string) []string {
28 for _, inkv := range in {
29 k := strings.SplitAfterN(inkv, "=", 2)[0]
30 for i, outkv := range out {
31 if strings.HasPrefix(outkv, k) {
36 out = append(out, inkv)