2 ** mts.c -- definitions for the mail transport system
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
9 #include <h/mh.h> /* for snprintf() */
20 static void getuserinfo(void);
22 /* Cache the username and fullname of the user */
23 static char username[BUFSIZ];
24 static char fullname[BUFSIZ];
28 ** Get the fully qualified name of the local host.
33 static char buffer[BUFSIZ] = "";
34 struct addrinfo hints, *res;
36 /* check if we have cached the local name */
40 memset(buffer, 0, sizeof(buffer));
41 /* first get our local name */
42 gethostname(buffer, sizeof(buffer) - 1);
44 /* now fully qualify our name */
45 memset(&hints, 0, sizeof(hints));
46 hints.ai_flags = AI_CANONNAME;
47 hints.ai_family = PF_UNSPEC;
48 if (getaddrinfo(buffer, NULL, &hints, &res) == 0) {
49 strncpy(buffer, res->ai_canonname, sizeof(buffer) - 1);
58 ** Get the username of current user
63 if (username[0] == '\0')
71 ** Get full name of current user (typically from GECOS
72 ** field of password file).
77 if (username[0] == '\0')
85 ** Find the user's username and full name, and cache them.
97 if (!(pw = getpwuid(getuid())) || !pw->pw_name || !*pw->pw_name) {
98 strncpy(username, "unknown", sizeof(username));
99 snprintf(fullname, sizeof(fullname),
100 "The Unknown User-ID (%d)", (int)getuid());
107 ** Get the user's real name from the GECOS field. Stop once
108 ** we hit a ',', which some OSes use to separate other 'finger'
109 ** information in the GECOS field, like phone number.
111 for (cp = tmp; *np != '\0' && *np != ',';) {
115 strncpy(username, pw->pw_name, sizeof(username));
118 ** The $SIGNATURE environment variable overrides the GECOS field's
119 ** idea of your real name.
121 if ((cp = getenv("SIGNATURE")) && *cp)
122 strncpy(tmp, cp, sizeof(tmp));
124 /* quote the fullname as needed */
126 for (tp=tmp; *tp; tp++) {
128 case '(': case ')': case '<': case '>': case '[': case ']':
129 case ':': case ';': case '@': case '\\': case ',': case '.':
130 case '"': /* cf. RFC 5322 */
131 break; /* ... the switch */
133 continue; /* ... the loop */
135 /* we've found a special char */
143 for (tp=tmp; *tp; *cp++=*tp++) {
145 *cp++ = '\\'; /* prepend backslash */