2 * ux_x11.c: fetch local auth data for X forwarding.
9 void platform_get_x11_auth(char *display, int *protocol,
10 unsigned char *data, int *datalen)
14 int maxsize = *datalen;
17 command = dupprintf("xauth list %s 2>/dev/null", display);
18 fp = popen(command, "r");
22 return; /* assume no auth */
24 localbuf = smalloc(maxsize);
28 * Read a line from stdin, and attempt to parse it into a
29 * display name (ignored), auth protocol, and auth string.
31 int c, i, hexdigit, proto;
34 /* Skip the display name. */
35 while (c = getc(fp), c != EOF && c != '\n' && !isspace(c));
37 if (c == '\n') continue;
39 /* Skip white space. */
40 while (c != EOF && c != '\n' && isspace(c))
43 if (c == '\n') continue;
45 /* Read the auth protocol name, and see if it matches any we
48 while (c != EOF && c != '\n' && !isspace(c)) {
49 if (i < lenof(protoname)-1) protoname[i++] = c;
54 for (i = X11_NO_AUTH; ++i < X11_NAUTHS ;) {
55 if (!strcmp(protoname, x11_authnames[i]))
58 if (i >= X11_NAUTHS || i <= proto) {
59 /* Unrecognised protocol name, or a worse one than we already have.
61 while (c != EOF && c != '\n')
67 /* Skip white space. */
68 while (c != EOF && c != '\n' && isspace(c))
71 if (c == '\n') continue;
74 * Now grab pairs of hex digits and shove them into `data'.
78 while (c != EOF && c != '\n') {
80 if (c >= 'A' && c <= 'F')
81 hexval = c + 10 - 'A';
82 if (c >= 'a' && c <= 'f')
83 hexval = c + 10 - 'a';
84 if (c >= '0' && c <= '9')
88 hexdigit = (hexdigit << 4) + hexval;
90 localbuf[i++] = hexdigit;
100 memcpy(data, localbuf, i);
102 /* Nonetheless, continue looping round; we might find a better one. */