home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / lib / util / getllog.bsd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-02-01  |  1.5 KB  |  89 lines

  1. #include "util.h"
  2. #include <pwd.h>
  3. #include <utmp.h>
  4. #include <lastlog.h>
  5.  
  6. /* get informtion about who is logged in */
  7.  
  8. extern char *blt();
  9.  
  10. LOCVAR char LLPATH[]  = "/usr/adm/lastlog";
  11. LOCVAR FILE *llogfp = NULL;
  12. LOCVAR struct utmp llogentry;
  13. LOCVAR int lloguid;       /* keep track of current uid */
  14.  
  15. int setllog()
  16. {
  17.     if( llogfp == NULL )
  18.         {
  19.         llogfp = fopen( LLPATH, "r" );
  20.         if ( llogfp == NULL )
  21.             return(NOTOK);
  22.         }
  23.     else
  24.         rewind( llogfp );
  25.  
  26.     lloguid = 0;
  27.     return (OK);
  28. }
  29.  
  30. endllog()
  31. {
  32.     if( llogfp != NULL ){
  33.         fclose( llogfp );
  34.         llogfp = NULL;
  35.     }
  36. }
  37.  
  38. struct utmp *
  39. getllog()
  40. {
  41.     union tmpunion
  42.     {
  43.         char        io[sizeof (struct lastlog)];
  44.         struct lastlog entry;
  45.     }       llogbuf;
  46.     struct passwd *pwdptr,
  47.               *getpwuid ();
  48.  
  49.     if (llogfp == NULL) {
  50.         if( (llogfp = fopen( LLPATH, "r" )) == NULL )
  51.             return(0);
  52.  
  53.         lloguid = 0;
  54.     }
  55.  
  56.     if (fread (llogbuf.io, sizeof (struct lastlog), 1, llogfp) != 1)
  57.         return(0);
  58.  
  59.     llogentry.ut_time = (long) llogbuf.entry.ll_time;
  60.     blt (llogbuf.entry.ll_line, llogentry.ut_line, 8);
  61.  
  62.     if ((pwdptr = getpwuid (lloguid)) == 0)
  63.         strcpy (llogentry.ut_name, "????");
  64.     else
  65.         blt (pwdptr -> pw_name, llogentry.ut_name, 8);
  66.  
  67.     lloguid++;
  68.     return(&llogentry);
  69. }
  70.  
  71. struct utmp *
  72. getllnam(name)
  73. char name[];
  74. {
  75.     struct passwd *pwdptr,
  76.               *gpwlnam ();
  77.  
  78.     if ((pwdptr = gpwlnam (name)) == 0)
  79.         return (0);
  80.  
  81.     if (setllog () == NOTOK)
  82.         return(NULL);
  83.  
  84.     lloguid = pwdptr->pw_uid;
  85.     fseek(llogfp, (long)pwdptr->pw_uid * sizeof (struct lastlog), 0);
  86.  
  87.     return (getllog ());
  88. }
  89.