home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / inetutils-1.2-src.tgz / tar.out / fsf / inetutils / libinetutils / logwtmp.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  3KB  |  123 lines

  1. /* A version of bsd `logwtmp' that should be widely portable
  2.  
  3.    Copyright (C) 1996 Free Software Foundation, Inc.
  4.  
  5.    Written by Miles Bader <miles@gnu.ai.mit.edu>
  6.  
  7.    This program is free software; you can redistribute it and/or
  8.    modify it under the terms of the GNU General Public License as
  9.    published by the Free Software Foundation; either version 2, or (at
  10.    your option) any later version.
  11.  
  12.    This program is distributed in the hope that it will be useful, but
  13.    WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.    General Public License for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License
  18.    along with this program; if not, write to the Free Software
  19.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* If `KEEP_OPEN' is defined, then a special version of logwtmp is compiled,
  22.    called logwtmp_keep_open, which keeps the wtmp file descriptor open
  23.    between calls, and doesn't attempt to open the file after the first call. */
  24.  
  25. #ifdef HAVE_CONFIG_H
  26. #include <config.h>
  27. #endif
  28.  
  29. #include <unistd.h>
  30. #include <sys/types.h>
  31. #include <sys/time.h>
  32. #include <sys/fcntl.h>
  33. #include <sys/stat.h>
  34. #include <sys/file.h>
  35. #ifdef HAVE_ERRNO_H
  36. #include <errno.h>
  37. #endif
  38. #ifdef HAVE_UTMP_H
  39. #include <utmp.h>
  40. #else
  41. #ifdef  HAVE_UTMPX_H
  42. #include <utmpx.h>
  43. #define utmp utmpx        /* make utmpx look more like utmp */
  44. #endif
  45. #endif
  46.  
  47. #ifndef HAVE_ERRNO_DECL
  48. extern int errno;
  49. #endif
  50.  
  51. static void
  52. _logwtmp (ut)
  53.      struct utmp *ut;
  54. {
  55. #ifdef KEEP_OPEN
  56.   static int fd = -1;
  57.  
  58.   if (fd < 0)
  59.     fd = open (PATH_WTMP, O_WRONLY|O_APPEND, 0);
  60. #else
  61.   int fd = open (PATH_WTMP, O_WRONLY|O_APPEND, 0);
  62. #endif
  63.  
  64.   if (fd >= 0)
  65.     {
  66.       struct stat st;
  67.  
  68. #ifdef HAVE_FLOCK
  69.       if (flock (fd, LOCK_EX|LOCK_NB) < 0 && errno != ENOSYS)
  70.     {
  71.       sleep (1);
  72.       flock (fd, LOCK_EX|LOCK_NB); /* ignore error */
  73.     }
  74. #endif
  75.  
  76. #ifdef HAVE_FTRUNCATE
  77.       if (fstat (fd, &st) == 0
  78.       && write (fd, (char *)ut, sizeof *ut) != sizeof *ut)
  79.     ftruncate (fd, st.st_size);
  80. #else
  81.       write (fd, (char *)ut, sizeof *ut);
  82. #endif
  83.  
  84. #ifdef HAVE_FLOCK
  85.       flock (fd, LOCK_UN);
  86. #endif
  87.  
  88. #ifndef KEEP_OPEN
  89.       close (fd);
  90. #endif
  91.     }
  92. }
  93.  
  94. void
  95. #ifdef KEEP_OPEN
  96. logwtmp_keep_open (line, name, host)
  97. #else
  98. logwtmp (line, name, host)
  99. #endif
  100.      char *line, *name, *host;
  101. {
  102.   struct utmp ut;
  103.  
  104.   /* Set information in new entry.  */
  105.   bzero (&ut, sizeof (ut));
  106. #ifdef HAVE_UTMP_UT_TYPE
  107.   ut.ut_type = USER_PROCESS;
  108. #endif
  109.   strncpy (ut.ut_line, line, sizeof ut.ut_line);
  110.   strncpy (ut.ut_name, name, sizeof ut.ut_name);
  111. #ifdef HAVE_UTMP_UT_HOST
  112.   strncpy (ut.ut_host, host, sizeof ut.ut_host);
  113. #endif
  114.  
  115. #ifdef HAVE_UTMP_UT_TV
  116.   gettimeofday (&ut.ut_tv, NULL);
  117. #else
  118.   time (&ut.ut_time);
  119. #endif
  120.  
  121.   _logwtmp (&ut);
  122. }
  123.