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)
20 #ifdef HAVE_GETHOSTBYNAME
22 #endif /* HAVE_GETHOSTBYNAME */
23 #include <sys/types.h>
25 #include "fetchmail.h"
29 extern char *getenv(); /* needed on sysV68 R3V7.1. */
31 extern char *program_name;
33 void envquery(int argc, char **argv)
34 /* set up basic stuff from the environment (including the rc file name) */
36 struct passwd by_name, by_uid, *pwp;
38 if (!(user = getenv("LOGNAME")))
39 user = getenv("USER");
41 if (!(pwp = getpwuid(getuid())))
44 _("%s: You don't exist. Go away.\n"),
50 memcpy(&by_uid, pwp, sizeof(struct passwd));
53 else if ((pwp = getpwnam(user)))
56 * This logic is needed to handle gracefully the possibility
57 * that multiple names might be mapped to one UID
59 memcpy(&by_name, pwp, sizeof(struct passwd));
61 if (by_name.pw_uid == by_uid.pw_uid)
69 _("%s: can't find your name and home directory!\n"),
73 user = xstrdup(pwp->pw_name);
76 if (!(home = getenv("HOME")))
79 if ((program_name = strrchr(argv[0], '/')) != NULL)
82 program_name = argv[0];
84 #define RCFILE_NAME ".fetchmailrc"
85 rcfile = (char *) xmalloc(strlen(home)+strlen(RCFILE_NAME)+2);
86 /* avoid //.fetchmailrc */
87 if (strcmp(home, "/") != 0) {
93 strcat(rcfile, RCFILE_NAME);
97 /* get the FQDN of the machine we're running */
99 char tmpbuf[HOSTLEN+1];
101 if (gethostname(tmpbuf, sizeof(tmpbuf)))
103 fprintf(stderr, _("%s: can't determine your host!"),
107 #ifdef HAVE_GETHOSTBYNAME
108 /* if we got a . in the hostname assume it is a FQDN */
109 if (strchr(tmpbuf, '.') == NULL)
113 /* if we got a basename (as we do in Linux) make a FQDN of it */
114 hp = gethostbyname(tmpbuf);
115 if (hp == (struct hostent *) NULL)
117 /* exit with error message */
119 _("gethostbyname failed for %s\n"), tmpbuf);
122 return(xstrdup(hp->h_name));
125 #endif /* HAVE_GETHOSTBYNAME */
126 return(xstrdup(tmpbuf));
129 static char *tzoffset(time_t *now)
130 /* calculate timezone offset */
132 static char offset_string[6];
139 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
140 if (lt->tm_year < gmt.tm_year)
142 else if (lt->tm_year > gmt.tm_year)
144 else if (lt->tm_yday < gmt.tm_yday)
146 else if (lt->tm_yday > gmt.tm_yday)
152 if (off >= 24 * 60) /* should be impossible */
153 off = 23 * 60 + 59; /* if not, insert silly value */
154 sprintf(offset_string, "%c%02d%02d", sign, off / 60, off % 60);
155 return (offset_string);
158 char *rfc822timestamp(void)
159 /* return a timestamp in RFC822 form */
167 * Conform to RFC822. We generate a 4-digit year here, avoiding
168 * Y2K hassles. Max length of this timestamp in an English locale
169 * should be 29 chars. The only things that should vary by locale
170 * are the day and month abbreviations.
172 strftime(buf, sizeof(buf)-1,
173 "%a, %d %b %Y %H:%M:%S XXXXX (%Z)", localtime(&now));
174 strncpy(strstr(buf, "XXXXX"), tzoffset(&now), 5);
177 * This is really just a portability fallback, as the
178 * date format ctime(3) emits is not RFC822
181 strcpy(buf, ctime(&now));
182 buf[strlen(buf)-1] = '\0'; /* remove trailing \n */
183 #endif /* HAVE_STRFTIME */
188 const char *showproto(int proto)
189 /* protocol index to protocol name mapping */
193 case P_AUTO: return("auto");
195 case P_POP2: return("POP2");
196 #endif /* POP2_ENABLE */
197 case P_POP3: return("POP3");
198 case P_IMAP: return("IMAP");
199 case P_IMAP_K4: return("IMAP-K4");
201 case P_IMAP_GSS: return("IMAP-GSS");
203 case P_APOP: return("APOP");
204 case P_RPOP: return("RPOP");
205 case P_ETRN: return("ETRN");
206 default: return("unknown?!?");
210 char *visbuf(const char *buf)
211 /* visibilize a given string */
213 static char vbuf[BUFSIZ];
220 *tp++ = '\\'; *tp++ = '"';
223 else if (isprint(*buf) || *buf == ' ')
225 else if (*buf == '\n')
227 *tp++ = '\\'; *tp++ = 'n';
230 else if (*buf == '\r')
232 *tp++ = '\\'; *tp++ = 'r';
235 else if (*buf == '\b')
237 *tp++ = '\\'; *tp++ = 'b';
242 *tp++ = '\\'; *tp++ = '^'; *tp++ = '@' + *buf;
247 (void) sprintf(tp, "\\0x%02x", *buf++);
255 /* env.c ends here */