]> Pileus Git - ~andy/fetchmail/blob - env.c
fc2dd22fdd3bfe8b35dec5d4dd0f9403f989d612
[~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 char *rfc822timestamp(void)
108 /* return a timestamp in RFC822 form */
109 {
110     time_t      now;
111     static char buf[40];
112
113     time(&now);
114 #ifdef HAVE_STRFTIME
115     /*
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.
122      */
123     strftime(buf, sizeof(buf)-1, 
124              "%a, %d %b %Y %H:%M:%S +0000 (UTC)",
125              gmtime(&now));
126 #else
127     /*
128      * This is really just a portability fallback, as the
129      * date format ctime(3) emits is not RFC822
130      * conformant.
131      */
132     strcpy(buf, ctime(&now));
133     buf[strlen(buf)-1] = '\0';  /* remove trailing \n */
134 #endif /* HAVE_STRFTIME */
135
136     return(buf);
137 }
138
139 const char *showproto(int proto)
140 /* protocol index to protocol name mapping */
141 {
142     switch (proto)
143     {
144     case P_AUTO: return("auto"); break;
145 #ifdef POP2_ENABLE
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;
151 #ifdef GSSAPI
152     case P_IMAP_GSS: return("IMAP-GSS"); break;
153 #endif /* GSSAPI */
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;
158     }
159 }
160
161 char *visbuf(const char *buf)
162 /* visibilize a given string */
163 {
164     static char vbuf[BUFSIZ];
165     char *tp = vbuf;
166
167     while (*buf)
168     {
169         if (*buf == '"')
170         {
171             *tp++ = '\\'; *tp++ = '"';
172             buf++;
173         }
174         else if (isprint(*buf) || *buf == ' ')
175             *tp++ = *buf++;
176         else if (*buf == '\n')
177         {
178             *tp++ = '\\'; *tp++ = 'n';
179             buf++;
180         }
181         else if (*buf == '\r')
182         {
183             *tp++ = '\\'; *tp++ = 'r';
184             buf++;
185         }
186         else if (*buf == '\b')
187         {
188             *tp++ = '\\'; *tp++ = 'b';
189             buf++;
190         }
191         else if (*buf < ' ')
192         {
193             *tp++ = '\\'; *tp++ = '^'; *tp++ = '@' + *buf;
194             buf++;
195         }
196         else
197         {
198             (void) sprintf(tp, "\\0x%02x", *buf++);
199             tp += strlen(tp);
200         }
201     }
202     *tp++ = '\0';
203     return(vbuf);
204 }
205
206 /* env.c ends here */