7 /* lookup personal name of invoking user
9 /* #include <fullname.h>
11 /* const char *fullname()
13 /* fullname() looks up the personal name of the invoking user.
14 /* The result is volatile. Make a copy if it is to be used for
15 /* an appreciable amount of time.
17 /* On UNIX systems, fullname() first tries to use the NAME environment
18 /* variable, provided that the environment can be trusted.
19 /* If that fails, fullname() extracts the username from the GECOS
20 /* field of the user's password-file entry, replacing any occurrence
21 /* of "&" by the login name, first letter capitalized.
23 /* A null result means that no full name information was found.
25 /* safe_getenv(3) safe getenv() interface
29 /* The Secure Mailer license must be distributed with this software.
32 /* IBM T.J. Watson Research
34 /* Yorktown Heights, NY 10598, USA
46 /* Utility library. */
52 /* fullname - get name of user */
54 const char *fullname(void)
56 static VSTRING
*result
;
63 result
= vstring_alloc(10);
66 * Try the environment.
68 if ((cp
= safe_getenv("NAME")) != 0)
69 return (vstring_str(vstring_strcpy(result
, cp
)));
72 * Try the password file database.
75 if ((pwd
= getpwuid(uid
)) == 0)
79 * Replace all `&' characters by the login name of this user, first
80 * letter capitalized. Although the full name comes from the protected
81 * password file, the actual data is specified by the user so we should
82 * not trust its sanity.
84 VSTRING_RESET(result
);
85 for (cp
= pwd
->pw_gecos
; (ch
= *(unsigned char *) cp
) != 0; cp
++) {
86 if (ch
== ',' || ch
== ';' || ch
== '%')
89 if (pwd
->pw_name
[0]) {
90 VSTRING_ADDCH(result
, TOUPPER(pwd
->pw_name
[0]));
91 vstring_strcat(result
, pwd
->pw_name
+ 1);
94 VSTRING_ADDCH(result
, ch
);
97 VSTRING_TERMINATE(result
);
98 return (vstring_str(result
));
105 int main(int unused_argc
, char **unused_argv
)
107 const char *cp
= fullname();
109 printf("%s\n", cp
? cp
: "null!");