[comp.windows.x] test: am I running X10 or X11?

swick@ATHENA.MIT.EDU (Ralph R. Swick) (02/21/88)

Here's the hack we've been using at Athena.
---------
#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create:
#	xversion.man
#	xversion.c
# This archive created: Sun Feb 21 07:12:25 1988
export PATH; PATH=/bin:/usr/bin:$PATH
if test -f 'xversion.man'
then
	echo shar: "will not over-write existing file 'xversion.man'"
else
cat << \SHAR_EOF > 'xversion.man'
.\" $Header: xversion.man,v 1.2 87/09/14 14:40:13 toddb Exp $
.TH XVERSION "5 October 1987" "X Version 11"
.UC 4
.SH NAME
xversion \- determine which version of X is running
.SH SYNOPSIS
.B xversion
[ -r ]
.SH DESCRIPTION
.I Xversion
prints to standard output the text '10' or '11' according
to the version of the X Window System currently running on the
workstation.
.PP
If
.I xversion
cannot make a connection to the server, it prints the text '?unknown'.
.PP
.I xversion
is intended principally as a tool to be used within shell scripts,
such as
.I xswitch.
.SH BUGS
The display name is hard-coded as 'unix:0'; i.e. the value of the
DISPLAY environment variable is ignored.
SHAR_EOF
fi
if test -f 'xversion.c'
then
	echo shar: "will not over-write existing file 'xversion.c'"
else
cat << \SHAR_EOF > 'xversion.c'
#include <X11/copyright.h>
/* Copyright    Massachusetts Institute of Technology    1985	*/

/*
 * stolen from X10/XOpenDisplay
 *
 * determine which server is likely to be running by attempting to
 *
 */
 
#include <X11/Xlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
 
#define X_UNIX_PATH	"/tmp/X0"	/* X10 UNIX socket! */

main()
{
    struct sockaddr_un addr;		/* UNIX socket address */
    int addrlen;
    int fd;
    Display *dpy;

    addr.sun_family = AF_UNIX;
    strcpy(addr.sun_path, X_UNIX_PATH);
    addrlen = strlen(addr.sun_path) + 2;

    if ((fd = socket(addr.sun_family, SOCK_STREAM, 0)) < 0) {
        /* Socket call failed! */
        /* errno set by system call. */
        perror( "couldn't open socket" );
        exit( 1 );
    }
 
    /* try connecting to X10 first */
    if (connect(fd, &addr, addrlen) == 0) {
        close(fd);
        printf( "10\n" );
	exit( 0 );
    }

    /* X11 requires connection information */
    if ((dpy = XOpenDisplay( "unix:0" )) != NULL) {
        printf( "11\n" );
	XCloseDisplay( dpy );
	exit( 0 );
    }

    printf( "?unknown\n" );
    exit( 1 );
}
SHAR_EOF
fi
exit 0
#	End of shell archive

karlton@decwrl.dec.com (Philip Karlton) (02/22/88)

I noticed that the program uses the constant string "unix:0" to attempt to
connect to the local X11 server. You may be better served by using just ":0".
If the bug, excuse me, feature, in Xlib that prevents a node name from being
"unix" is ever fixed, the former will stop working.

PK