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_GETHOSTBYNAME
21 #endif /* HAVE_GETHOSTBYNAME */
22 #include <sys/types.h>
24 #include "fetchmail.h"
28 extern char *getenv(); /* needed on sysV68 R3V7.1. */
30 extern char *program_name;
32 void envquery(int argc, char **argv)
33 /* set up basic stuff from the environment (including the rc file name) */
35 struct passwd by_name, by_uid, *pwp;
37 if (!(user = getenv("LOGNAME")))
38 user = getenv("USER");
40 if (!(pwp = getpwuid(getuid())))
43 _("%s: You don't exist. Go away.\n"),
49 memcpy(&by_uid, pwp, sizeof(struct passwd));
52 else if ((pwp = getpwnam(user)))
55 * This logic is needed to handle gracefully the possibility
56 * that multiple names might be mapped to one UID
58 memcpy(&by_name, pwp, sizeof(struct passwd));
60 if (by_name.pw_uid == by_uid.pw_uid)
68 _("%s: can't find your name and home directory!\n"),
72 user = xstrdup(pwp->pw_name);
75 if (!(home = getenv("HOME")))
78 if ((program_name = strrchr(argv[0], '/')) != NULL)
81 program_name = argv[0];
83 #define RCFILE_NAME ".fetchmailrc"
84 rcfile = (char *) xmalloc(strlen(home)+strlen(RCFILE_NAME)+2);
85 /* avoid //.fetchmailrc */
86 if (strcmp(home, "/") != 0) {
92 strcat(rcfile, RCFILE_NAME);
96 /* get the FQDN of the machine we're running */
98 char tmpbuf[HOSTLEN+1];
100 if (gethostname(tmpbuf, sizeof(tmpbuf)))
102 fprintf(stderr, _("%s: can't determine your host!"),
106 #ifdef HAVE_GETHOSTBYNAME
107 /* if we got a . in the hostname assume it is a FQDN */
108 if (strchr(tmpbuf, '.') == NULL)
112 /* if we got a basename (as we do in Linux) make a FQDN of it */
113 hp = gethostbyname(tmpbuf);
114 if (hp == (struct hostent *) NULL)
116 /* exit with error message */
118 _("gethostbyname failed for %s\n"), tmpbuf);
121 return(xstrdup(hp->h_name));
124 #endif /* HAVE_GETHOSTBYNAME */
125 return(xstrdup(tmpbuf));
128 static char *tzoffset(time_t *now)
129 /* calculate timezone offset */
131 static char offset_string[6];
138 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
139 if (lt->tm_year < gmt.tm_year)
141 else if (lt->tm_year > gmt.tm_year)
143 else if (lt->tm_yday < gmt.tm_yday)
145 else if (lt->tm_yday > gmt.tm_yday)
151 if (off >= 24 * 60) /* should be impossible */
152 off = 23 * 60 + 59; /* if not, insert silly value */
153 sprintf(offset_string, "%c%02d%02d", sign, off / 60, off % 60);
154 return (offset_string);
157 char *rfc822timestamp(void)
158 /* return a timestamp in RFC822 form */
166 * Conform to RFC822. We generate a 4-digit year here, avoiding
167 * Y2K hassles. Max length of this timestamp in an English locale
168 * should be 29 chars. The only things that should vary by locale
169 * are the day and month abbreviations.
171 strftime(buf, sizeof(buf)-1,
172 "%a, %d %b %Y %H:%M:%S XXXXX (%Z)", localtime(&now));
173 strncpy(strstr(buf, "XXXXX"), tzoffset(&now), 5);
176 * This is really just a portability fallback, as the
177 * date format ctime(3) emits is not RFC822
180 strcpy(buf, ctime(&now));
181 buf[strlen(buf)-1] = '\0'; /* remove trailing \n */
182 #endif /* HAVE_STRFTIME */
187 const char *showproto(int proto)
188 /* protocol index to protocol name mapping */
192 case P_AUTO: return("auto");
194 case P_POP2: return("POP2");
195 #endif /* POP2_ENABLE */
196 case P_POP3: return("POP3");
197 case P_IMAP: return("IMAP");
198 case P_IMAP_K4: return("IMAP-K4");
200 case P_IMAP_GSS: return("IMAP-GSS");
202 case P_IMAP_CRAM_MD5: return("IMAP-LOGIN");
203 case P_IMAP_LOGIN: return("IMAP-LOGIN");
204 case P_APOP: return("APOP");
205 case P_RPOP: return("RPOP");
206 case P_ETRN: return("ETRN");
207 default: return("unknown?!?");
211 char *visbuf(const char *buf)
212 /* visibilize a given string */
214 static char vbuf[BUFSIZ];
221 *tp++ = '\\'; *tp++ = '"';
224 else if (*buf == '\\')
226 *tp++ = '\\'; *tp++ = '\\';
229 else if (isprint(*buf) || *buf == ' ')
231 else if (*buf == '\n')
233 *tp++ = '\\'; *tp++ = 'n';
236 else if (*buf == '\r')
238 *tp++ = '\\'; *tp++ = 'r';
241 else if (*buf == '\b')
243 *tp++ = '\\'; *tp++ = 'b';
248 *tp++ = '\\'; *tp++ = '^'; *tp++ = '@' + *buf;
253 (void) sprintf(tp, "\\0x%02x", *buf++);
261 /* env.c ends here */