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.
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])
19 return uint16(b[0])<<8 + uint16(b[1]), nil
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)
29 return "", os.NewError("Xauthority entry too long for buffer")
31 _, err = io.ReadFull(r, b[0:n])
35 return string(b[0:n]), nil
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).
44 // As per /usr/include/X11/Xauth.h.
45 const familyLocal = 256
47 fn := os.Getenv("XAUTHORITY")
49 home := os.Getenv("HOME")
51 err = os.NewError("Xauthority not found: $XAUTHORITY, $HOME not set")
54 fn = home + "/.Xauthority"
61 br := bufio.NewReader(r)
63 hostname, err := os.Hostname()
68 family, err := readU16BE(br, b[0:2])
72 addr, err := readStr(br, b[0:])
76 disp, err := readStr(br, b[0:])
80 name0, err := readStr(br, b[0:])
84 data0, err := readStr(br, b[0:])
88 if family == familyLocal && addr == hostname && disp == displayStr {
89 return name0, data0, nil