home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / sys / next / software / 3636 < prev    next >
Encoding:
Text File  |  1993-01-26  |  1.7 KB  |  64 lines

  1. Path: sparky!uunet!ogicse!uwm.edu!csd4.csd.uwm.edu!xepo
  2. From: xepo@csd4.csd.uwm.edu (Scott R Violet)
  3. Newsgroups: comp.sys.next.software
  4. Subject: Re: How can a program check to see if user is on console?
  5. Message-ID: <1k2i61INNfe6@uwm.edu>
  6. Date: 26 Jan 93 05:28:33 GMT
  7. Article-I.D.: uwm.1k2i61INNfe6
  8. References: <7466@ucsbcsl.ucsb.edu>
  9. Organization: Computing Services Division, University of Wisconsin - Milwaukee
  10. Lines: 51
  11. NNTP-Posting-Host: 129.89.7.4
  12.  
  13. In article <7466@ucsbcsl.ucsb.edu> doug@foxtrot.ccmrc.ucsb.edu (Douglas Scott) writes:
  14. >I am writing some C code which needs to check to see if the user is logged
  15. >onto the console (i.e., any tty, as long as the user is sitting at the
  16. >console).
  17. >
  18. >What is the easiest way to check to see if this is true?
  19.  
  20. This is the eisiest way that I have found to do it.  You should be
  21. able to modify it to suit your needs as follows.
  22.  
  23. -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  24.  
  25. #include <stdio.h>
  26. #include <utmp.h>
  27.  
  28. #define        ATCONSOLE    1
  29. #define        FAILED        2
  30. #define        NOTATCONSOLE    3
  31.  
  32. #define        MAXNAME        12
  33.  
  34. find_user(char *name)
  35. {
  36.   struct utmp ubuf;
  37.   FILE *fd;
  38.  
  39.   if ((fd = fopen("/etc/utmp", "r")) == NULL) {
  40.     perror("Can't open /etc/utmp");
  41.     return (FAILED);
  42.   }
  43.   while (fread((char *) &ubuf, sizeof ubuf, 1, fd) == 1) {
  44.     if(strncmp(ubuf.ut_name, name, sizeof(ubuf.ut_name)) == 0 &&
  45.        strcmp(ubuf.ut_line, "console") == 0)
  46.       return ATCONSOLE;
  47.   }
  48.   return NOTATCONSOLE;
  49. }
  50.  
  51. main() {
  52.   char *loginName;
  53.   loginName = (char *)malloc(sizeof(char) * MAXNAME);
  54.   scanf("%s", loginName);
  55.   if(find_user(loginName) == ATCONSOLE)
  56.     printf("%s is at the console.\n", loginName);
  57.   else
  58.     printf("%s is not at the console.\n");
  59. }
  60.  
  61. -- 
  62.  
  63.         -Scott Violet (xepo@csd4.csd.uwm.edu)
  64.