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