OSDN Git Service

Update Go library to r60.
[pf3gnuchains/gcc-fork.git] / libgo / go / os / env_plan9.go
1 // Copyright 2011 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.
4
5 // Plan 9 environment variables.
6
7 package os
8
9 import "syscall"
10
11 // ENOENV is the Error indicating that an environment variable does not exist.
12 var ENOENV = NewError("no such environment variable")
13
14 // Getenverror retrieves the value of the environment variable named by the key.
15 // It returns the value and an error, if any.
16 func Getenverror(key string) (value string, err Error) {
17         if len(key) == 0 {
18                 return "", EINVAL
19         }
20         f, e := Open("/env/" + key)
21         if iserror(e) {
22                 return "", ENOENV
23         }
24         defer f.Close()
25
26         l, _ := f.Seek(0, 2)
27         f.Seek(0, 0)
28         buf := make([]byte, l)
29         n, e := f.Read(buf)
30         if iserror(e) {
31                 return "", ENOENV
32         }
33
34         if n > 0 && buf[n-1] == 0 {
35                 buf = buf[:n-1]
36         }
37         return string(buf), nil
38 }
39
40 // Getenv retrieves the value of the environment variable named by the key.
41 // It returns the value, which will be empty if the variable is not present.
42 func Getenv(key string) string {
43         v, _ := Getenverror(key)
44         return v
45 }
46
47 // Setenv sets the value of the environment variable named by the key.
48 // It returns an Error, if any.
49 func Setenv(key, value string) Error {
50         if len(key) == 0 {
51                 return EINVAL
52         }
53
54         f, e := Create("/env/" + key)
55         if iserror(e) {
56                 return e
57         }
58         defer f.Close()
59
60         _, e = f.Write([]byte(value))
61         return nil
62 }
63
64 // Clearenv deletes all environment variables.
65 func Clearenv() {
66         syscall.RawSyscall(syscall.SYS_RFORK, syscall.RFCENVG, 0, 0)
67 }
68
69 // Environ returns an array of strings representing the environment,
70 // in the form "key=value".
71 func Environ() []string {
72         env := make([]string, 0, 100)
73
74         f, e := Open("/env")
75         if iserror(e) {
76                 panic(e)
77         }
78         defer f.Close()
79
80         names, e := f.Readdirnames(-1)
81         if iserror(e) {
82                 panic(e)
83         }
84
85         for _, k := range names {
86                 if v, e := Getenverror(k); !iserror(e) {
87                         env = append(env, k+"="+v)
88                 }
89         }
90         return env[0:len(env)]
91 }
92
93 // TempDir returns the default directory to use for temporary files.
94 func TempDir() string {
95         return "/tmp"
96 }