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

  1. /*
  2.  * Copyright 1989, 1990, 1991, 1992, 1993, 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.  * This software is provided on an AS-IS basis and the author makes
  12.  * no warrantee of any kind.
  13.  */
  14.  
  15. #include <sys/types.h>
  16. #include <utmp.h>
  17. #ifdef    SVR4
  18. #include <utmpx.h>
  19. #endif
  20. #include "pwd.h"
  21. #include <fcntl.h>
  22. #include <time.h>
  23. #ifndef    BSD
  24. #include <string.h>
  25. #include <memory.h>
  26. #else
  27. #include <strings.h>
  28. #define    strchr    index
  29. #define    strrchr    rindex
  30. #endif
  31. #include "config.h"
  32.  
  33. #ifndef    lint
  34. static    char    sccsid[] = "@(#)log.c    3.7    08:07:11    19 Jul 1993";
  35. #endif
  36.  
  37. #include "lastlog.h"
  38.  
  39. #ifndef    LASTLOG_FILE
  40. #ifdef    SVR4
  41. #define    LASTLOG_FILE    "/var/adm/lastlog"
  42. #else
  43. #define    LASTLOG_FILE    "/usr/adm/lastlog"
  44. #endif    /* SVR4 */
  45. #endif    /* LASTLOG_FILE */
  46.  
  47. extern    struct    utmp    utent;
  48. #ifdef    SVR4
  49. extern    struct    utmpx    utxent;
  50. #endif
  51. extern    struct    passwd    pwent;
  52. extern    struct    lastlog    lastlog;
  53. extern    char    **environ;
  54.  
  55. long    lseek ();
  56. time_t    time ();
  57.  
  58. /* 
  59.  * log - create lastlog entry
  60.  *
  61.  *    A "last login" entry is created for the user being logged in.  The
  62.  *    UID is extracted from the global (struct passwd) entry and the
  63.  *    TTY information is gotten from the (struct utmp).
  64.  */
  65.  
  66. void    log ()
  67. {
  68.     int    fd;
  69.     off_t    offset;
  70.     struct    lastlog    newlog;
  71.  
  72.     /*
  73.      * If the file does not exist, don't create it.
  74.      */
  75.  
  76.     if ((fd = open (LASTLOG_FILE, O_RDWR)) == -1)
  77.         return;
  78.  
  79.     /*
  80.      * The file is indexed by UID number.  Seek to the record
  81.      * for this UID.  Negaive UID's will create problems, but ...
  82.      */
  83.  
  84.     offset = pwent.pw_uid * sizeof lastlog;
  85.  
  86.     if (lseek (fd, offset, 0) != offset) {
  87.         (void) close (fd);
  88.         return;
  89.     }
  90.  
  91.     /*
  92.      * Read the old entry so we can tell the user when they last
  93.      * logged in.  Then construct the new entry and write it out
  94.      * the way we read the old one in.
  95.      */
  96.  
  97.     if (read (fd, (char *) &lastlog, sizeof lastlog) != sizeof lastlog)
  98. #ifndef    BSD
  99.         memset ((char *) &lastlog, sizeof lastlog, 0);
  100. #else
  101.         bzero ((char *) &lastlog, sizeof lastlog);
  102. #endif
  103.     newlog = lastlog;
  104.  
  105.     (void) time (&newlog.ll_time);
  106.     (void) strncpy (newlog.ll_line, utent.ut_line, sizeof newlog.ll_line);
  107. #ifdef    SVR4
  108.     (void) strncpy (newlog.ll_host, utxent.ut_host, sizeof newlog.ll_host);
  109. #endif
  110.     (void) lseek (fd, offset, 0);
  111.     (void) write (fd, (char *) &newlog, sizeof newlog);
  112.     (void) close (fd);
  113. }
  114.