home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3340 / utmp.c < prev   
Encoding:
C/C++ Source or Header  |  1991-05-17  |  2.9 KB  |  145 lines

  1. /*
  2.  * Copyright 1989, 1990, 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.  
  12. #include <sys/types.h>
  13. #include <utmp.h>
  14. #include <fcntl.h>
  15. #ifndef    BSD
  16. #include <string.h>
  17. #include <memory.h>
  18. #define    bzero(a,n)    memset(a, 0, n)
  19. #else
  20. #include <strings.h>
  21. #define    strchr    index
  22. #define    strrchr    rindex
  23. #endif
  24. #include <stdio.h>
  25. #include "config.h"
  26.  
  27. #ifndef    lint
  28. static    char    sccsid[] = "%W%    %U%    %G%";
  29. #endif
  30.  
  31. extern    struct    utmp    utent;
  32. extern    char    name[];
  33.  
  34. extern    struct    utmp    *getutent();
  35. extern    void    setutent();
  36. extern    void    endutent();
  37. extern    time_t    time();
  38. extern    char    *ttyname();
  39.  
  40. #define    NO_UTENT \
  41.     "No utmp entry.  You must exec \"login\" from the lowest level \"sh\""
  42.  
  43. /*
  44.  * checkutmp - see if utmp file is correct for this process
  45.  *
  46.  *    System V is very picky about the contents of the utmp file
  47.  *    and requires that a slot for the current process exist.
  48.  *    The utmp file is scanned for an entry with the same process
  49.  *    ID.  If no entry exists the process exits with a message.
  50.  */
  51.  
  52. void
  53. checkutmp (picky)
  54. int    picky;
  55. {
  56.     struct    utmp    *ut;
  57.     char    *line;
  58. #ifndef    NDEBUG
  59.     int    pid = getppid ();
  60. #else
  61.     int    pid = getpid ();
  62. #endif
  63.     setutent ();
  64.  
  65. #ifndef    BSD
  66.     if (picky) {
  67.         while (ut = getutent ())
  68.             if (ut->ut_pid == pid)
  69.                 break;
  70.  
  71.         if (ut)
  72.             utent = *ut;
  73.  
  74.         endutent ();
  75.  
  76.         if (ut && utent.ut_pid == pid)
  77.             return;
  78.  
  79.         puts (NO_UTENT);
  80.         exit (1);
  81.     } else {
  82.         line = ttyname (0);
  83.         if (strncmp (line, "/dev/", 5) == 0)
  84.             line += 5;
  85.  
  86.         strncpy (utent.ut_line, line, sizeof utent.ut_line);
  87.         if (ut = getutline (&utent))
  88.             strncpy (utent.ut_id, ut->ut_id, sizeof ut->ut_id);
  89.  
  90.         strcpy (utent.ut_user, "LOGIN");
  91.         utent.ut_pid = getpid ();
  92.         utent.ut_type = LOGIN_PROCESS;
  93.         time (&utent.ut_time);
  94.     }
  95. #endif
  96. }
  97.  
  98. /*
  99.  * setutmp - put a USER_PROCESS entry in the utmp file
  100.  *
  101.  *    setutmp changes the type of the current utmp entry to
  102.  *    USER_PROCESS.  the wtmp file will be updated as well.
  103.  */
  104.  
  105. void
  106. setutmp (name, line)
  107. char    *name;
  108. char    *line;
  109. {
  110.     FILE    *wtmp;
  111.     struct    utmp    utent;
  112.     int    fd;
  113.     int    i;
  114.     int    found = 0;
  115.  
  116.     if (! (fd = open ("/etc/utmp", O_RDWR)))
  117.         return;
  118.  
  119.     while (! found && read (fd, &utent, sizeof utent) == sizeof utent) {
  120.         if (! strncmp (line, utent.ut_line, sizeof utent.ut_line))
  121.             found++;
  122.     }
  123.     if (! found) {
  124.         bzero (&utent, sizeof utent);
  125.         strncpy (utent.ut_line, line, sizeof utent.ut_line);
  126.     }
  127.     (void) strncpy (utent.ut_user, name, sizeof utent.ut_user);
  128. #ifndef    BSD
  129.     utent.ut_type = USER_PROCESS;
  130.     utent.ut_pid = getpid ();
  131. #endif
  132.     (void) time (&utent.ut_time);
  133.  
  134.     if (found)
  135.         lseek (fd, (long) - sizeof utent, 1);
  136.  
  137.     write (fd, &utent, sizeof utent);
  138.     close (fd);
  139.  
  140.     if ((wtmp = fopen (WTMP_FILE, "a+"))) {
  141.         fwrite (&utent, sizeof utent, 1, wtmp);
  142.         fclose (wtmp);
  143.     }
  144. }
  145.