home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume2 / lastlog-sys5 < prev    next >
Encoding:
Internet Message Format  |  1991-08-07  |  4.9 KB

  1. From: lenny@icus.UUCP (Lenny Tropiano)
  2. Newsgroups: comp.sources.misc
  3. Subject: v02i065: lastlogin.c source (was: Re: SysV lastlog)
  4. Message-ID: <7468@ncoast.UUCP>
  5. Date: 5 Mar 88 22:25:45 GMT
  6. Approved: allbery@ncoast.UUCP
  7.  
  8. Comp.sources.misc: Volume 2, Issue 65
  9. Submitted-By: "Lenny Tropiano" <lenny@icus.UUCP>
  10. Archive-Name: lastlog-sys5
  11.  
  12. In article <9766@shemp.CS.UCLA.EDU> jimmy@pic.ucla.edu (Jim Gottlieb) writes:
  13. |>I notice that SystemV (at least the SystemV on my AT&T 3B1) doesn't
  14. |>have a /usr/adm/lastlog or any program that performs a similar
  15. |>function.  This prevents the use of a last(1) command and the ability
  16. |>to see the last login in the finger program.  I could keep /etc/wtmp
  17. |>around forever, but that gets too big.  Any suggestions/solutions?
  18. |>
  19. |>Thanks...
  20.  
  21. Here's my implementation to lastlogin ... 
  22.  
  23. --- cut here --- --- cut here --- --- cut here --- --- cut here ---
  24.  
  25. /***************************************************************************
  26.  * Program:  lastlogin             (c)1987 ICUS Computer Group        *
  27.  * By:       Lenny Tropiano        ...{ihnp4,mtune}!icus!lenny        *
  28.  *                                                                         *
  29.  * Program intent:   This will allow programs like 'finger' and 'last' to  *
  30.  *                   lookup in the file /usr/adm/lastlogin.log and see     *
  31.  *                   when a particular user has logged-in.   This saves    *
  32.  *                   the necessity to keep /etc/wtmp around for a long     *
  33.  *                   period of time.                                       *
  34.  *                                                                         *
  35.  *                   This program can be used/modified and redistributed   *
  36.  *                   I declare it PUBLIC DOMAIN.  Please give me credit    *
  37.  *                   when credit is due.                                   *
  38.  *                                                                         *
  39.  *      AT&T 3B1 compiling instructions for shared-libaries:               *
  40.  *                                                                         *
  41.  *      $ cc -c -O lastlogin.c                                             *
  42.  *      $ ld -s -o lastlogin lastlogin.o /lib/shlib.ifile /lib/crt0s.o     *
  43.  *      $ mv lastlogin /etc                                                *
  44.  *      $ su                                                               *
  45.  *      Password:                                                          *
  46.  *      # chown adm /etc/lastlogin /usr/adm                                *
  47.  *      # chgrp adm /etc/lastlogin /usr/adm                                *
  48.  *      # chmod 4755 /etc/lastlogin                                        *
  49.  *                                                                         *
  50.  *      Place a call to /etc/lastlogin in your /etc/localprofile           *
  51.  *      to be run on all user logins.                                      *
  52.  ***************************************************************************/
  53.  
  54.               /* Print the last login time and record the new time */
  55.  
  56. #include <stdio.h>
  57. #include <sys/types.h>
  58. #include <fcntl.h>
  59. #include <string.h>
  60. #include <time.h>
  61. #include <utmp.h>
  62.  
  63. #define    LOGFILE    "/usr/adm/lastlogin.log"
  64.  
  65. main()
  66. {
  67.     struct utmp *utent, *getutent();
  68.     int    fd;
  69.     long   hrs, min, sec;
  70.     struct lastlog {
  71.        char ll_line[8];
  72.        time_t ll_time;
  73.     } ll;
  74.  
  75.     if (access(LOGFILE, 0) == -1) {
  76.        if ((fd = creat(LOGFILE,0644)) == -1) {
  77.         fprintf(stderr,"Cannot create file %s: ", LOGFILE);
  78.         perror("creat()");
  79.         exit(1);
  80.        }
  81.     } else {
  82.        if ((fd = open(LOGFILE,O_RDWR)) == -1) {
  83.         fprintf(stderr,"Cannot open file %s: ", LOGFILE);
  84.         perror("open()");
  85.         exit(1);
  86.        }
  87.     }
  88.  
  89.     if (lseek(fd, (long)(getuid()*sizeof(struct lastlog)), 0) == -1) {
  90.         fprintf(stderr,"Cannot position file %s: ", LOGFILE);
  91.         perror("lseek()");
  92.         exit(1);
  93.     }
  94.  
  95.     if (read(fd, (char *) &ll, sizeof ll) == sizeof ll &&
  96.         ll.ll_time != 0L) {
  97.         printf("Last login: %.*s on %.*s\n" , 19
  98.             , (char *) ctime(&ll.ll_time) , sizeof(ll.ll_line)
  99.             , ll.ll_line);
  100.     } else  printf("Last login: [No Login information on record]\n");
  101.  
  102.     sprintf(ll.ll_line, "%.8s", strrchr(ttyname(0), '/')+1);
  103.     setutent();
  104.     while ((utent = getutent()) != NULL) 
  105.        if (strcmp(utent->ut_line, ll.ll_line) == 0)
  106.         break;
  107.  
  108.     if (utent == NULL) {
  109.         fprintf(stderr,"Cannot locate utmp entry for tty\n");
  110.         exit(1);
  111.     }
  112.     ll.ll_time = utent->ut_time;
  113.     endutent();
  114.  
  115.     lseek(fd, (long)(getuid()*sizeof(struct lastlog)), 0);
  116.     write(fd, (char *) &ll, sizeof ll);
  117.     close(fd);
  118.  
  119.     exit(0);
  120. }
  121.  
  122.  
  123. -- 
  124. US MAIL  : Lenny Tropiano, ICUS Computer Group        IIIII  CCC U   U  SSS
  125.            PO Box 1                                     I   C    U   U S
  126.        Islip Terrace, New York  11752               I   C    U   U  SS 
  127. PHONE    : (516) 968-8576 [H] (516) 582-5525 [W]        I   C    U   U    S
  128. TELEX    : 154232428 [ICUS]                           IIIII  CCC  UUU  SSS 
  129. AT&T MAIL: ...attmail!icus!lenny  
  130. UUCP     : ...{mtune, ihnp4, boulder, talcott, sbcs, bc-cis}!icus!lenny 
  131.