🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.sys.next.programmer
1 messages
1 total messages Started by Scott R Violet Tue, 26 Jan 1993 23:11
Checking if someone is at the console
#2377
Author: Scott R Violet
Date: Tue, 26 Jan 1993 23:11
47 lines
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)
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads