home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume02 / lstlgsys.5 < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  5.4 KB

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