File: fullname.c

package info (click to toggle)
nn 6.7.3-8
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 2,508 kB
  • ctags: 3,199
  • sloc: ansic: 32,035; sh: 1,491; awk: 138; makefile: 98
file content (115 lines) | stat: -rw-r--r-- 2,814 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * This file is derived from Bnews' fullname.c file.
 * Bnews is Copyright (c) 1986 by Rick Adams.
 *
 * NOTICE: THIS CODE HAS BEEN MODIFIED TO FIT THE NN ENVIRONMENT:
 *
 *	The full_name function has been rewritten entirely, although
 *	there are still some structural resemblence.
 *	Fullname checks $NAME before looking at /etc/passwd.
 *	The LOCALNAME alternative has been removed, because it would fit
 *	nn very poorly.
 *	The buildfname function is made static and moved before full_name.
 *
 * fullname.c - this file is made separate so that different local
 * conventions can be applied.  The stock version understands two
 * conventions:
 *
 * (a) Berkeley finger: the gecos field in /etc/passwd begins with
 *     the full name, terminated with comma, semicolon, or end of
 *     field.  & expands to the login name.
 * (b) BTL RJE: the gecos field looks like
 *	: junk - full name ( junk :
 *     where the "junk -" is optional.
 *
 * If you have a different local convention, modify this file accordingly.
 */

#ifdef SCCSID
static char    *SccsId = "@(#)fullname.c	1.13	11/4/87";
#endif				/* SCCSID */

#include <stdlib.h>
#include <pwd.h>
#include <string.h>
#include <ctype.h>
#include "config.h"
#include "global.h"


/* fullname.c */

static void     buildfname(register char *p, char *login, char *buf);



/*
**  BUILDFNAME -- build full name from gecos style entry.
**	(routine lifted from sendmail)
**
**	This routine interprets the strange entry that would appear
**	in the GECOS field of the password file.
**
**	Parameters:
**		p -- name to build.
**		login -- the login name of this user (for &).
**		buf -- place to put the result.
**
**	Returns:
**		none.
**
**	Side Effects:
**		none.
*/

static void
buildfname(register char *p, char *login, char *buf)
{
    register char  *bp = buf;

    if (*p == '*')
	p++;
    while (*p != '\0' && *p != ',' && *p != ';' && *p != ':' && *p != '(') {
	if (*p == '-' && (isdigit(p[-1]) || isspace(p[-1]))) {
	    bp = buf;
	    p++;
	} else if (*p == '&') {
	    strcpy(bp, login);
	    if ((bp == buf || !isalpha(bp[-1])) && islower(*bp))
		*bp = toupper(*bp);
	    while (*bp != '\0')
		bp++;
	    p++;
	} else
	    *bp++ = *p++;
    }
    *bp = '\0';
}

/*
 * Figure out who is sending the message and sign it.
 * We attempt to look up the user in the gecos field of /etc/passwd.
 */
char           *
full_name(void)
{
    static char    *fullname = NULL;
    char            inbuf[FILENAME];
    struct passwd  *pw;

    if (fullname == NULL) {
	if ((fullname = getenv("NAME")) != NULL)
	    return fullname;

	pw = getpwuid(user_id);
	if (pw == NULL)
	    return fullname = "?";

	buildfname(pw->pw_gecos, pw->pw_name, inbuf);
	if (inbuf[0] == 0)
	    strcpy(inbuf, pw->pw_name);

	fullname = copy_str(inbuf);
    }
    return fullname;
}