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