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

  1. /*
  2.  * getperson.c -- read person oriented data (December 1985)
  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: getperson.c,v 3.0 90/07/06 13:10:51 budd Rel $";
  17. # endif /* lint not defined */
  18.  
  19. # include <sys/types.h>
  20. # include <strings.h>
  21. # include <ctype.h>
  22. # include <stdio.h>
  23. # include <pwd.h>            /* getpwent defns */
  24. # ifdef INQUIRE
  25. # include <inquir.h>
  26. # endif /* INQUIRE defined */
  27. # include "person.h"
  28. # include "args.h"            /* before luser.h */
  29. # include "luser.h"
  30. # include "finger.h"
  31.  
  32. # ifndef DEFSHELL
  33. # define DEFSHELL "/bin/csh"        /* dull shell */
  34. # endif /* DEFSHELL not defined */
  35.  
  36. # ifndef PRJLEN
  37. # define PRJLEN 70            /* max lex for project */
  38. # endif /* PRJLEN not defined */
  39.  
  40. extern char *strip();            /* from string.c */
  41. extern BOOL useinquire;
  42.  
  43. PERSON *makeperson();            /* forward... */
  44.  
  45. GLOBAL int treesize( t )        /* count entries in a tree */
  46. register LUSER *t;
  47. {
  48.     if( t == NULL )
  49.     return( 0 );
  50.     else
  51.     return( treesize( t->u_left ) + treesize( t->u_right ) + 1 );
  52. }
  53.  
  54. GLOBAL LUSER *treefind(t, uname)    /* find uname in tree */
  55. register LUSER *t;
  56. char *uname;
  57. {
  58.     register int compare;
  59.  
  60.     while( t != NULL ) {
  61. # ifdef DEBUG
  62.     printf(" tf '%s' ", uname );
  63.     fflush(stdout);
  64.     printf(" '%s'", t->u_user );
  65.     fflush(stdout);
  66. # endif /* DEBUG defined */
  67.         compare = strcmp(uname, t->u_user); /* do compare once */
  68. # ifdef DEBUG
  69.     printf(" %d\n", compare );
  70. # endif /* DEBUG defined */
  71.     if( compare > 0 )
  72.         t = t->u_right;
  73.     else if( compare < 0 )
  74.         t = t->u_left;
  75.     else
  76.         return( t );
  77.     }
  78.     return( NULL );
  79. } /* treefind */
  80.  
  81. # ifdef GETPWNAM_SLOW
  82. GLOBAL int pwtree( t )            /* get pw entries for whole tree */
  83. LTREE *t;
  84. {
  85.     register struct passwd *pw;
  86.     int size, todo;            /* number of tree entries */
  87.  
  88.     size = todo = treesize( t );    /* get entries in tree */
  89.     if( todo == 0 )
  90.     return( 0 );            /* nothing to do! */
  91.  
  92.     setpwent();
  93.     while( todo > 0  &&  (pw = getpwent()) != NULL ) { /* for each pwentry */
  94.                     /* search in tree for this uname */
  95.                     /* if found, create a person struct */
  96.                     /* and look for multiple logins */
  97.  
  98.     register LUSER *tp;
  99.     register PERSON *pp;
  100.  
  101. # ifdef DEBUG
  102.     printf("pwtree(%d): %s\n", todo, pw->pw_name );
  103. # endif /* DEBUG defined */
  104.  
  105.     tp = t;                /* get root of tree */
  106.     pp = NULL;            /* no person entry created yet */
  107.  
  108.     tp = treefind(tp, pw->pw_name); /* lookup user in tree */
  109.     while( todo > 0  &&  tp != NULL ) {
  110.         if( tp->u_person != NULL )    /* already have person? */
  111.         break;            /* must be is passwd twice! (or yp) */
  112.  
  113.         todo--;            /* one less to do */
  114. # ifdef DEBUG
  115.         printf(" ** %s **\n", tp->u_user );
  116. # endif /* DEBUG defined */
  117.         if( pp == NULL )        /* created a person yet? */
  118.             pp = makeperson(tp,pw,NULL); /* do it now. */
  119.         pp->p_count++;        /* bump use count */
  120.         tp->u_person = pp;        /* point user to person */
  121.         tp = treefind(tp->u_right, pw->pw_name); /* lookup again */
  122.                     /* assumes that dupes are added */
  123.                     /* to right side (.see insert)*/
  124.     } /* while tp != NULL */
  125.     } /* while getpwent */
  126.     endpwent();
  127.     return( size );
  128. } /* pwtree */
  129. # else  /* GETPWNAM_SLOW not defined */
  130. LOCAL int pwtree2_count;        /* ah for pascal */
  131. LOCAL void pwtree2();            /* forward */
  132.  
  133. GLOBAL int pwtree( t )
  134. LTREE *t;
  135. {
  136.     setpwent();                /* getpwnam does set/end */
  137.                     /* so this is a noop */
  138.     pwtree2_count = 0;
  139.     pwtree2( t );
  140.     endpwent();
  141.     return( pwtree2_count );
  142. } /* pwtree */
  143.  
  144. LOCAL void pwtree2( t )            /* fill in person entries */
  145. LTREE *t;                /* using getpwnam */
  146. {    
  147.     register struct passwd *pw;
  148.     register LUSER *tp;
  149.     register PERSON *pp;
  150.  
  151. # ifdef DEBUG
  152.     printf("pwtree2(%d): %s\n", todo, pw->pw_name );
  153. # endif /* DEBUG defined */
  154.  
  155.     tp = (LUSER *)t;            /* get root of tree */
  156.     pp = NULL;                /* no person entry created yet */
  157.  
  158.     if( tp == NULL )
  159.     return;
  160.  
  161.     while( tp != NULL && tp->u_person == NULL ) {
  162.     if( pp == NULL ) {        /* created a person yet? */
  163.         if( (pw = getpwnam( tp->u_user )) == NULL )
  164.         break;
  165.         pp = makeperson(tp,pw,NULL); /* do it now. */
  166.     }
  167.     pp->p_count++;            /* bump use count */
  168.     pwtree2_count++;
  169.     tp->u_person = pp;        /* point user to person */
  170.     tp = treefind(tp->u_right, pw->pw_name); /* lookup again */
  171.                     /* assumes that dupes are added */
  172.                     /* to right side (.see insert)*/
  173.     } /* while tp != NULL */
  174.  
  175.     pwtree2( t->u_right );
  176.     pwtree2( t->u_left );
  177.  
  178. } /* pwtree2 */
  179. # endif /* GETPWNAM_SLOW not defined */
  180.  
  181. GLOBAL PERSON *makeperson( up, pw, inq )
  182. LUSER *up;
  183. struct passwd *pw;
  184. struct info *inq;
  185. {
  186.     char fname[100], pbuf[ PLEN+1 ];
  187.     register PERSON *pp;
  188.     register char *dp;
  189.     register int i;
  190.     char *sp;
  191.     int file;
  192.  
  193.     if( (pp = (PERSON *) malloc( sizeof( PERSON ) )) == NULL )
  194.     return( NULL );
  195.  
  196.     pp->p_count = 0;
  197.     pp->p_flags = 0;            /* clear flags */
  198.     pp->p_waddr = pp->p_wphone = pp->p_hphone = pp->p_haddr = NULL;
  199.     pp->p_birthday = pp->p_supervisor = pp->p_project = pp->p_nickname = NULL;
  200.     pp->p_maddr = pp->p_personal = pp->p_home = NULL;
  201.     pp->p_remarks = pp->p_shell = NULL;
  202.     pp->p_group = pp->p_relation = ' ';
  203.  
  204.     if( pw != NULL ) {            /* !! */
  205.     pp->p_uid = pw->pw_uid;        /* copy user id */
  206.     pp->p_gid = pw->pw_gid;        /* copy group id */
  207.     pp->p_home = savestr( pw->pw_dir ); /* home directory */
  208.  
  209.     maddr( pp, pw->pw_name );    /* get mailing address */
  210.  
  211.     sp = pw->pw_gecos;        /* source pointer */
  212.     if( strcmp(sp, "RC") == 0 ) {    /* gecos == "RC" */
  213.         pp->p_flags |= P_RC;    /* flag it */
  214.         return( pp );
  215.     } /* RC */
  216.     }
  217.     else
  218.     pp->p_flags |= P_NOPWENT;
  219.  
  220. # ifdef INQUIRE
  221.     if( pw != NULL )
  222.     name = pw->pw_name;
  223.     else                /* seems unlikely, but */
  224.     name = up->u_user;        /* of use when yp kaput! */
  225.     if( useinquire && doinquire( name, pp, inq ) ) /* check holmes database */
  226.     pp->p_flags |= P_INQUIRE;    /* flag it */
  227. # else  /* INQUIRE not defined */
  228.     acct_group( pp );
  229. # endif /* INQUIRE not defined */
  230.  
  231.     if( pw != NULL && pp->p_personal == NULL ) {
  232.     dp = pbuf;            /* pointer to dest */
  233.     for( i = 0; i < PLEN; i++ ) {    /* for max len of personal name */
  234.         if( *sp == EOS || *sp == ',' )
  235.         break;
  236.         if( *sp == '&' ) {        /* sigh, handle ampersand crock */
  237.         register char *unp;    /* uname pointer */
  238.         unp = pw->pw_name;    /* get pointer to user name */
  239.         if( islower( *unp ) && (i == 0 || isspace(sp[-1])) ) {
  240.             *dp++ = toupper( *unp );
  241.             unp++;        /* avoid side effect as macro arg */
  242.             i++;
  243.         } /* first char is lower */
  244.  
  245.         while( i < PLEN && *unp != NULL ) { /* copy it in */
  246.             i++;
  247.             *dp++ = *unp++;
  248.         } /* while */
  249.         sp++;
  250.         } /* & */
  251.         else
  252.         *dp++ = *sp++;
  253.     } /* for i */
  254.     *dp = EOS;
  255.     pp->p_personal = savestr( pbuf );
  256.     } /* have pw entry, no personal name */
  257.  
  258.     if( !sw_whois )            /* **** doing full? **** */
  259.     return( pp );            /* no, quit now */
  260.  
  261.     if( pw != NULL &&
  262.        (pp->p_waddr == NULL ||
  263.     pp->p_wphone == NULL || pp->p_hphone == NULL ) ) {
  264.  
  265.     /* BUG: handle USG format gecos data */
  266.     sp = index(pw->pw_gecos, ',');    /* anything after user? */
  267.     while( sp != NULL ) {        /* radioactive waste *BOGUS LOOP* */
  268.         sp++;            /* skip comma */
  269.  
  270.         if( !skipwhite( &sp ) )
  271.         break;            /* break bogus loop */
  272.                     /* avoids harmful goto! */
  273.         dp = index(sp, ',');
  274.         if( dp != NULL )
  275.         *dp++ = EOS;
  276.         sp = strip( sp );
  277.         if( pp->p_waddr == NULL && strlen( sp ) > 0 )
  278.         pp->p_waddr = savestr( office( sp ) );
  279.         if( dp == NULL )
  280.         break;            /* break bogus loop */
  281.  
  282.         sp = dp;
  283.         if( !skipwhite( &sp ) )
  284.         break;            /* break bogus loop */
  285.  
  286.         dp = index(sp, ',');
  287.         if( dp != NULL )
  288.         *dp++ = EOS;
  289.         sp = strip( sp );
  290.         if( *sp != EOS )
  291.         if( pp->p_wphone == NULL )
  292.             pp->p_wphone = savestr( sp );
  293.  
  294.         if( dp == NULL )
  295.         break;            /* break bogus loop */
  296.  
  297.         sp = dp;
  298.         if( !skipwhite( &sp ) )
  299.         break;            /* break bogus loop */
  300.  
  301.         sp = strip( sp );
  302.         if( *sp != EOS )
  303.         if( pp->p_hphone == NULL )
  304.             pp->p_hphone = savestr( sp );
  305.  
  306.         break;            /* break bogus loop */
  307.     } /* process radioactive waste (gecos) */
  308.     } /* pw entry & missing info from GECOS */
  309.  
  310.     if( pw != NULL ) {
  311.     if( strcmp(pw->pw_shell, DEFSHELL) != 0 )
  312.         pp->p_shell = savestr( pw->pw_shell );
  313.     else
  314.         pp->p_shell = NULL;
  315.     
  316.     if( pp->p_project == NULL ) {
  317.         strcpy(fname, pp->p_home);
  318.         strcat(fname, "/.project");
  319.         while( (file = open(fname, 0)) >= 0 ) {
  320.         char prjbuf[PRJLEN];
  321.         char *cp;
  322.         int cc;
  323.         
  324.         cc = read(file, prjbuf, PRJLEN-1);
  325.         close(file);
  326.         if( cc < 1 )
  327.             break;        /* leave bogus loop */
  328.         prjbuf[cc] = EOS;
  329.         
  330.         if( (cp = index(prjbuf, '\n')) != NULL )
  331.             *cp = EOS;
  332.         
  333.         cp = strip( prjbuf );
  334.         if( *cp != EOS )
  335.             pp->p_project = savestr( cp );
  336.         break;        /* leave bogus loop */
  337.         } /* found project */
  338.     } /* look at .project file */
  339.     } /* have pw entry */
  340.     return( pp );
  341. } /* makeperson */
  342.  
  343. /*
  344.  * Local variables:
  345.  * comment-column: 40
  346.  * End:
  347.  */
  348.