home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!ogicse!uwm.edu!csd4.csd.uwm.edu!xepo
- From: xepo@csd4.csd.uwm.edu (Scott R Violet)
- Newsgroups: comp.sys.next.software
- Subject: Re: How can a program check to see if user is on console?
- Message-ID: <1k2i61INNfe6@uwm.edu>
- Date: 26 Jan 93 05:28:33 GMT
- Article-I.D.: uwm.1k2i61INNfe6
- References: <7466@ucsbcsl.ucsb.edu>
- Organization: Computing Services Division, University of Wisconsin - Milwaukee
- Lines: 51
- NNTP-Posting-Host: 129.89.7.4
-
- In article <7466@ucsbcsl.ucsb.edu> doug@foxtrot.ccmrc.ucsb.edu (Douglas Scott) writes:
- >I am writing some C code which needs to check to see if the user is logged
- >onto the console (i.e., any tty, as long as the user is sitting at the
- >console).
- >
- >What is the easiest way to check to see if this is true?
-
- This is the eisiest way that I have found to do it. You should be
- able to modify it to suit your needs as follows.
-
- -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-
- #include <stdio.h>
- #include <utmp.h>
-
- #define ATCONSOLE 1
- #define FAILED 2
- #define NOTATCONSOLE 3
-
- #define MAXNAME 12
-
- find_user(char *name)
- {
- struct utmp ubuf;
- FILE *fd;
-
- if ((fd = fopen("/etc/utmp", "r")) == NULL) {
- perror("Can't open /etc/utmp");
- return (FAILED);
- }
- while (fread((char *) &ubuf, sizeof ubuf, 1, fd) == 1) {
- if(strncmp(ubuf.ut_name, name, sizeof(ubuf.ut_name)) == 0 &&
- strcmp(ubuf.ut_line, "console") == 0)
- return ATCONSOLE;
- }
- return NOTATCONSOLE;
- }
-
- main() {
- char *loginName;
- loginName = (char *)malloc(sizeof(char) * MAXNAME);
- scanf("%s", loginName);
- if(find_user(loginName) == ATCONSOLE)
- printf("%s is at the console.\n", loginName);
- else
- printf("%s is not at the console.\n");
- }
-
- --
-
- -Scott Violet (xepo@csd4.csd.uwm.edu)
-