which is my login terminal

Jay Batson jay at isis.UUCP
Fri Jun 27 01:32:30 AEST 1986


In article <34 at gec-rl-hrc.co.uk> nwh at gec-rl-hrc.co.uk (Nigel Holder Marconi) writes:
>
>   I have an application that needs to know whether I am logged
>on at the console to decide whether I can paint all over the
>sun bit-mapped screen.

This is a simplistic solution, and probably does more work than necessary.
Somebody will probably enlighten us as to another way, but this _should_
also work.  And I've been flamed about using my _V7_ info (since that's
what I'm running) to answer net questions, but I thought I'd
chime in anyway....

The function below will simply check if the minor device numbers of
the console and the tty device associated with the current process.
The minor device number should probably be the same if they are the
same terminal.  One _IMPORTANT_ caveat - I don't know enough about a Sun to
say FOR SURE that this will work on that machine.  If windows are
implemented as separate logical devices, I'm certain it won't.
To be honest, I'm not sure that the minor numbers would be the same on
all machines - I know it is on mine.  I've never looked into the
matter extensively.

Look at stat(2), ttyname/slot(2), and types(5), and try this:
(for you purists, I know I'm not doing error checking...)
(I'm not certain that /etc/ttys is used/present in SV, so this probably
will only work on 4.2 and similar.)

___cut here___
/* returns 0 if on console, -1 if not. */

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
on_console()
{
	struct stat somevar1, somevar2;
	char ttydata[16], ttyline[16]; /* size is somewhat arbitrary here... */
	int slotnum; /* for the line number in /etc/ttys indicating the
			tty line our process is associated with */
	FILE *ttydescr; /* to read /etc/ttys */
	
		/* get the /etc/ttys line we are associated with */
	slotnum = ttyslot();
		/* actually get the /etc/ttys entry - this seems redundant -
			there ought to be another way... */
	ttydescr = fopen("/etc/ttys", "r");
	for ( ; slotnum > 0 ; slotnum--) fgets(ttydata, 16, ttydescr);
	fclose(ttydescr);
		/* zap nl */
	ttydata[(strlen(ttydata) - 1)] = '\0';
		/* need "/dev/", and need to zap 2 char ttys line hdr */
	sprintf(ttyline, "/dev/%s", &ttydata[2]);
		/* we want the minor device numbers of the console, and
			of our device */
	stat ("/dev/console", &somevar1);
	stat (ttyline, &somevar2);
		/* test if they are the same and return appropriate values */
	if (minor(somevar1.st_rdev) == minor(somevar2.st_rdev))
		return(0);
	else
		return(-1);
}

Well, now that I've said that, it seems like a lot of trouble, and I'm
not sure of it's portability, although lint only objects to my lack of
error checking.

Oh well, it was an interesting 10 minutes.  LET THE FLAMES BEGIN!!!
--------

"Stop it!! Stop it now.  This is getting silly again, and this silliness
has _got_ to stop.  Go on to the next sketch.  Go on.  Turn this camera o    "

Jay Batson
{seismo,hplabs}!hao!isis!jay



More information about the Comp.unix mailing list