2 * env.c -- small service routines
4 * Copyright 1998 by Eric S. Raymond
5 * For license terms, see the file COPYING in this directory.
11 #if defined(STDC_HEADERS)
14 #if defined(HAVE_UNISTD_H)
19 #ifdef HAVE_NET_SOCKET_H
20 #include <net/socket.h>
22 #ifdef HAVE_GETHOSTBYNAME
24 #endif /* HAVE_GETHOSTBYNAME */
25 #include <sys/types.h>
27 #include "fetchmail.h"
31 extern char *getenv(); /* needed on sysV68 R3V7.1. */
33 extern char *program_name;
35 void envquery(int argc, char **argv)
36 /* set up basic stuff from the environment (including the rc file name) */
38 struct passwd by_name, by_uid, *pwp;
40 (user = getenv("FETCHMAILUSER"))
41 || (user = getenv("LOGNAME"))
42 || (user = getenv("USER"));
44 if (!(pwp = getpwuid(getuid())))
47 _("%s: You don't exist. Go away.\n"),
53 memcpy(&by_uid, pwp, sizeof(struct passwd));
54 if (!user || !(pwp = getpwnam(user)))
59 * This logic is needed to handle gracefully the possibility
60 * that multiple names might be mapped to one UID.
62 memcpy(&by_name, pwp, sizeof(struct passwd));
64 if (by_name.pw_uid == by_uid.pw_uid)
69 user = xstrdup(pwp->pw_name);
72 /* compute user's home directory */
73 if (!(home = getenv("HOME")))
76 /* compute fetchmail's home directory */
77 if (!(fmhome = getenv("FETCHMAILHOME")))
80 if ((program_name = strrchr(argv[0], '/')) != NULL)
83 program_name = argv[0];
85 #define RCFILE_NAME "fetchmailrc"
87 * The (fmhome==home) leaves an extra character for a . at the
88 * beginning of the rc file's name, iff fetchmail is using $HOME
89 * for its files. We don't want to do that if fetchmail has its
90 * own home ($FETCHMAILHOME), however.
92 rcfile = (char *)xmalloc(strlen(fmhome)+sizeof(RCFILE_NAME)+(fmhome==home)+2);
93 /* avoid //.fetchmailrc */
94 if (strcmp(fmhome, "/") != 0)
95 strcpy(rcfile, fmhome);
99 if (rcfile[strlen(rcfile) - 1] != '/')
103 strcat(rcfile, RCFILE_NAME);
106 char *host_fqdn(void)
107 /* get the FQDN of the machine we're running */
109 char tmpbuf[HOSTLEN+1];
111 if (gethostname(tmpbuf, sizeof(tmpbuf)))
113 fprintf(stderr, _("%s: can't determine your host!"),
117 #ifdef HAVE_GETHOSTBYNAME
118 /* if we got a . in the hostname assume it is a FQDN */
119 if (strchr(tmpbuf, '.') == NULL)
123 /* if we got a basename (as we do in Linux) make a FQDN of it */
124 hp = gethostbyname(tmpbuf);
125 if (hp == (struct hostent *) NULL)
127 /* exit with error message */
129 _("gethostbyname failed for %s\n"), tmpbuf);
132 return(xstrdup(hp->h_name));
135 #endif /* HAVE_GETHOSTBYNAME */
136 return(xstrdup(tmpbuf));
139 static char *tzoffset(time_t *now)
140 /* calculate timezone offset */
142 static char offset_string[6];
149 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
150 if (lt->tm_year < gmt.tm_year)
152 else if (lt->tm_year > gmt.tm_year)
154 else if (lt->tm_yday < gmt.tm_yday)
156 else if (lt->tm_yday > gmt.tm_yday)
162 if (off >= 24 * 60) /* should be impossible */
163 off = 23 * 60 + 59; /* if not, insert silly value */
164 sprintf(offset_string, "%c%02d%02d", sign, off / 60, off % 60);
165 return (offset_string);
168 char *rfc822timestamp(void)
169 /* return a timestamp in RFC822 form */
177 * Conform to RFC822. We generate a 4-digit year here, avoiding
178 * Y2K hassles. Max length of this timestamp in an English locale
179 * should be 29 chars. The only things that should vary by locale
180 * are the day and month abbreviations. The set_locale calls prevent
181 * weird multibyte i18n characters (such as kanji) for showing up
182 * in your Received headers.
184 setlocale (LC_TIME, "C");
185 strftime(buf, sizeof(buf)-1,
186 "%a, %d %b %Y %H:%M:%S XXXXX (%Z)", localtime(&now));
187 setlocale (LC_TIME, "");
188 strncpy(strstr(buf, "XXXXX"), tzoffset(&now), 5);
191 * This is really just a portability fallback, as the
192 * date format ctime(3) emits is not RFC822
195 strcpy(buf, ctime(&now));
196 buf[strlen(buf)-1] = '\0'; /* remove trailing \n */
197 #endif /* HAVE_STRFTIME */
202 const char *showproto(int proto)
203 /* protocol index to protocol name mapping */
207 case P_AUTO: return("auto");
209 case P_POP2: return("POP2");
210 #endif /* POP2_ENABLE */
212 case P_POP3: return("POP3");
213 case P_APOP: return("APOP");
214 case P_RPOP: return("RPOP");
215 #endif /* POP3_ENABLE */
217 case P_IMAP: return("IMAP");
218 #endif /* IMAP_ENABLE */
220 case P_ETRN: return("ETRN");
221 #endif /* ETRN_ENABLE */
223 case P_ODMR: return("ODMR");
224 #endif /* ODMR_ENABLE */
225 default: return("unknown?!?");
229 char *visbuf(const char *buf)
230 /* visibilize a given string */
232 static char vbuf[BUFSIZ];
239 *tp++ = '\\'; *tp++ = '"';
242 else if (*buf == '\\')
244 *tp++ = '\\'; *tp++ = '\\';
247 else if (isprint(*buf) || *buf == ' ')
249 else if (*buf == '\n')
251 *tp++ = '\\'; *tp++ = 'n';
254 else if (*buf == '\r')
256 *tp++ = '\\'; *tp++ = 'r';
259 else if (*buf == '\b')
261 *tp++ = '\\'; *tp++ = 'b';
266 *tp++ = '\\'; *tp++ = '^'; *tp++ = '@' + *buf;
271 (void) sprintf(tp, "\\0x%02x", *buf++);
279 /* env.c ends here */