home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / unix_c / sysadmin / lstlgsys.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-21  |  4.8 KB  |  128 lines

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