]> Pileus Git - ~andy/fetchmail/blob - env.c
BeOS support.
[~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 #ifdef HAVE_NET_SOCKET_H
20 #include <net/socket.h>
21 #endif
22 #ifdef HAVE_GETHOSTBYNAME
23 #include <netdb.h>
24 #endif /* HAVE_GETHOSTBYNAME */
25 #include  <sys/types.h>
26 #include  <time.h>
27 #include "fetchmail.h"
28
29 #include "i18n.h"
30
31 extern char *getenv();  /* needed on sysV68 R3V7.1. */
32
33 extern char *program_name;
34
35 void envquery(int argc, char **argv)
36 /* set up basic stuff from the environment (including the rc file name) */
37 {
38     struct passwd by_name, by_uid, *pwp;
39
40     if (!(user = getenv("LOGNAME")))
41         user = getenv("USER");
42
43     if (!(pwp = getpwuid(getuid())))
44     {
45         fprintf(stderr,
46                 _("%s: You don't exist.  Go away.\n"),
47                 program_name);
48         exit(PS_UNDEFINED);
49     }
50     else
51     {
52         memcpy(&by_uid, pwp, sizeof(struct passwd));
53         if (!user)
54             pwp = &by_uid;
55         else if ((pwp = getpwnam(user)))
56         {
57             /*
58              * This logic is needed to handle gracefully the possibility
59              * that multiple names might be mapped to one UID
60              */
61             memcpy(&by_name, pwp, sizeof(struct passwd));
62
63             if (by_name.pw_uid == by_uid.pw_uid)
64                 pwp = &by_name;
65             else
66                 pwp = &by_uid;
67         }
68         else
69         {
70             fprintf(stderr,
71                     _("%s: can't find your name and home directory!\n"),
72                     program_name);
73             exit(PS_UNDEFINED);
74         }
75         user = xstrdup(pwp->pw_name);
76     }
77
78     if (!(home = getenv("HOME")))
79         home = pwp->pw_dir;
80
81     if ((program_name = strrchr(argv[0], '/')) != NULL)
82         ++program_name;
83     else
84         program_name = argv[0];
85
86 #define RCFILE_NAME     ".fetchmailrc"
87     rcfile = (char *) xmalloc(strlen(home)+sizeof(RCFILE_NAME)+1);
88     /* avoid //.fetchmailrc */
89     if (strcmp(home, "/") != 0) {
90         strcpy(rcfile, home);
91     } else {
92         *rcfile = '\0';
93     }
94     strcat(rcfile, "/");
95     strcat(rcfile, RCFILE_NAME);
96 }
97
98 char *host_fqdn(void)
99 /* get the FQDN of the machine we're running */
100 {
101     char        tmpbuf[HOSTLEN+1];
102
103     if (gethostname(tmpbuf, sizeof(tmpbuf)))
104     {
105         fprintf(stderr, _("%s: can't determine your host!"),
106                 program_name);
107         exit(PS_DNS);
108     }
109 #ifdef HAVE_GETHOSTBYNAME
110     /* if we got a . in the hostname assume it is a FQDN */
111     if (strchr(tmpbuf, '.') == NULL)
112     {
113         struct hostent *hp;
114
115         /* if we got a basename (as we do in Linux) make a FQDN of it */
116         hp = gethostbyname(tmpbuf);
117         if (hp == (struct hostent *) NULL)
118         {
119             /* exit with error message */
120             fprintf(stderr,
121                     _("gethostbyname failed for %s\n"), tmpbuf);
122             exit(PS_DNS);
123         }
124         return(xstrdup(hp->h_name));
125     }
126     else
127 #endif /* HAVE_GETHOSTBYNAME */
128         return(xstrdup(tmpbuf));
129 }
130
131 static char *tzoffset(time_t *now)
132 /* calculate timezone offset */
133 {
134     static char offset_string[6];
135     struct tm gmt, *lt;
136     int off;
137     char sign = '+';
138
139     gmt = *gmtime(now);
140     lt = localtime(now);
141     off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
142     if (lt->tm_year < gmt.tm_year)
143         off -= 24 * 60;
144     else if (lt->tm_year > gmt.tm_year)
145         off += 24 * 60;
146     else if (lt->tm_yday < gmt.tm_yday)
147         off -= 24 * 60;
148     else if (lt->tm_yday > gmt.tm_yday)
149         off += 24 * 60;
150     if (off < 0) {
151         sign = '-';
152         off = -off;
153     }
154     if (off >= 24 * 60)                 /* should be impossible */
155         off = 23 * 60 + 59;             /* if not, insert silly value */
156     sprintf(offset_string, "%c%02d%02d", sign, off / 60, off % 60);
157     return (offset_string);
158 }
159
160 char *rfc822timestamp(void)
161 /* return a timestamp in RFC822 form */
162 {
163     time_t      now;
164     static char buf[50];
165
166     time(&now);
167 #ifdef HAVE_STRFTIME
168     /*
169      * Conform to RFC822.  We generate a 4-digit year here, avoiding
170      * Y2K hassles.  Max length of this timestamp in an English locale
171      * should be 29 chars.  The only things that should vary by locale
172      * are the day and month abbreviations.
173      */
174     strftime(buf, sizeof(buf)-1, 
175              "%a, %d %b %Y %H:%M:%S XXXXX (%Z)", localtime(&now));
176     strncpy(strstr(buf, "XXXXX"), tzoffset(&now), 5);
177 #else
178     /*
179      * This is really just a portability fallback, as the
180      * date format ctime(3) emits is not RFC822
181      * conformant.
182      */
183     strcpy(buf, ctime(&now));
184     buf[strlen(buf)-1] = '\0';  /* remove trailing \n */
185 #endif /* HAVE_STRFTIME */
186
187     return(buf);
188 }
189
190 const char *showproto(int proto)
191 /* protocol index to protocol name mapping */
192 {
193     switch (proto)
194     {
195     case P_AUTO: return("auto");
196 #ifdef POP2_ENABLE
197     case P_POP2: return("POP2");
198 #endif /* POP2_ENABLE */
199     case P_POP3: return("POP3");
200     case P_IMAP: return("IMAP");
201     case P_IMAP_K4: return("IMAP-K4");
202 #ifdef GSSAPI
203     case P_IMAP_GSS: return("IMAP-GSS");
204 #endif /* GSSAPI */
205     case P_IMAP_CRAM_MD5: return("IMAP-LOGIN");
206     case P_IMAP_LOGIN: return("IMAP-LOGIN");
207     case P_APOP: return("APOP");
208     case P_RPOP: return("RPOP");
209     case P_ETRN: return("ETRN");
210     default: return("unknown?!?");
211     }
212 }
213
214 char *visbuf(const char *buf)
215 /* visibilize a given string */
216 {
217     static char vbuf[BUFSIZ];
218     char *tp = vbuf;
219
220     while (*buf)
221     {
222         if (*buf == '"')
223         {
224             *tp++ = '\\'; *tp++ = '"';
225             buf++;
226         }
227         else if (*buf == '\\')
228         {
229             *tp++ = '\\'; *tp++ = '\\';
230             buf++;
231         }
232         else if (isprint(*buf) || *buf == ' ')
233             *tp++ = *buf++;
234         else if (*buf == '\n')
235         {
236             *tp++ = '\\'; *tp++ = 'n';
237             buf++;
238         }
239         else if (*buf == '\r')
240         {
241             *tp++ = '\\'; *tp++ = 'r';
242             buf++;
243         }
244         else if (*buf == '\b')
245         {
246             *tp++ = '\\'; *tp++ = 'b';
247             buf++;
248         }
249         else if (*buf < ' ')
250         {
251             *tp++ = '\\'; *tp++ = '^'; *tp++ = '@' + *buf;
252             buf++;
253         }
254         else
255         {
256             (void) sprintf(tp, "\\0x%02x", *buf++);
257             tp += strlen(tp);
258         }
259     }
260     *tp++ = '\0';
261     return(vbuf);
262 }
263
264 /* env.c ends here */