home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / sys / next / programm / 8332 < prev    next >
Encoding:
Text File  |  1993-01-28  |  1.5 KB  |  60 lines

  1. Path: sparky!uunet!think.com!spool.mu.edu!uwm.edu!csd4.csd.uwm.edu!xepo
  2. From: xepo@csd4.csd.uwm.edu (Scott R Violet)
  3. Newsgroups: comp.sys.next.programmer
  4. Subject: Checking if someone is at the console
  5. Date: 26 Jan 1993 23:11:49 GMT
  6. Organization: Computing Services Division, University of Wisconsin - Milwaukee
  7. Lines: 47
  8. Distribution: usa
  9. Message-ID: <1k4gflINNpl9@uwm.edu>
  10. NNTP-Posting-Host: 129.89.7.4
  11. Originator: xepo@csd4.csd.uwm.edu
  12.  
  13.  
  14. Hi,
  15.     I noticed that a few people have asked for this code over the
  16. years.  So, below is a short snippet that will check to see if a given
  17. user is at the console.  Just compile it, and then run it, and then
  18. type in the users name.  It probably isn't the most elegant way to do
  19. it, but it works.
  20.  
  21.  
  22. #include <stdio.h>
  23. #include <utmp.h>
  24.  
  25. #define        ATCONSOLE    1
  26. #define        FAILED        2
  27. #define        NOTATCONSOLE    3
  28.  
  29. #define        MAXNAME        12
  30.  
  31. find_user(char *name)
  32. {
  33.   struct utmp ubuf;
  34.   FILE *fd;
  35.  
  36.   if ((fd = fopen("/etc/utmp", "r")) == NULL) {
  37.     perror("Can't open /etc/utmp");
  38.     return (FAILED);
  39.   }
  40.   while (fread((char *) &ubuf, sizeof ubuf, 1, fd) == 1) {
  41.     if(strncmp(ubuf.ut_name, name, sizeof(ubuf.ut_name)) == 0 &&
  42.        strcmp(ubuf.ut_line, "console") == 0)
  43.       return ATCONSOLE;
  44.   }
  45.   return NOTATCONSOLE;
  46. }
  47.  
  48. main() {
  49.   char *loginName;
  50.   loginName = (char *)malloc(sizeof(char) * MAXNAME);
  51.   scanf("%s", loginName);
  52.   if(find_user(loginName) == ATCONSOLE)
  53.     printf("%s is at the console.\n", loginName);
  54.   else
  55.     printf("%s is not at the console.\n");
  56. }
  57. -- 
  58.  
  59.         -Scott Violet (xepo@csd4.csd.uwm.edu)
  60.