home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume22 / nn6.4 / part20 / fullname.c < prev    next >
C/C++ Source or Header  |  1990-06-07  |  3KB  |  108 lines

  1. /*
  2.  * This file is derived from Bnews' fullname.c file.
  3.  * Bnews is Copyright (c) 1986 by Rick Adams.
  4.  *
  5.  * NOTICE: THIS CODE HAS BEEN MODIFIED TO FIT THE NN ENVIRONMENT:
  6.  *
  7.  *    The full_name function has been rewritten entirely, although
  8.  *    there are still some structural resemblence.
  9.  *    Fullname checks $NAME before looking at /etc/passwd.
  10.  *    The LOCALNAME alternative has been removed, because it would fit
  11.  *    nn very poorly.
  12.  *    The buildfname function is made static and moved before full_name.
  13.  *
  14.  * fullname.c - this file is made separate so that different local
  15.  * conventions can be applied.  The stock version understands two
  16.  * conventions:
  17.  *
  18.  * (a) Berkeley finger: the gecos field in /etc/passwd begins with
  19.  *     the full name, terminated with comma, semicolon, or end of
  20.  *     field.  & expands to the login name.
  21.  * (b) BTL RJE: the gecos field looks like
  22.  *    : junk - full name ( junk :
  23.  *     where the "junk -" is optional.
  24.  *
  25.  * If you have a different local convention, modify this file accordingly.
  26.  */
  27.  
  28. #ifdef SCCSID
  29. static char    *SccsId = "@(#)fullname.c    1.13    11/4/87";
  30. #endif /* SCCSID */
  31.  
  32. #include "config.h"
  33. #include <pwd.h>
  34.  
  35. /*
  36. **  BUILDFNAME -- build full name from gecos style entry.
  37. **    (routine lifted from sendmail)
  38. **
  39. **    This routine interprets the strange entry that would appear
  40. **    in the GECOS field of the password file.
  41. **
  42. **    Parameters:
  43. **        p -- name to build.
  44. **        login -- the login name of this user (for &).
  45. **        buf -- place to put the result.
  46. **
  47. **    Returns:
  48. **        none.
  49. **
  50. **    Side Effects:
  51. **        none.
  52. */
  53.  
  54. static buildfname(p, login, buf)
  55.     register char *p;
  56.     char *login;
  57.     char *buf;
  58. {
  59.     register char *bp = buf;
  60.  
  61.     if (*p == '*')
  62.         p++;
  63.     while (*p != '\0' && *p != ',' && *p != ';' && *p != ':' && *p != '(')
  64.     {
  65.         if (*p == '-' && (isdigit(p[-1]) || isspace(p[-1]))) {
  66.             bp = buf;
  67.             p++;
  68.         }
  69.         else if (*p == '&')
  70.         {
  71.             strcpy(bp, login);
  72.             if ((bp == buf || !isalpha(bp[-1])) && islower(*bp))
  73.                 *bp = toupper(*bp);
  74.             while (*bp != '\0')
  75.                 bp++;
  76.             p++;
  77.         }
  78.         else
  79.             *bp++ = *p++;
  80.     }
  81.     *bp = '\0';
  82. }
  83.  
  84. /*
  85.  * Figure out who is sending the message and sign it.
  86.  * We attempt to look up the user in the gecos field of /etc/passwd.
  87.  */
  88. char *full_name()
  89. {
  90.     static char *fullname = NULL;
  91.     char inbuf[FILENAME];
  92.     struct passwd *pw, *getpwuid();
  93.  
  94.     if (fullname == NULL) {
  95.     if ((fullname = getenv("NAME")) != NULL)
  96.         return fullname;
  97.  
  98.     pw = getpwuid((int)user_id);
  99.     if (pw == NULL) return fullname = "?";
  100.  
  101.     buildfname(pw->pw_gecos, pw->pw_name, inbuf);
  102.     if (inbuf[0] == 0) strcpy(inbuf, pw->pw_name);
  103.  
  104.     fullname = copy_str(inbuf);
  105.     }
  106.     return fullname;
  107. }
  108.