/**********************************************************************
 * luc_fullname.c -- return a full name of a user if possible
 *
 * $Author: brlewis $
 * $Source: /afs/athena.mit.edu/astaff/project/lucydev/src/lib/RCS/luc_fullname.c,v $
 * $Header: /afs/athena.mit.edu/astaff/project/lucydev/src/lib/RCS/luc_fullname.c,v 1.1 91/08/01 14:31:34 brlewis Exp Locker: brlewis $
 *
 * Copyright 1991 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 **********************************************************************/
#include <mit-copyright.h>

#ifndef lint
static char rcsid_luc_fullname_c[] = "$Header: /afs/athena.mit.edu/astaff/project/lucydev/src/lib/RCS/luc_fullname.c,v 1.1 91/08/01 14:31:34 brlewis Exp Locker: brlewis $";
#endif /* lint */

#include <stdio.h>
#include <strings.h>
#include <hesiod.h>

/**********************************************************************
 * luc_fullname(uname)
 *	caller sets uname to a username
 *
 * - uses hesiod information
 * - if no hesiod info available, then returns original string
 * - return value need not be freed
 **********************************************************************/

char *
luc_fullname(uname)
     char *uname;
{
  char **namelist;
  register char *s1, *s2;
  static char finalname[256];
  int i;

  if ((namelist = hes_resolve(uname, "passwd")) == NULL) {
    strcpy(finalname, uname);
    return(finalname);
  }
  s1 = *namelist;
  /* Find fourth colon-separated field */
  for (i=0; i<4; i++)
    if ((s1 = index(++s1, ':')) == NULL) {
      /* Password entry has too few fields.  Give up. */
      strcpy(finalname, uname);
      return(finalname);
    }
  s1++;			/* first character past colon */

  /* Copy up to end of field */
  s2 = finalname;
  while(*s1 != ':' && *s1 != ',' && *s1 != '\0')
    *s2++ = *s1++;
  *s2 = '\0';
  return(finalname);
}
 
