home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / finger / part02 / getut.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-03  |  2.0 KB  |  94 lines

  1. /*
  2.  * getut.c -- read utmp file
  3.  *
  4.  * Copyright (C) 1986, 1990  Philip L. Budne
  5.  *
  6.  * This file is part of "Phil's Finger Program".
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 1, or (at your option)
  11.  * any later version.
  12.  *
  13.  */
  14.  
  15. # ifndef lint
  16. static char *rcsid = "$Id: getut.c,v 3.0 90/07/06 13:10:58 budd Rel $";
  17. # endif /* lint not defined */
  18.  
  19. # include "finger.h"
  20. # ifdef USG                /* UTMP_NEEDS_TIME_T? */
  21. # include <sys/types.h>            /* for time_t */
  22. # endif /* USG defined */
  23. # include <utmp.h>
  24. # include <stdio.h>
  25.  
  26. # ifdef USER_PROCESS
  27. # include <errno.h>
  28. # endif /* USER_PROCESS defined */
  29.  
  30. # define UTFILE "/etc/utmp"
  31. # define MAXNAME 100
  32.  
  33. LOCAL char utfile[MAXNAME] = UTFILE;
  34. LOCAL FILE *f = NULL;
  35.  
  36. void setutent() {
  37.     if( f == NULL )
  38.     f = fopen( utfile, "r" );
  39.     else
  40.     rewind( f );
  41. } /* setutent */
  42.  
  43. void endutent() {
  44.     if( f != NULL )
  45.     fclose( f );
  46. } /* endutent */
  47.  
  48. void utmpname( file )
  49. char *file;
  50. {
  51.     endutent();
  52.     strcpy(utfile, file);
  53. } /* utmpname */
  54.  
  55. struct utmp *getutent() {
  56.     static struct utmp ut;
  57.  
  58.     if( f == NULL ) {
  59.     setutent();
  60.     if( f == NULL )
  61.         return( NULL );
  62.     }
  63.  
  64.     while( fread(&ut, sizeof( ut ), 1, f) == 1 ) {
  65. # ifdef USER_PROCESS
  66.     if( ut.ut_type == USER_PROCESS
  67.        && (kill( ut.ut_pid, 0 ) == 0 || errno == EPERM ) )
  68. # endif /* USER_PROCESS defined */
  69.         return( &ut );
  70.     }
  71.     rewind( f );
  72.     return( NULL );
  73. } /* getutent */
  74.  
  75. /*
  76.  *    not implemented:
  77.  *
  78.  *    void pututline( struct utmp *u );
  79.  *        (uses direct write(2))
  80.  *    struct utmp *getutline( struct utmp *line )
  81.  *        find entry with matching ut->line
  82.  *    struct utmp *getutid( struct utmp *id )
  83.  *        find entry with matching ut->type (!!)
  84.  *        (RUN_LVL, BOOT_TIME, OLD_TIME, NEW_TIME)
  85.  *        if one of (INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS
  86.  *        or DEAD_PROCESS) ut_id must mach also.
  87.  */
  88.  
  89. /*
  90.  * Local variables:
  91.  * comment-column: 40
  92.  * End:
  93.  */
  94.