// Copyright 2010 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.

//go:build unix || (js && wasm) || plan9 || wasip1

// Unix environment variables.

package syscall

import (
	
	
)

var (
	// envOnce guards initialization by copyenv, which populates env.
	envOnce sync.Once

	// envLock guards env and envs.
	envLock sync.RWMutex

	// env maps from an environment variable to its first occurrence in envs.
	env map[string]int

	// envs is provided by the runtime. elements are expected to
	// be of the form "key=value". An empty string means deleted
	// (or a duplicate to be ignored).
	envs []string = runtime_envs()
)

func runtime_envs() []string // in package runtime

func copyenv() {
	env = make(map[string]int)
	for ,  := range envs {
		for  := 0;  < len(); ++ {
			if [] == '=' {
				 := [:]
				if ,  := env[]; ! {
					env[] =  // first mention of key
				} else {
					// Clear duplicate keys. This permits Unsetenv to
					// safely delete only the first item without
					// worrying about unshadowing a later one,
					// which might be a security problem.
					envs[] = ""
				}
				break
			}
		}
	}
}

func ( string) error {
	envOnce.Do(copyenv)

	envLock.Lock()
	defer envLock.Unlock()

	if ,  := env[];  {
		envs[] = ""
		delete(env, )
	}
	runtimeUnsetenv()
	return nil
}

func ( string) ( string,  bool) {
	envOnce.Do(copyenv)
	if len() == 0 {
		return "", false
	}

	envLock.RLock()
	defer envLock.RUnlock()

	,  := env[]
	if ! {
		return "", false
	}
	 := envs[]
	for  := 0;  < len(); ++ {
		if [] == '=' {
			return [+1:], true
		}
	}
	return "", false
}

func (,  string) error {
	envOnce.Do(copyenv)
	if len() == 0 {
		return EINVAL
	}
	for  := 0;  < len(); ++ {
		if [] == '=' || [] == 0 {
			return EINVAL
		}
	}
	// On Plan 9, null is used as a separator, eg in $path.
	if runtime.GOOS != "plan9" {
		for  := 0;  < len(); ++ {
			if [] == 0 {
				return EINVAL
			}
		}
	}

	envLock.Lock()
	defer envLock.Unlock()

	,  := env[]
	 :=  + "=" + 
	if  {
		envs[] = 
	} else {
		 = len(envs)
		envs = append(envs, )
	}
	env[] = 
	runtimeSetenv(, )
	return nil
}

func () {
	envOnce.Do(copyenv) // prevent copyenv in Getenv/Setenv

	envLock.Lock()
	defer envLock.Unlock()

	for  := range env {
		runtimeUnsetenv()
	}
	env = make(map[string]int)
	envs = []string{}
}

func () []string {
	envOnce.Do(copyenv)
	envLock.RLock()
	defer envLock.RUnlock()
	 := make([]string, 0, len(envs))
	for ,  := range envs {
		if  != "" {
			 = append(, )
		}
	}
	return 
}