home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume8 / hint / part01 / user.c < prev   
Encoding:
C/C++ Source or Header  |  1989-08-24  |  1.9 KB  |  70 lines

  1. /* user.c -- functions dealing with logins
  2.    Copyright (C) 1989 David MacKenzie
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  17.  
  18. #include "hint.h"
  19. #include <utmp.h>
  20.  
  21. /* Add all ttys where `user' is logged on to the list of ttys to hint.
  22.    Return the number of times `user' is logged on.  */
  23.  
  24. int
  25. adduser (user)
  26.      char *user;
  27. {
  28.   struct utmp ut;
  29.   int nfound;            /* Number of times `user' is logged on. */
  30.   int fd;
  31.  
  32.   fd = open ("/etc/utmp", O_RDONLY);
  33.   if (fd == -1)
  34.     pfatal ("/etc/utmp");
  35.  
  36.   nfound = 0;
  37.   while (read (fd, &ut, sizeof (struct utmp)) > 0)
  38.     nfound += addmatch (user, &ut);
  39.  
  40.   close (fd);
  41.   return nfound;
  42. }
  43.  
  44. /* If the name field in `utp' matches `user' and the tty is hint y,
  45.      add the `utp' tty field to the list of ttys to hint and return 1;
  46.    else,
  47.      return 0.  */
  48.  
  49. int
  50. addmatch (user, utp)
  51.      char *user;
  52.      struct utmp *utp;
  53. {
  54.   char temp_line[30];        /* 30 is random number > max. ut_line size. */
  55.  
  56.   /* Make sure it's a valid entry. */
  57.   if (*utp->ut_name == 0)
  58.     return 0;
  59.  
  60.   if (!strncmp (user, utp->ut_name, sizeof (utp->ut_name)))
  61.     {
  62.       strncpy (temp_line, utp->ut_line, sizeof (utp->ut_line));
  63.       temp_line[sizeof (utp->ut_line)] = 0;
  64.       addtty (user, temp_line);
  65.       return 1;
  66.     }
  67.   else
  68.     return 0;
  69. }
  70.