home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume38 / shadow / part12 / lastlog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-14  |  3.1 KB  |  149 lines

  1. /*
  2.  * Copyright 1989, 1990, 1992, John F. Haugh II
  3.  * All rights reserved.
  4.  *
  5.  * Permission is granted to copy and create derivative works for any
  6.  * non-commercial purpose, provided this copyright notice is preserved
  7.  * in all copies of source code, or included in human readable form
  8.  * and conspicuously displayed on all copies of object code or
  9.  * distribution media.
  10.  *
  11.  * Modified from faillog.c by Philip Street
  12.  * Installation:  Maine National Guard
  13.  */
  14.  
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <stdio.h>
  18. #include "pwd.h"
  19. #include <time.h>
  20. #ifndef    BSD
  21. #include <string.h>
  22. #include <memory.h>
  23. #else
  24. #include <strings.h>
  25. #define    strchr    index
  26. #define    strrchr    rindex
  27. #endif
  28.  
  29. #define LASTFILE "/usr/adm/lastlog"
  30.  
  31. #include "config.h"
  32. #include "lastlog.h"
  33.  
  34. #ifndef    lint
  35. static    char    sccsid[] = "@(#)lastlog.c    3.2    10:30:33    18 Jul 1993";
  36. #endif
  37.  
  38. FILE    *lastlogfile;    /* lastlog file stream */
  39. off_t    user;        /* one single user, specified on command line */
  40. int    days;        /* number of days to consider for print command */
  41. time_t    seconds;    /* that number of days in seconds */
  42.  
  43. int    uflg;        /* set if user is a valid user id */
  44. int    tflg;        /* print is restricted to most recent days */
  45. struct    lastlog    lastlog; /* scratch structure to play with ... */
  46. struct    stat    statbuf; /* fstat buffer for file size */
  47.  
  48. int freadcode;
  49.  
  50. extern    int    optind;
  51. extern    char    *optarg;
  52. extern    char    *asctime ();
  53. extern    struct    passwd    *getpwuid ();
  54. extern    struct    passwd    *getpwnam ();
  55. extern    struct    passwd    *getpwent ();
  56. extern    struct    tm    *localtime ();
  57.  
  58. #define    DAY    (24L*3600L)
  59. #define    NOW    (time ((time_t *) 0))
  60.  
  61. main (argc, argv)
  62. int    argc;
  63. char    **argv;
  64. {
  65.     int    c;
  66.     struct    passwd    *pwent;
  67.  
  68.     if ((lastlogfile = fopen (LASTFILE,"r")) == (FILE *) 0) {
  69.         perror (LASTFILE);
  70.         exit (1);
  71.     }
  72.     while ((c = getopt (argc, argv, "u:t:")) != EOF) {
  73.         switch (c) {
  74.             case 'u':
  75.                 pwent = getpwnam (optarg);
  76.                 if (! pwent) {
  77.                     fprintf (stderr, "Unknown User: %s\n", optarg);
  78.                     exit (1);
  79.                 }
  80.                 uflg++;
  81.                 user = pwent->pw_uid;
  82.                 break;
  83.             case 't':
  84.                 days = atoi (optarg);
  85.                 seconds = days * DAY;
  86.                 tflg++;
  87.                 break;
  88.         }
  89.     }
  90.     print ();
  91.     fclose (lastlogfile);
  92.     exit (0);
  93.     /*NOTREACHED*/
  94. }
  95.  
  96. print ()
  97. {
  98.     int    uid;
  99.     off_t    offset;
  100.  
  101.     if (uflg) {
  102.         offset = user * sizeof lastlog;
  103.         fstat (fileno (lastlogfile), &statbuf);
  104.         if (offset >= statbuf.st_size)
  105.             return;
  106.  
  107.         fseek (lastlogfile, (off_t) user * sizeof lastlog, 0);
  108.         if (fread ((char *) &lastlog, sizeof lastlog, 1, lastlogfile) == 1)
  109.             print_one (user);
  110.         else
  111.             perror (LASTFILE);
  112.     } else {
  113.         for (uid = 0;
  114.             fread ((char *) &lastlog, sizeof lastlog, 1, lastlogfile) == 1;
  115.                 uid++) {
  116.  
  117.             if (tflg && NOW - lastlog.ll_time > seconds)
  118.                 continue;
  119.  
  120.             print_one (uid);
  121.         }
  122.     }
  123. }
  124.  
  125. print_one (uid)
  126. int    uid;
  127. {
  128.     static    int    once;
  129.     char    *cp;
  130.     struct    tm    *tm;
  131.     struct    passwd    *pwent;
  132.  
  133.     if (! once) {
  134.         printf ("Username\t\tPort\tLatest\n");
  135.         once++;
  136.     }
  137.     pwent = getpwuid (uid);
  138.     tm = localtime (&lastlog.ll_time);
  139.     cp = asctime (tm);
  140.     cp[24] = '\0';
  141.  
  142.     if(lastlog.ll_time == (time_t) 0)
  143.         cp = "**Never logged in**\0";
  144.  
  145.     if (pwent) {
  146.         printf ("%-16s\t%s\t%s\n",pwent->pw_name,lastlog.ll_line,cp);
  147.     }
  148. }
  149.