Article View: comp.sys.next.programmer
Article #2377Checking if someone is at the console
From: Scott R Violet
Date: Tue, 26 Jan 1993 23:11
Date: Tue, 26 Jan 1993 23:11
47 lines
1609 bytes
1609 bytes
Hi,
I noticed that a few people have asked for this code over the
years. So, below is a short snippet that will check to see if a given
user is at the console. Just compile it, and then run it, and then
type in the users name. It probably isn't the most elegant way to do
it, but it works.
#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)
Technical Details
Message-ID:
<1k4gflINNpl9@uwm.edu>Path:
arc1-ca.pugleaf.net!sparky!uunet!think.com!spool.mu.edu!uwm.edu!csd4.csd.uwm.edu!xepoShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!think.com!spool.mu.edu!uwm.edu!csd4.csd.uwm.edu!xepo From: xepo@csd4.csd.uwm.edu (Scott R Violet) Newsgroups: comp.sys.next.programmer Subject: Checking if someone is at the console Date: 26 Jan 1993 23:11:49 GMT Organization: Computing Services Division, University of Wisconsin - Milwaukee Lines: 47 Distribution: usa Message-ID: <1k4gflINNpl9@uwm.edu> NNTP-Posting-Host: 129.89.7.4 Originator: xepo@csd4.csd.uwm.edu