home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / nn.tar / nn-6.5.1 / fullname.c < prev    next >
C/C++ Source or Header  |  1995-04-29  |  3KB  |  116 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. extern struct passwd *getpwuid();
  35.  
  36.  
  37. /* fullname.c */
  38.  
  39. static void buildfname __APROTO((register char *p, char *login, char *buf));
  40.  
  41.  
  42.  
  43. /*
  44. **  BUILDFNAME -- build full name from gecos style entry.
  45. **    (routine lifted from sendmail)
  46. **
  47. **    This routine interprets the strange entry that would appear
  48. **    in the GECOS field of the password file.
  49. **
  50. **    Parameters:
  51. **        p -- name to build.
  52. **        login -- the login name of this user (for &).
  53. **        buf -- place to put the result.
  54. **
  55. **    Returns:
  56. **        none.
  57. **
  58. **    Side Effects:
  59. **        none.
  60. */
  61.  
  62. static void buildfname(p, login, buf)
  63.     register char *p;
  64.     char *login;
  65.     char *buf;
  66. {
  67.     register char *bp = buf;
  68.  
  69.     if (*p == '*')
  70.         p++;
  71.     while (*p != '\0' && *p != ',' && *p != ';' && *p != ':' && *p != '(')
  72.     {
  73.         if (*p == '-' && (isdigit(p[-1]) || isspace(p[-1]))) {
  74.             bp = buf;
  75.             p++;
  76.         }
  77.         else if (*p == '&')
  78.         {
  79.             strcpy(bp, login);
  80.             if ((bp == buf || !isalpha(bp[-1])) && islower(*bp))
  81.                 *bp = toupper(*bp);
  82.             while (*bp != '\0')
  83.                 bp++;
  84.             p++;
  85.         }
  86.         else
  87.             *bp++ = *p++;
  88.     }
  89.     *bp = '\0';
  90. }
  91.  
  92. /*
  93.  * Figure out who is sending the message and sign it.
  94.  * We attempt to look up the user in the gecos field of /etc/passwd.
  95.  */
  96. char *full_name()
  97. {
  98.     static char *fullname = NULL;
  99.     char inbuf[FILENAME];
  100.     struct passwd *pw;
  101.  
  102.     if (fullname == NULL) {
  103.     if ((fullname = getenv("NAME")) != NULL)
  104.         return fullname;
  105.  
  106.     pw = getpwuid((int)user_id);
  107.     if (pw == NULL) return fullname = "?";
  108.  
  109.     buildfname(pw->pw_gecos, pw->pw_name, inbuf);
  110.     if (inbuf[0] == 0) strcpy(inbuf, pw->pw_name);
  111.  
  112.     fullname = copy_str(inbuf);
  113.     }
  114.     return fullname;
  115. }
  116.