]> Pileus Git - ~andy/fetchmail/blob - env.c
95d93be234cde67b4664591c8f0d068e7b121287
[~andy/fetchmail] / env.c
1 /*
2  * env.c -- small service routines
3  *
4  * For license terms, see the file COPYING in this directory.
5  *
6  * i18n by Arnaldo Carvalho de Melo <acme@conectiva.com.br> 7-Nov-1998
7  */
8
9 #include "config.h"
10 #include <stdio.h>
11 #include <ctype.h>
12 #if defined(STDC_HEADERS)
13 #include <stdlib.h>
14 #endif
15 #if defined(HAVE_UNISTD_H)
16 #include <unistd.h>
17 #endif
18 #include <pwd.h>
19 #include <string.h>
20 #include <ctype.h>
21 #ifdef HAVE_GETHOSTBYNAME
22 #include <netdb.h>
23 #endif /* HAVE_GETHOSTBYNAME */
24 #ifndef HAVE_STRFTIME /* For ctime prototype */
25 #include  <sys/types.h>
26 #include  <time.h>
27 #endif
28 #include "fetchmail.h"
29
30 #include "i18n.h"
31
32 extern char *getenv();  /* needed on sysV68 R3V7.1. */
33
34 extern char *program_name;
35
36 void envquery(int argc, char **argv)
37 /* set up basic stuff from the environment (including the rc file name) */
38 {
39     struct passwd *pw;
40
41     if ((program_name = strrchr(argv[0], '/')) != NULL)
42         ++program_name;
43     else
44         program_name = argv[0];
45
46     if ((user = getenv("USER")) == (char *)NULL)
47         user = getenv("LOGNAME");
48
49     if ((user == (char *)NULL) || (home = getenv("HOME")) == (char *)NULL)
50     {
51         if ((pw = getpwuid(getuid())) != NULL)
52         {
53             user = pw->pw_name;
54             home = pw->pw_dir;
55         }
56         else
57         {
58             fprintf(stderr,
59                     _("%s: can't find your name and home directory!\n"),
60                     program_name);
61             exit(PS_UNDEFINED);
62         }
63     }
64
65 #define RCFILE_NAME     ".fetchmailrc"
66     rcfile = (char *) xmalloc(strlen(home)+strlen(RCFILE_NAME)+2);
67     /* avoid //.fetchmailrc */
68     if (strcmp(home, "/") != 0) {
69         strcpy(rcfile, home);
70     } else {
71         *rcfile = '\0';
72     }
73     strcat(rcfile, "/");
74     strcat(rcfile, RCFILE_NAME);
75 }
76
77 char *host_fqdn(void)
78 /* get the FQDN of the machine we're running */
79 {
80     char        tmpbuf[HOSTLEN+1];
81
82     if (gethostname(tmpbuf, sizeof(tmpbuf)))
83     {
84         fprintf(stderr, _("%s: can't determine your host!"),
85                 program_name);
86         exit(PS_DNS);
87     }
88 #ifdef HAVE_GETHOSTBYNAME
89     /* if we got a . in the hostname assume it is a FQDN */
90     if (strchr(tmpbuf, '.') == NULL)
91     {
92         struct hostent *hp;
93
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)
97         {
98             /* exit with error message */
99             fprintf(stderr,
100                     _("gethostbyname failed for %s\n"), tmpbuf);
101             exit(PS_DNS);
102         }
103         return(xstrdup(hp->h_name));
104     }
105     else
106 #endif /* HAVE_GETHOSTBYNAME */
107         return(xstrdup(tmpbuf));
108 }
109
110 char *rfc822timestamp(void)
111 /* return a timestamp in RFC822 form */
112 {
113     time_t      now;
114     static char buf[40];
115
116     time(&now);
117 #ifdef HAVE_STRFTIME
118     /*
119      * Conform to RFC822.  This is typically going to emit
120      * a three-letter timezone for %Z, which is going to
121      * be marked "obsolete syntax" in 822bis.  Note that we
122      * generate a 4-digit year here, avoiding Y2K hassles.
123      * Note: max length of this timestamp in an English locale
124      * should be 29 chars, assuming a 3-character timezone.
125      */
126     strftime(buf, sizeof(buf)-1, 
127              "%a, %d %b %Y %H:%M:%S %Z", localtime(&now));
128 #else
129     /*
130      * This is really just a portability fallback, as the
131      * date format ctime(3) emits is not RFC822
132      * conformant.
133      */
134     strcpy(buf, ctime(&now));
135     buf[strlen(buf)-1] = '\0';  /* remove trailing \n */
136 #endif /* HAVE_STRFTIME */
137
138     return(buf);
139 }
140
141 const char *showproto(int proto)
142 /* protocol index to protocol name mapping */
143 {
144     switch (proto)
145     {
146     case P_AUTO: return("auto"); break;
147 #ifdef POP2_ENABLE
148     case P_POP2: return("POP2"); break;
149 #endif /* POP2_ENABLE */
150     case P_POP3: return("POP3"); break;
151     case P_IMAP: return("IMAP"); break;
152     case P_IMAP_K4: return("IMAP-K4"); break;
153 #ifdef GSSAPI
154     case P_IMAP_GSS: return("IMAP-GSS"); break;
155 #endif /* GSSAPI */
156     case P_APOP: return("APOP"); break;
157     case P_RPOP: return("RPOP"); break;
158     case P_ETRN: return("ETRN"); break;
159     default: return("unknown?!?"); break;
160     }
161 }
162
163 char *visbuf(const char *buf)
164 /* visibilize a given string */
165 {
166     static char vbuf[BUFSIZ];
167     char *tp = vbuf;
168
169     while (*buf)
170     {
171         if (*buf == '"')
172         {
173             *tp++ = '\\'; *tp++ = '"';
174             buf++;
175         }
176         else if (isprint(*buf) || *buf == ' ')
177             *tp++ = *buf++;
178         else if (*buf == '\n')
179         {
180             *tp++ = '\\'; *tp++ = 'n';
181             buf++;
182         }
183         else if (*buf == '\r')
184         {
185             *tp++ = '\\'; *tp++ = 'r';
186             buf++;
187         }
188         else if (*buf == '\b')
189         {
190             *tp++ = '\\'; *tp++ = 'b';
191             buf++;
192         }
193         else if (*buf < ' ')
194         {
195             *tp++ = '\\'; *tp++ = '^'; *tp++ = '@' + *buf;
196             buf++;
197         }
198         else
199         {
200             (void) sprintf(tp, "\\0x%02x", *buf++);
201             tp += strlen(tp);
202         }
203     }
204     *tp++ = '\0';
205     return(vbuf);
206 }
207
208 /* env.c ends here */