home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!dtix!darwin.sura.net!haven.umd.edu!mimsy!kong!arensb
- From: arensb@kong.gsfc.nasa.gov (Andrew Arensburger - RMS)
- Newsgroups: comp.sys.sun.admin
- Subject: Re: auto set display variable on X terms
- Message-ID: <1992Aug21.204844.3186@kong.gsfc.nasa.gov>
- Date: 21 Aug 92 20:48:44 GMT
- References: <1992Aug20.144001.1759@trantor.harris-atd.com>
- Organization: Goddard Space Flight Center
- Lines: 72
-
- ccarpent@dw3f.ess.harris.com (Craig Carpenter) writes:
- >I am looking for the easy way to set the DISPLAY variable when a user
- >signs in. We have some
- >X terminals and they login to the Sparc and have to set the display
- >variable themselves,
- >what is the easy way to find out from the login process what terminal
- >they are signing in from.
-
- This is my solution. It does much the same thing as the other
- posters' solutions. This also started out as a shell script, but at
- some point I decided that I wanted the speed of C.
- It runs on Sparcs under SunOS 4.1.1 . For anything else, you're
- on your own.
-
- --
- Andrew Arensburger | See, Buzz? It's really fun to be psychotic!
- arensb@kong.gsfc.nasa.gov | -- MST3K
- ...!uunet!dftsrv!kong!arensb |
-
- ----- begin included file ----------------------------------------
- #include <stdio.h>
- #include <utmp.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <fcntl.h>
-
- extern char *strchr();
-
- main()
- {
- struct utmp entry;
- int ufd; /* /etc/utmp file descriptor */
- int slot;
-
- /* Get the /etc/utmp entry associated with this login session */
- if ((slot = ttyslot()) == 0)
- {
- fprintf(stderr, "You don't have a tty!\n");
- exit(1);
- }
-
- /* Open /etc/utmp so we can find the correct entry */
- if ((ufd = open("/etc/utmp",O_RDONLY)) < 0)
- {
- fprintf(stderr, "Can't open /etc/utmp\n");
- exit(1);
- }
-
- /* Go to the appropriate slot */
- if (lseek(ufd,slot * sizeof(struct utmp),SEEK_SET) < 0)
- {
- fprintf(stderr, "Can't lseek to slot %d\n", slot+1);
- exit(1);
- }
-
- /* Read the entry */
- if (read(ufd,&entry,sizeof(struct utmp)) == sizeof(struct utmp))
- {
- char *dot;
-
- /* Retain only the first component of the hostname,
- * by cutting off everything after, and including, the
- * first dot.
- */
- if ((dot = strchr(entry.ut_host,'.')) != NULL)
- *dot = '\0';
- printf("%s\n", entry.ut_host);
- exit(0);
- }
- }
- ----- end included file ------------------------------------------
-