2 * env.c -- small service routines
4 * For license terms, see the file COPYING in this directory.
6 * i18n by Arnaldo Carvalho de Melo <acme@conectiva.com.br> 7-Nov-1998
12 #if defined(STDC_HEADERS)
15 #if defined(HAVE_UNISTD_H)
21 #ifdef HAVE_GETHOSTBYNAME
23 #endif /* HAVE_GETHOSTBYNAME */
24 #ifndef HAVE_STRFTIME /* For ctime prototype */
25 #include <sys/types.h>
28 #include "fetchmail.h"
32 extern char *getenv(); /* needed on sysV68 R3V7.1. */
34 extern char *program_name;
36 void envquery(int argc, char **argv)
37 /* set up basic stuff from the environment (including the rc file name) */
41 if ((program_name = strrchr(argv[0], '/')) != NULL)
44 program_name = argv[0];
46 if ((user = getenv("USER")) == (char *)NULL)
47 user = getenv("LOGNAME");
49 if ((user == (char *)NULL) || (home = getenv("HOME")) == (char *)NULL)
51 if ((pw = getpwuid(getuid())) != NULL)
59 _("%s: can't find your name and home directory!\n"),
65 #define RCFILE_NAME ".fetchmailrc"
66 rcfile = (char *) xmalloc(strlen(home)+strlen(RCFILE_NAME)+2);
67 /* avoid //.fetchmailrc */
68 if (strcmp(home, "/") != 0) {
74 strcat(rcfile, RCFILE_NAME);
78 /* get the FQDN of the machine we're running */
80 char tmpbuf[HOSTLEN+1];
82 if (gethostname(tmpbuf, sizeof(tmpbuf)))
84 fprintf(stderr, _("%s: can't determine your host!"),
88 #ifdef HAVE_GETHOSTBYNAME
89 /* if we got a . in the hostname assume it is a FQDN */
90 if (strchr(tmpbuf, '.') == NULL)
94 /* if we got a basename (as we do in Linux) make a FQDN of it */
95 hp = gethostbyname(tmpbuf);
96 if (hp == (struct hostent *) NULL)
98 /* exit with error message */
100 _("gethostbyname failed for %s\n"), tmpbuf);
103 return(xstrdup(hp->h_name));
106 #endif /* HAVE_GETHOSTBYNAME */
107 return(xstrdup(tmpbuf));
110 char *rfc822timestamp(void)
111 /* return a timestamp in RFC822 form */
119 * Conform to RFC822. UTC rather than local time because of the
120 * mess that %Z generates obsolete 822 syntax but %z is not
121 * guaranteed portable. We generate a 4-digit year here, avoiding
122 * Y2K hassles. Max length of this timestamp in an English locale
123 * should be 29 chars. The only things that should vary by locale
124 * are the day and month abbreviations.
126 strftime(buf, sizeof(buf)-1,
127 "%a, %d %b %Y %H:%M:%S +0000 (UTC)",
131 * This is really just a portability fallback, as the
132 * date format ctime(3) emits is not RFC822
135 strcpy(buf, ctime(&now));
136 buf[strlen(buf)-1] = '\0'; /* remove trailing \n */
137 #endif /* HAVE_STRFTIME */
142 const char *showproto(int proto)
143 /* protocol index to protocol name mapping */
147 case P_AUTO: return("auto"); break;
149 case P_POP2: return("POP2"); break;
150 #endif /* POP2_ENABLE */
151 case P_POP3: return("POP3"); break;
152 case P_IMAP: return("IMAP"); break;
153 case P_IMAP_K4: return("IMAP-K4"); break;
155 case P_IMAP_GSS: return("IMAP-GSS"); break;
157 case P_APOP: return("APOP"); break;
158 case P_RPOP: return("RPOP"); break;
159 case P_ETRN: return("ETRN"); break;
160 default: return("unknown?!?"); break;
164 char *visbuf(const char *buf)
165 /* visibilize a given string */
167 static char vbuf[BUFSIZ];
174 *tp++ = '\\'; *tp++ = '"';
177 else if (isprint(*buf) || *buf == ' ')
179 else if (*buf == '\n')
181 *tp++ = '\\'; *tp++ = 'n';
184 else if (*buf == '\r')
186 *tp++ = '\\'; *tp++ = 'r';
189 else if (*buf == '\b')
191 *tp++ = '\\'; *tp++ = 'b';
196 *tp++ = '\\'; *tp++ = '^'; *tp++ = '@' + *buf;
201 (void) sprintf(tp, "\\0x%02x", *buf++);
209 /* env.c ends here */