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

  1. /*
  2.  * getent.c -- build local user tree for finger
  3.  * It all started here.. (November 1985)
  4.  *
  5.  * Copyright (C) 1986, 1990  Philip L. Budne
  6.  *
  7.  * This file is part of "Phil's Finger Program".
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 1, or (at your option)
  12.  * any later version.
  13.  *
  14.  */
  15.  
  16. # ifndef lint
  17. static char *rcsid = "$Id: getent.c,v 3.0 90/07/06 13:10:48 budd Rel $";
  18. # endif /* lint not defined */
  19.  
  20. # include <sys/types.h>
  21. # include <utmp.h>
  22. # include <stdio.h>
  23. # include <ctype.h>
  24. # include "person.h"
  25. # include "args.h"            /* for luser.h */
  26. # include "luser.h"
  27. # include "tsel.h"
  28. # include "finger.h"
  29.  
  30. extern struct utmp *getutent();        /* from utmp.c */
  31. extern endutent();            /* from utmp.c */
  32. extern LUSER *treefind();        /* from getperson.c */
  33.  
  34. FORWARD GLOBAL LUSER *linsert();
  35. FORWARD LOCAL  LUSER *mkluser();
  36.  
  37. LOCAL TSEL *first_tsel, *last_tsel;
  38.  
  39. # define LINESIZE sizeof( ((struct utmp *)0)->ut_line )    /* size of ut_line */
  40.  
  41. /* .see gtname() in output.c */
  42. # define TTY_PREFIX "tty"
  43. # define TTY_PREFIX_SIZE (sizeof(TTY_PREFIX)-1)
  44.  
  45.  
  46. GLOBAL void init_tsel() {
  47.     first_tsel = last_tsel = NULL;
  48. }
  49.  
  50. GLOBAL void addtty( s )
  51.     char *s;
  52. {
  53.     register TSEL *ts;
  54.     register l, l2;
  55.     if( (ts = (TSEL *) malloc( sizeof( TSEL ) )) == NULL ) {
  56.     perror("could not allocate tty selector");
  57.     exit( 1 );
  58.     }
  59.  
  60.     l = l2 = strlen( s );
  61.     if( l > LINESIZE ) {
  62.     l = LINESIZE;
  63.     if( l > LINESIZE - TTY_PREFIX_SIZE )
  64.         l2 = l - TTY_PREFIX_SIZE;
  65.     }
  66.  
  67.     ts->ts_name = s;
  68.     ts->ts_len = l;
  69.     ts->ts_len2 = l2;
  70.     ts->ts_next = NULL;
  71.     if( first_tsel == NULL )
  72.     first_tsel = last_tsel = ts;
  73.     else {
  74.     last_tsel->ts_next = ts;
  75.     last_tsel = ts;
  76.     }
  77. }
  78.  
  79. GLOBAL LTREE *maketree() {        /* create tree of users */
  80.     register struct utmp *ut;
  81.     LTREE *tree;
  82.     LUSER *new;
  83.  
  84.     tree = NULL;
  85.  
  86.     while( (ut = getutent()) != NULL ) {
  87.  
  88.     if( ut->ut_name[0] == EOS )    /* entry has name? */
  89.          continue;            /* no, get next */
  90.  
  91.     if( first_tsel != NULL ) {
  92.         register TSEL *t;
  93.         int ok;
  94.  
  95.         ok = FALSE;
  96.         for( t = first_tsel; t != NULL; t = t->ts_next ) {
  97.         if( strncmp( ut->ut_line, t->ts_name, t->ts_len ) == 0 ||
  98.            (strncmp( ut->ut_line, TTY_PREFIX, TTY_PREFIX_SIZE ) == 0 &&
  99.             strncmp( &ut->ut_line[TTY_PREFIX_SIZE],
  100.                 t->ts_name, t->ts_len2 ) == 0) ){
  101.             ok = TRUE;
  102.             break;
  103.         } /* if strncmp */
  104.         } /* for */
  105.         if( !ok )
  106.         continue;
  107.     } /* first_tsel */
  108.  
  109.     new = mkluser(ut);
  110.     tree = linsert(new, tree);
  111.     }
  112.     endutent();
  113.     return( tree );
  114. } /* maketree */
  115.  
  116. GLOBAL LTREE *linsert(new, tree)     /* insert new user into tree */
  117. register LUSER *new;
  118. register LTREE *tree;
  119. {
  120.     if( tree == NULL )            /* empty tree. return new entry */
  121.         return( new );
  122.  
  123.     if( strcmp(new->u_user, tree->u_user) < 0 ) /* less than root? */
  124.      tree->u_left = (LUSER *)linsert(new, tree->u_left); /* put on left */
  125.     else
  126.          tree->u_right = (LUSER *)linsert(new, tree->u_right);/* put on right */
  127.                     /* dupes are added to right */
  128.                     /* (.see pwtree) because we assume */
  129.                     /* that the utmp file is in tty name */
  130.                     /* order */
  131.  
  132.     return( tree );            /* return the tree */
  133. }
  134.  
  135. GLOBAL LUSER *newluser() {        /* create empty luser struct */
  136.     register LUSER *new;
  137.  
  138.     if( (new = (LUSER *)malloc( sizeof( LUSER ) )) == NULL ) {
  139.     fprintf(stderr, "malloc failed in newluser\n");
  140.     exit( 1 );
  141.     }
  142.     new->u_person = NULL;
  143.     new->u_left = new->u_right = NULL;
  144.     new->u_flags = 0;
  145.     new->u_sw = Sw;            /* get per-person switches */
  146.     return( new );
  147. } /* newluser */
  148.  
  149. LOCAL LUSER *mkluser( ut )        /* create a luser from utmp */
  150. register struct utmp *ut;
  151. {
  152.     register LUSER *new;
  153.  
  154.     new = newluser();
  155.  
  156.     strzcpy(new->u_user, ut->ut_name, sizeof(ut->ut_name) );
  157.     strzcpy(new->u_line, ut->ut_line, sizeof(ut->ut_line) );
  158. # ifndef UTMP_NO_HOST
  159.     strzcpy(new->u_host, ut->ut_host, sizeof(ut->ut_host) );
  160. # endif /* UTMP_NO_HOST not defined */
  161.     new->u_time = ut->ut_time;
  162.  
  163.     return( new );
  164. } /* mkluser */
  165.  
  166. /*
  167.  * here with tree of usernames from command line
  168.  * return new tree of logged in users
  169.  */
  170.  
  171. GLOBAL LTREE *ent_select( proto )
  172. LUSER *proto;
  173. {
  174.     register struct utmp *ut;
  175.     char name[ sizeof( ut->ut_name ) + 1 ];
  176.     LUSER *new, *found;
  177.     LTREE *t2;
  178.  
  179.     t2 = NULL;
  180.  
  181.     while( (ut = getutent()) != NULL ) {
  182.     if( ut->ut_name[0] == EOS )    /* entry has name? */
  183.         continue;            /* no, get next */
  184.  
  185.     strzcpy( name, ut->ut_name, sizeof( ut->ut_name ) );
  186.     if( (found = treefind( proto, name )) != NULL ) {
  187.         register PERSON *p;
  188.  
  189.         found->u_flags |= U_FOUND;
  190.         new = mkluser(ut);
  191.         p = new->u_person = found->u_person;
  192.         if( p != NULL )
  193.         p->p_count++;
  194.         t2 = linsert(new, t2);
  195.     } /* found template */
  196.     } /* while utmp */
  197.     endutent();
  198.  
  199.     return( t2 );
  200. } /* ent_select */
  201.  
  202. /*
  203.  * Local variables:
  204.  * comment-column: 40
  205.  * End:
  206.  */
  207.