home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2287 / utmp.c < prev   
Encoding:
C/C++ Source or Header  |  1990-12-28  |  1.6 KB  |  92 lines

  1. /*
  2.  * Copyright 1989, 1990, John F. Haugh II
  3.  * All rights reserved.
  4.  *
  5.  * Use, duplication, and disclosure prohibited without
  6.  * the express written permission of the author.
  7.  */
  8.  
  9. #include <sys/types.h>
  10. #include <utmp.h>
  11. #ifndef    BSD
  12. #include <string.h>
  13. #include <memory.h>
  14. #else
  15. #include <strings.h>
  16. #define    strchr    index
  17. #define    strrchr    rindex
  18. #endif
  19. #include <stdio.h>
  20. #include "config.h"
  21.  
  22. #ifndef    lint
  23. static    char    sccsid[] = "@(#)utmp.c    2.3    19:24:26    7/29/90";
  24. #endif
  25.  
  26. extern    struct    utmp    utent;
  27. extern    char    name[];
  28.  
  29. struct    utmp    *getutent ();
  30. void    setutent ();
  31. void    endutent ();
  32. void    pututline ();
  33. time_t    time ();
  34.  
  35. void    checkutmp ()
  36. {
  37.     struct    utmp    *ut;
  38. #ifndef    NDEBUG
  39.     int    pid = getppid ();
  40. #else
  41.     int    pid = getpid ();
  42. #endif
  43.     setutent ();
  44.  
  45.     while (ut = getutent ())
  46.         if (ut->ut_pid == pid)
  47.             break;
  48.  
  49.     if (ut)
  50.         utent = *ut;
  51.  
  52.     endutent ();
  53.  
  54.     if (ut && utent.ut_pid == pid)
  55.         return;
  56.  
  57.     puts ("No utmp entry.  You must exec \"login\" from the lowest level \"sh\"");
  58.     exit (1);
  59. }
  60.  
  61. void    setutmp ()
  62. {
  63.     FILE    *wtmp;
  64.     char    tty[sizeof utent.ut_line + 1];
  65.     char    *line;
  66.  
  67.     setutent ();
  68.  
  69.     (void) strncpy (utent.ut_user, name, sizeof utent.ut_user);
  70.  
  71.     utent.ut_type = USER_PROCESS;
  72.  
  73.     if (line = strrchr (utent.ut_line, '/')) {
  74.         (void) strcpy (tty, line + 1);
  75. #ifndef    BSD
  76.         (void) memset (utent.ut_line, '\0', sizeof utent.ut_line);
  77. #else
  78.         bzero (utent.ut_line, sizeof utent.ut_line);
  79. #endif
  80.         (void) strcpy (utent.ut_line, tty);
  81.     }
  82.     (void) time (&utent.ut_time);
  83.  
  84.     pututline (&utent);
  85.     endutent ();
  86.  
  87.     if ((wtmp = fopen (WTMP_FILE, "a+"))) {
  88.         fwrite (&utent, sizeof utent, 1, wtmp);
  89.         fclose (wtmp);
  90.     }
  91. }
  92.