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() */
23 static void getuserinfo(void);
25 /* Cache the username and fullname of the user */
26 static char username[BUFSIZ];
27 static char fullname[BUFSIZ];
31 ** Get the fully qualified name of the local host.
36 static char buffer[BUFSIZ] = "";
37 struct addrinfo hints, *res;
39 /* check if we have cached the local name */
43 memset(buffer, 0, sizeof(buffer));
44 /* first get our local name */
45 gethostname(buffer, sizeof(buffer) - 1);
47 /* now fully qualify our name */
48 memset(&hints, 0, sizeof(hints));
49 hints.ai_flags = AI_CANONNAME;
50 hints.ai_family = PF_UNSPEC;
51 if (getaddrinfo(buffer, NULL, &hints, &res) == 0) {
52 strncpy(buffer, res->ai_canonname, sizeof(buffer) - 1);
61 ** Get the username of current user
66 if (username[0] == '\0')
74 ** Get full name of current user (typically from GECOS
75 ** field of password file).
80 if (username[0] == '\0')
88 ** Find the user's username and full name, and cache them.
100 if (!(pw = getpwuid(getuid())) || !pw->pw_name || !*pw->pw_name) {
101 strncpy(username, "unknown", sizeof(username));
102 snprintf(fullname, sizeof(fullname),
103 "The Unknown User-ID (%d)", (int)getuid());
110 ** Get the user's real name from the GECOS field. Stop once
111 ** we hit a ',', which some OSes use to separate other 'finger'
112 ** information in the GECOS field, like phone number.
114 for (cp = tmp; *np != '\0' && *np != ',';) {
118 strncpy(username, pw->pw_name, sizeof(username));
121 ** The $SIGNATURE environment variable overrides the GECOS field's
122 ** idea of your real name.
124 if ((cp = getenv("SIGNATURE")) && *cp)
125 strncpy(tmp, cp, sizeof(tmp));
127 /* quote the fullname as needed */
129 for (tp=tmp; *tp; tp++) {
131 case '(': case ')': case '<': case '>': case '[': case ']':
132 case ':': case ';': case '@': case '\\': case ',': case '.':
133 case '"': /* cf. RFC 5322 */
134 break; /* ... the switch */
136 continue; /* ... the loop */
138 /* we've found a special char */
146 for (tp=tmp; *tp; *cp++=*tp++) {
148 *cp++ = '\\'; /* prepend backslash */