]> Pileus Git - ~andy/fetchmail/blob - env.c
*** empty log message ***
[~andy/fetchmail] / env.c
1 /*
2  * env.c -- small service routines
3  *
4  * Copyright 1998 by Eric S. Raymond
5  * For license terms, see the file COPYING in this directory.
6  */
7
8 #include "config.h"
9 #include <stdio.h>
10 #include <ctype.h>
11 #if defined(STDC_HEADERS)
12 #include <stdlib.h>
13 #endif
14 #if defined(HAVE_UNISTD_H)
15 #include <unistd.h>
16 #endif
17 #include <pwd.h>
18 #include <string.h>
19 #include <ctype.h>
20 #ifdef HAVE_GETHOSTBYNAME
21 #include <netdb.h>
22 #endif /* HAVE_GETHOSTBYNAME */
23 #include  <sys/types.h>
24 #include  <time.h>
25 #include "fetchmail.h"
26
27 #include "i18n.h"
28
29 extern char *getenv();  /* needed on sysV68 R3V7.1. */
30
31 extern char *program_name;
32
33 void envquery(int argc, char **argv)
34 /* set up basic stuff from the environment (including the rc file name) */
35 {
36     struct passwd *pw;
37
38     if ((program_name = strrchr(argv[0], '/')) != NULL)
39         ++program_name;
40     else
41         program_name = argv[0];
42
43     if ((user = getenv("USER")) == (char *)NULL)
44         user = getenv("LOGNAME");
45
46     if ((user == (char *)NULL) || (home = getenv("HOME")) == (char *)NULL)
47     {
48         if ((pw = getpwuid(getuid())) != NULL)
49         {
50             user = pw->pw_name;
51             home = pw->pw_dir;
52         }
53         else
54         {
55             fprintf(stderr,
56                     _("%s: can't find your name and home directory!\n"),
57                     program_name);
58             exit(PS_UNDEFINED);
59         }
60     }
61
62 #define RCFILE_NAME     ".fetchmailrc"
63     rcfile = (char *) xmalloc(strlen(home)+strlen(RCFILE_NAME)+2);
64     /* avoid //.fetchmailrc */
65     if (strcmp(home, "/") != 0) {
66         strcpy(rcfile, home);
67     } else {
68         *rcfile = '\0';
69     }
70     strcat(rcfile, "/");
71     strcat(rcfile, RCFILE_NAME);
72 }
73
74 char *host_fqdn(void)
75 /* get the FQDN of the machine we're running */
76 {
77     char        tmpbuf[HOSTLEN+1];
78
79     if (gethostname(tmpbuf, sizeof(tmpbuf)))
80     {
81         fprintf(stderr, _("%s: can't determine your host!"),
82                 program_name);
83         exit(PS_DNS);
84     }
85 #ifdef HAVE_GETHOSTBYNAME
86     /* if we got a . in the hostname assume it is a FQDN */
87     if (strchr(tmpbuf, '.') == NULL)
88     {
89         struct hostent *hp;
90
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)
94         {
95             /* exit with error message */
96             fprintf(stderr,
97                     _("gethostbyname failed for %s\n"), tmpbuf);
98             exit(PS_DNS);
99         }
100         return(xstrdup(hp->h_name));
101     }
102     else
103 #endif /* HAVE_GETHOSTBYNAME */
104         return(xstrdup(tmpbuf));
105 }
106
107 static char *tzoffset(time_t *now)
108 /* calculate timezone offset */
109 {
110     static char offset_string[6];
111     struct tm gmt, *lt;
112     int off;
113     char sign = '+';
114
115     gmt = *gmtime(now);
116     lt = localtime(now);
117     off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
118     if (lt->tm_year < gmt.tm_year)
119         off -= 24 * 60;
120     else if (lt->tm_year > gmt.tm_year)
121         off += 24 * 60;
122     else if (lt->tm_yday < gmt.tm_yday)
123         off -= 24 * 60;
124     else if (lt->tm_yday > gmt.tm_yday)
125         off += 24 * 60;
126     if (off < 0) {
127         sign = '-';
128         off = -off;
129     }
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);
134 }
135
136 char *rfc822timestamp(void)
137 /* return a timestamp in RFC822 form */
138 {
139     time_t      now;
140     static char buf[50];
141
142     time(&now);
143 #ifdef HAVE_STRFTIME
144     /*
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.
149      */
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);
153 #else
154     /*
155      * This is really just a portability fallback, as the
156      * date format ctime(3) emits is not RFC822
157      * conformant.
158      */
159     strcpy(buf, ctime(&now));
160     buf[strlen(buf)-1] = '\0';  /* remove trailing \n */
161 #endif /* HAVE_STRFTIME */
162
163     return(buf);
164 }
165
166 const char *showproto(int proto)
167 /* protocol index to protocol name mapping */
168 {
169     switch (proto)
170     {
171     case P_AUTO: return("auto");
172 #ifdef POP2_ENABLE
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");
178 #ifdef GSSAPI
179     case P_IMAP_GSS: return("IMAP-GSS");
180 #endif /* GSSAPI */
181     case P_APOP: return("APOP");
182     case P_RPOP: return("RPOP");
183     case P_ETRN: return("ETRN");
184     default: return("unknown?!?");
185     }
186 }
187
188 char *visbuf(const char *buf)
189 /* visibilize a given string */
190 {
191     static char vbuf[BUFSIZ];
192     char *tp = vbuf;
193
194     while (*buf)
195     {
196         if (*buf == '"')
197         {
198             *tp++ = '\\'; *tp++ = '"';
199             buf++;
200         }
201         else if (isprint(*buf) || *buf == ' ')
202             *tp++ = *buf++;
203         else if (*buf == '\n')
204         {
205             *tp++ = '\\'; *tp++ = 'n';
206             buf++;
207         }
208         else if (*buf == '\r')
209         {
210             *tp++ = '\\'; *tp++ = 'r';
211             buf++;
212         }
213         else if (*buf == '\b')
214         {
215             *tp++ = '\\'; *tp++ = 'b';
216             buf++;
217         }
218         else if (*buf < ' ')
219         {
220             *tp++ = '\\'; *tp++ = '^'; *tp++ = '@' + *buf;
221             buf++;
222         }
223         else
224         {
225             (void) sprintf(tp, "\\0x%02x", *buf++);
226             tp += strlen(tp);
227         }
228     }
229     *tp++ = '\0';
230     return(vbuf);
231 }
232
233 /* env.c ends here */