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 char *rfc822timestamp(void)
108 /* return a timestamp in RFC822 form */
116 * Conform to RFC822. UTC rather than local time because of the
117 * mess that %Z generates obsolete 822 syntax but %z is not
118 * guaranteed portable. We generate a 4-digit year here, avoiding
119 * Y2K hassles. Max length of this timestamp in an English locale
120 * should be 29 chars. The only things that should vary by locale
121 * are the day and month abbreviations.
123 strftime(buf, sizeof(buf)-1,
124 "%a, %d %b %Y %H:%M:%S +0000 (UTC)",
128 * This is really just a portability fallback, as the
129 * date format ctime(3) emits is not RFC822
132 strcpy(buf, ctime(&now));
133 buf[strlen(buf)-1] = '\0'; /* remove trailing \n */
134 #endif /* HAVE_STRFTIME */
139 const char *showproto(int proto)
140 /* protocol index to protocol name mapping */
144 case P_AUTO: return("auto"); break;
146 case P_POP2: return("POP2"); break;
147 #endif /* POP2_ENABLE */
148 case P_POP3: return("POP3"); break;
149 case P_IMAP: return("IMAP"); break;
150 case P_IMAP_K4: return("IMAP-K4"); break;
152 case P_IMAP_GSS: return("IMAP-GSS"); break;
154 case P_APOP: return("APOP"); break;
155 case P_RPOP: return("RPOP"); break;
156 case P_ETRN: return("ETRN"); break;
157 default: return("unknown?!?"); break;
161 char *visbuf(const char *buf)
162 /* visibilize a given string */
164 static char vbuf[BUFSIZ];
171 *tp++ = '\\'; *tp++ = '"';
174 else if (isprint(*buf) || *buf == ' ')
176 else if (*buf == '\n')
178 *tp++ = '\\'; *tp++ = 'n';
181 else if (*buf == '\r')
183 *tp++ = '\\'; *tp++ = 'r';
186 else if (*buf == '\b')
188 *tp++ = '\\'; *tp++ = 'b';
193 *tp++ = '\\'; *tp++ = '^'; *tp++ = '@' + *buf;
198 (void) sprintf(tp, "\\0x%02x", *buf++);
206 /* env.c ends here */