Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!caip!nike!ucbcad!ucbvax!hplabs!hao!nbires!isis!jay From: jay@isis.UUCP (Jay Batson) Newsgroups: net.unix-wizards,net.unix,net.lang.c Subject: Re: which is my login terminal Message-ID: <901@isis.UUCP> Date: Thu, 26-Jun-86 11:32:30 EDT Article-I.D.: isis.901 Posted: Thu Jun 26 11:32:30 1986 Date-Received: Sun, 29-Jun-86 04:57:56 EDT References: <34@gec-rl-hrc.co.uk> Reply-To: jay@isis.UUCP (Jay Batson) Organization: University of Denver Math and Computer Science Lines: 75 Keywords: sun, control terminal Xref: watmath net.unix-wizards:18631 net.unix:8433 net.lang.c:9624 Summary: try stat(2) In article <34@gec-rl-hrc.co.uk> nwh@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 #include #include 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