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) */
38 if ((program_name = strrchr(argv[0], '/')) != NULL)
41 program_name = argv[0];
43 if ((user = getenv("USER")) == (char *)NULL)
44 user = getenv("LOGNAME");
46 if ((user == (char *)NULL) || (home = getenv("HOME")) == (char *)NULL)
48 if ((pw = getpwuid(getuid())) != NULL)
56 _("%s: can't find your name and home directory!\n"),
62 #define RCFILE_NAME ".fetchmailrc"
63 rcfile = (char *) xmalloc(strlen(home)+strlen(RCFILE_NAME)+2);
64 /* avoid //.fetchmailrc */
65 if (strcmp(home, "/") != 0) {
71 strcat(rcfile, RCFILE_NAME);
75 /* get the FQDN of the machine we're running */
77 char tmpbuf[HOSTLEN+1];
79 if (gethostname(tmpbuf, sizeof(tmpbuf)))
81 fprintf(stderr, _("%s: can't determine your host!"),
85 #ifdef HAVE_GETHOSTBYNAME
86 /* if we got a . in the hostname assume it is a FQDN */
87 if (strchr(tmpbuf, '.') == NULL)
91 /* if we got a basename (as we do in Linux) make a FQDN of it */
92 hp = gethostbyname(tmpbuf);
93 if (hp == (struct hostent *) NULL)
95 /* exit with error message */
97 _("gethostbyname failed for %s\n"), tmpbuf);
100 return(xstrdup(hp->h_name));
103 #endif /* HAVE_GETHOSTBYNAME */
104 return(xstrdup(tmpbuf));
107 static char *tzoffset(time_t *now)
108 /* calculate timezone offset */
110 static char offset_string[6];
117 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
118 if (lt->tm_year < gmt.tm_year)
120 else if (lt->tm_year > gmt.tm_year)
122 else if (lt->tm_yday < gmt.tm_yday)
124 else if (lt->tm_yday > gmt.tm_yday)
130 if (off >= 24 * 60) /* should be impossible */
131 off = 23 * 60 + 59; /* if not, insert silly value */
132 sprintf(offset_string, "%c%02d%02d", sign, off / 60, off % 60);
133 return (offset_string);
136 char *rfc822timestamp(void)
137 /* return a timestamp in RFC822 form */
145 * Conform to RFC822. We generate a 4-digit year here, avoiding
146 * Y2K hassles. Max length of this timestamp in an English locale
147 * should be 29 chars. The only things that should vary by locale
148 * are the day and month abbreviations.
150 strftime(buf, sizeof(buf)-1,
151 "%a, %d %b %Y %H:%M:%S XXXXX (%Z)", localtime(&now));
152 strncpy(strstr(buf, "XXXXX"), tzoffset(&now), 5);
155 * This is really just a portability fallback, as the
156 * date format ctime(3) emits is not RFC822
159 strcpy(buf, ctime(&now));
160 buf[strlen(buf)-1] = '\0'; /* remove trailing \n */
161 #endif /* HAVE_STRFTIME */
166 const char *showproto(int proto)
167 /* protocol index to protocol name mapping */
171 case P_AUTO: return("auto");
173 case P_POP2: return("POP2");
174 #endif /* POP2_ENABLE */
175 case P_POP3: return("POP3");
176 case P_IMAP: return("IMAP");
177 case P_IMAP_K4: return("IMAP-K4");
179 case P_IMAP_GSS: return("IMAP-GSS");
181 case P_APOP: return("APOP");
182 case P_RPOP: return("RPOP");
183 case P_ETRN: return("ETRN");
184 default: return("unknown?!?");
188 char *visbuf(const char *buf)
189 /* visibilize a given string */
191 static char vbuf[BUFSIZ];
198 *tp++ = '\\'; *tp++ = '"';
201 else if (isprint(*buf) || *buf == ' ')
203 else if (*buf == '\n')
205 *tp++ = '\\'; *tp++ = 'n';
208 else if (*buf == '\r')
210 *tp++ = '\\'; *tp++ = 'r';
213 else if (*buf == '\b')
215 *tp++ = '\\'; *tp++ = 'b';
220 *tp++ = '\\'; *tp++ = '^'; *tp++ = '@' + *buf;
225 (void) sprintf(tp, "\\0x%02x", *buf++);
233 /* env.c ends here */