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 / logout.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  3KB  |  141 lines

  1. /* A version of bsd `logout' 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. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24.  
  25. #include <unistd.h>
  26. #include <sys/types.h>
  27. #include <sys/time.h>
  28. #include <utmp.h>
  29.  
  30. int
  31. logout (char *line)
  32. {
  33.   int success = 0;
  34.  
  35. #ifdef HAVE_SETUTENT        /* sysvish version */
  36.   struct utmp *ut, search;
  37.  
  38. #ifdef HAVE_UTMPNAME
  39.   if (! utmpname (PATH_UTMP))
  40.     return 0;
  41. #endif
  42.  
  43.   setutent ();
  44.  
  45. #ifdef HAVE_UTMP_UT_TYPE
  46.   search.ut_type = USER_PROCESS;
  47. #endif
  48.   strncpy (search.ut_line, line, sizeof search.ut_line);
  49.  
  50.   ut = getutline (&search);
  51.   if (ut)
  52.     {
  53. #ifdef HAVE_UTMP_UT_HOST
  54.       bzero (ut->ut_host, sizeof ut->ut_host);
  55. #endif
  56. #ifdef HAVE_UTMP_UT_TV
  57.       gettimeofday (&ut->ut_tv, NULL);
  58. #else
  59.       time (&ut->ut_time);
  60. #endif
  61.  
  62.       if (pututline (ut) >= 0)
  63.     success = 1;
  64.  
  65.       endutent ();
  66.     }
  67.  
  68. #else /* !HAVE_SETUTENT */
  69. #ifdef HAVE_GETUXLINE
  70.   /* Strange utmpx version */
  71.   struct utmpx *ut, search;
  72.  
  73.   strncpy (search.ut_line, line, sizeof (search.ut_line));
  74.   ut = getutxline (&search);
  75.   if (ut)
  76.     {
  77.       ut->ut_type = DEAD_PROCESS;
  78.       ut->ut_exit.e_termination = 0;
  79.       ut->ut_exit.e_exit = 0;
  80. #ifdef HAVE_GETTIMEOFDAY
  81.       gettimeofday (&ut->ut_tv, NULL);
  82. #else
  83.       time (&ut->ut_tv.tv_sec);
  84.       ut->ut_tv.tv_usec = 0;
  85. #endif
  86.       modutx (ut);
  87.       success = 1;
  88.   }
  89.  
  90.   endutxent();
  91.  
  92. #else /* !HAVE_GETUTXLINE */
  93.   /* Old bsdish version */
  94.   int ut_fd;
  95.  
  96.   ut_fd = open (PATH_UTMP, O_RDWR);
  97.   if (ut_fd >= 0)
  98.     {
  99.       struct utmp *ut;
  100.       struct utmp ut_buf[100];
  101.       off_t pos = 0;        /* Position in file */
  102.  
  103.       while (!success && (rd = read (ut_fd, ut_buf, sizeof ut_buf)) > 0)
  104.     {
  105.       struct utmp *ut_end = (struct utmp *)((char *)ut_buf + rd);
  106.  
  107.       for (ut = ut_buf; ut < ut_end; ut++, pos += sizeof *ut)
  108.         if (ut->ut_name[0]
  109.         && strncmp (ut->ut_name, line, sizeof ut->ut_line) == 0)
  110.           /* Found the entry for LINE; mark it as logged out.  */
  111.           {
  112.         /* Zero out entries describing who's logged in.  */
  113.         bzero (ut->ut_name, sizeof ut->ut_name);
  114. #ifdef HAVE_UTMP_UT_HOST
  115.         bzero (ut->ut_host, sizeof ut->ut_host);
  116. #endif
  117. #ifdef HAVE_UTMP_UT_TV
  118.         gettimeofday (&ut->ut_tv, NULL);
  119. #else
  120.         time (&ut->ut_time);
  121. #endif
  122.         
  123.         /* Now seek back to the position in utmp at which UT occured,
  124.            and write the new version of UT there.  */
  125.         if (lseek (ut_fd, pos, SEEK_SET) >= 0
  126.             && write (ut_fd, (char *)ut, sizeof (*ut)) == sizeof (*ut))
  127.           {
  128.             success = 1;
  129.             break;
  130.           }
  131.           }
  132.     }
  133.  
  134.       close (ut_fd);
  135.     }
  136. #endif /* HAVE_GETUTXLINE */
  137. #endif /* HAVE_SETUTENT */
  138.  
  139.   return success;
  140. }
  141.