home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume6 / shadow-2.pt3 / utmp.c < prev    next >
C/C++ Source or Header  |  1989-02-03  |  1KB  |  70 lines

  1. #include <sys/types.h>
  2. #include <utmp.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include "config.h"
  6.  
  7. extern    struct    utmp    utent;
  8. extern    char    name[];
  9.  
  10. struct    utmp    *getutent ();
  11. void    setutent ();
  12. void    endutent ();
  13. void    pututline ();
  14. char    *memset ();
  15. time_t    time ();
  16.  
  17. void    checkutmp ()
  18. {
  19.     struct    utmp    *ut;
  20. #ifndef    NDEBUG
  21.     int    pid = getppid ();
  22. #else
  23.     int    pid = getpid ();
  24. #endif
  25.     setutent ();
  26.  
  27.     while (ut = getutent ())
  28.         if (ut->ut_pid == pid)
  29.             break;
  30.  
  31.     if (ut)
  32.         utent = *ut;
  33.  
  34.     endutent ();
  35.  
  36.     if (ut && utent.ut_pid == pid)
  37.         return;
  38.  
  39.     puts ("No utmp entry.  You must exec \"login\" from the lowest level \"sh\"");
  40.     exit (1);
  41. }
  42.  
  43. void    setutmp ()
  44. {
  45.     FILE    *wtmp;
  46.     char    tty[sizeof utent.ut_line + 1];
  47.     char    *line;
  48.  
  49.     setutent ();
  50.  
  51.     (void) strncpy (utent.ut_user, name, sizeof utent.ut_user);
  52.  
  53.     utent.ut_type = USER_PROCESS;
  54.  
  55.     if (line = strrchr (utent.ut_line, '/')) {
  56.         (void) strcpy (tty, line + 1);
  57.         (void) memset (utent.ut_line, '\0', sizeof utent.ut_line);
  58.         (void) strcpy (utent.ut_line, tty);
  59.     }
  60.     (void) time (&utent.ut_time);
  61.  
  62.     pututline (&utent);
  63.     endutent ();
  64.  
  65.     if ((wtmp = fopen (WTMP_FILE, "a+"))) {
  66.         fwrite (&utent, sizeof utent, 1, wtmp);
  67.         fclose (wtmp);
  68.     }
  69. }
  70.