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"
30 #if defined(HAVE_SETLOCALE) && defined(ENABLE_NLS) && defined(HAVE_STRFTIME)
35 extern char *getenv(); /* needed on sysV68 R3V7.1. */
37 extern char *program_name;
39 void envquery(int argc, char **argv)
40 /* set up basic stuff from the environment (including the rc file name) */
42 struct passwd by_name, by_uid, *pwp;
44 if (!(user = getenv("FETCHMAILUSER")))
46 if (!(user = getenv("LOGNAME")))
48 user = getenv("USER");
52 if (!(pwp = getpwuid(getuid())))
55 _("%s: You don't exist. Go away.\n"),
61 memcpy(&by_uid, pwp, sizeof(struct passwd));
62 if (!user || !(pwp = getpwnam(user)))
67 * This logic is needed to handle gracefully the possibility
68 * that multiple names might be mapped to one UID.
70 memcpy(&by_name, pwp, sizeof(struct passwd));
72 if (by_name.pw_uid == by_uid.pw_uid)
77 user = xstrdup(pwp->pw_name);
80 /* compute user's home directory */
81 if (!(home = getenv("HOME")))
84 /* compute fetchmail's home directory */
85 if (!(fmhome = getenv("FETCHMAILHOME")))
88 if ((program_name = strrchr(argv[0], '/')) != NULL)
91 program_name = argv[0];
93 #define RCFILE_NAME "fetchmailrc"
95 * The (fmhome==home) leaves an extra character for a . at the
96 * beginning of the rc file's name, iff fetchmail is using $HOME
97 * for its files. We don't want to do that if fetchmail has its
98 * own home ($FETCHMAILHOME), however.
100 rcfile = (char *)xmalloc(strlen(fmhome)+sizeof(RCFILE_NAME)+(fmhome==home)+2);
101 /* avoid //.fetchmailrc */
102 if (strcmp(fmhome, "/") != 0)
103 strcpy(rcfile, fmhome);
107 if (rcfile[strlen(rcfile) - 1] != '/')
111 strcat(rcfile, RCFILE_NAME);
114 char *host_fqdn(void)
115 /* get the FQDN of the machine we're running */
117 char tmpbuf[HOSTLEN+1];
119 if (gethostname(tmpbuf, sizeof(tmpbuf)))
121 fprintf(stderr, _("%s: can't determine your host!"),
125 #ifdef HAVE_GETHOSTBYNAME
126 /* if we got a . in the hostname assume it is a FQDN */
127 if (strchr(tmpbuf, '.') == NULL)
131 /* if we got a basename (as we do in Linux) make a FQDN of it */
132 hp = gethostbyname(tmpbuf);
133 if (hp == (struct hostent *) NULL)
135 /* exit with error message */
137 _("gethostbyname failed for %s\n"), tmpbuf);
140 return(xstrdup(hp->h_name));
143 #endif /* HAVE_GETHOSTBYNAME */
144 return(xstrdup(tmpbuf));
147 static char *tzoffset(time_t *now)
148 /* calculate timezone offset */
150 static char offset_string[6];
157 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
158 if (lt->tm_year < gmt.tm_year)
160 else if (lt->tm_year > gmt.tm_year)
162 else if (lt->tm_yday < gmt.tm_yday)
164 else if (lt->tm_yday > gmt.tm_yday)
170 if (off >= 24 * 60) /* should be impossible */
171 off = 23 * 60 + 59; /* if not, insert silly value */
172 sprintf(offset_string, "%c%02d%02d", sign, off / 60, off % 60);
173 return (offset_string);
176 char *rfc822timestamp(void)
177 /* return a timestamp in RFC822 form */
185 * Conform to RFC822. We generate a 4-digit year here, avoiding
186 * Y2K hassles. Max length of this timestamp in an English locale
187 * should be 29 chars. The only things that should vary by locale
188 * are the day and month abbreviations. The set_locale calls prevent
189 * weird multibyte i18n characters (such as kanji) from showing up
190 * in your Received headers.
192 #if defined(HAVE_SETLOCALE) && defined(ENABLE_NLS) && defined(HAVE_STRFTIME)
193 setlocale (LC_TIME, "C");
195 strftime(buf, sizeof(buf)-1,
196 "%a, %d %b %Y %H:%M:%S XXXXX (%Z)", localtime(&now));
197 #if defined(HAVE_SETLOCALE) && defined(ENABLE_NLS) && defined(HAVE_STRFTIME)
198 setlocale (LC_TIME, "");
200 strncpy(strstr(buf, "XXXXX"), tzoffset(&now), 5);
203 * This is really just a portability fallback, as the
204 * date format ctime(3) emits is not RFC822
207 strcpy(buf, ctime(&now));
208 buf[strlen(buf)-1] = '\0'; /* remove trailing \n */
209 #endif /* HAVE_STRFTIME */
214 const char *showproto(int proto)
215 /* protocol index to protocol name mapping */
219 case P_AUTO: return("auto");
221 case P_POP2: return("POP2");
222 #endif /* POP2_ENABLE */
224 case P_POP3: return("POP3");
225 case P_APOP: return("APOP");
226 case P_RPOP: return("RPOP");
227 #endif /* POP3_ENABLE */
229 case P_IMAP: return("IMAP");
230 #endif /* IMAP_ENABLE */
232 case P_ETRN: return("ETRN");
233 #endif /* ETRN_ENABLE */
235 case P_ODMR: return("ODMR");
236 #endif /* ODMR_ENABLE */
237 default: return("unknown?!?");
241 char *visbuf(const char *buf)
242 /* visibilize a given string */
244 static char vbuf[BUFSIZ];
251 *tp++ = '\\'; *tp++ = '"';
254 else if (*buf == '\\')
256 *tp++ = '\\'; *tp++ = '\\';
259 else if (isprint(*buf) || *buf == ' ')
261 else if (*buf == '\n')
263 *tp++ = '\\'; *tp++ = 'n';
266 else if (*buf == '\r')
268 *tp++ = '\\'; *tp++ = 'r';
271 else if (*buf == '\b')
273 *tp++ = '\\'; *tp++ = 'b';
278 *tp++ = '\\'; *tp++ = '^'; *tp++ = '@' + *buf;
283 (void) sprintf(tp, "\\0x%02x", *buf++);
291 /* env.c ends here */