OSDN Git Service

Update Go library to r60.
[pf3gnuchains/gcc-fork.git] / libgo / go / exp / gui / x11 / auth.go
1 // Copyright 2009 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 package x11
6
7 import (
8         "bufio"
9         "io"
10         "os"
11 )
12
13 // readU16BE reads a big-endian uint16 from r, using b as a scratch buffer.
14 func readU16BE(r io.Reader, b []byte) (uint16, os.Error) {
15         _, err := io.ReadFull(r, b[0:2])
16         if err != nil {
17                 return 0, err
18         }
19         return uint16(b[0])<<8 + uint16(b[1]), nil
20 }
21
22 // readStr reads a length-prefixed string from r, using b as a scratch buffer.
23 func readStr(r io.Reader, b []byte) (string, os.Error) {
24         n, err := readU16BE(r, b)
25         if err != nil {
26                 return "", err
27         }
28         if int(n) > len(b) {
29                 return "", os.NewError("Xauthority entry too long for buffer")
30         }
31         _, err = io.ReadFull(r, b[0:n])
32         if err != nil {
33                 return "", err
34         }
35         return string(b[0:n]), nil
36 }
37
38 // readAuth reads the X authority file and returns the name/data pair for the display.
39 // displayStr is the "12" out of a $DISPLAY like ":12.0".
40 func readAuth(displayStr string) (name, data string, err os.Error) {
41         // b is a scratch buffer to use and should be at least 256 bytes long
42         // (i.e. it should be able to hold a hostname).
43         var b [256]byte
44         // As per /usr/include/X11/Xauth.h.
45         const familyLocal = 256
46
47         fn := os.Getenv("XAUTHORITY")
48         if fn == "" {
49                 home := os.Getenv("HOME")
50                 if home == "" {
51                         err = os.NewError("Xauthority not found: $XAUTHORITY, $HOME not set")
52                         return
53                 }
54                 fn = home + "/.Xauthority"
55         }
56         r, err := os.Open(fn)
57         if err != nil {
58                 return
59         }
60         defer r.Close()
61         br := bufio.NewReader(r)
62
63         hostname, err := os.Hostname()
64         if err != nil {
65                 return
66         }
67         for {
68                 family, err := readU16BE(br, b[0:2])
69                 if err != nil {
70                         return
71                 }
72                 addr, err := readStr(br, b[0:])
73                 if err != nil {
74                         return
75                 }
76                 disp, err := readStr(br, b[0:])
77                 if err != nil {
78                         return
79                 }
80                 name0, err := readStr(br, b[0:])
81                 if err != nil {
82                         return
83                 }
84                 data0, err := readStr(br, b[0:])
85                 if err != nil {
86                         return
87                 }
88                 if family == familyLocal && addr == hostname && disp == displayStr {
89                         return name0, data0, nil
90                 }
91         }
92         panic("unreachable")
93 }