]> Pileus Git - ~andy/fetchmail/blob - env.c
06f9da672aaa81f098136d318f1e3515a0aaca2e
[~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 "fetchmail.h"
9 #include <stdio.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
23 extern char *getenv();  /* needed on sysV68 R3V7.1. */
24
25 char *user, *home, *fetchmailhost;
26
27 extern char *program_name;
28
29 void envquery(int argc, char **argv)
30 /* set up basic stuff from the environment (including the rc file name) */
31 {
32     char *tmpdir, tmpbuf[BUFSIZ]; 
33     struct passwd *pw;
34     struct query *ctl;
35
36     if ((program_name = strrchr(argv[0], '/')) != NULL)
37         ++program_name;
38     else
39         program_name = argv[0];
40
41     if ((user = getenv("USER")) == (char *)NULL)
42         user = getenv("LOGNAME");
43
44     if ((user == (char *)NULL) || (home = getenv("HOME")) == (char *)NULL)
45     {
46         if ((pw = getpwuid(getuid())) != NULL)
47         {
48             user = pw->pw_name;
49             home = pw->pw_dir;
50         }
51         else
52         {
53             fprintf(stderr,
54                     "%s: can't find your name and home directory!\n",
55                     program_name);
56             exit(PS_UNDEFINED);
57         }
58     }
59
60     /* we'll need this for the SMTP forwarding target and error messages */
61     if (gethostname(tmpbuf, sizeof(tmpbuf)))
62     {
63         fprintf(stderr, "%s: can't determine your host!", program_name);
64         exit(PS_IOERR);
65     }
66 #ifdef HAVE_GETHOSTBYNAME
67     {
68         struct hostent *hp;
69
70         /* in case we got a basename (as we do in Linux) make a FQDN of it */
71         hp = gethostbyname(tmpbuf);
72         if (hp == (struct hostent *) NULL)
73         {
74             /* exit with error message */
75             fprintf(stderr, "gethostbyname failed for %s", tmpbuf);
76             exit(PS_DNS);
77         }
78         strcpy(tmpbuf, hp->h_name);
79     }
80 #endif /* HAVE_GETHOSTBYNAME */
81     fetchmailhost = xstrdup(tmpbuf);
82
83 #define RCFILE_NAME     ".fetchmailrc"
84     rcfile = (char *) xmalloc(strlen(home)+strlen(RCFILE_NAME)+2);
85     strcpy(rcfile, home);
86     strcat(rcfile, "/");
87     strcat(rcfile, RCFILE_NAME);
88 }
89
90 char *showproto(int proto)
91 /* protocol index to protocol name mapping */
92 {
93     switch (proto)
94     {
95     case P_AUTO: return("auto"); break;
96 #ifdef POP2_ENABLE
97     case P_POP2: return("POP2"); break;
98 #endif /* POP2_ENABLE */
99     case P_POP3: return("POP3"); break;
100     case P_IMAP: return("IMAP"); break;
101     case P_IMAP_K4: return("IMAP-K4"); break;
102     case P_APOP: return("APOP"); break;
103     case P_RPOP: return("RPOP"); break;
104     case P_ETRN: return("ETRN"); break;
105     default: return("unknown?!?"); break;
106     }
107 }
108
109 char *visbuf(const char *buf)
110 /* visibilize a given string */
111 {
112     static char vbuf[BUFSIZ];
113     char *tp = vbuf;
114
115     while (*buf)
116     {
117         if (isprint(*buf) || *buf == ' ')
118             *tp++ = *buf++;
119         else if (*buf == '\n')
120         {
121             *tp++ = '\\'; *tp++ = 'n';
122             buf++;
123         }
124         else if (*buf == '\r')
125         {
126             *tp++ = '\\'; *tp++ = 'r';
127             buf++;
128         }
129         else if (*buf == '\b')
130         {
131             *tp++ = '\\'; *tp++ = 'b';
132             buf++;
133         }
134         else if (*buf < ' ')
135         {
136             *tp++ = '\\'; *tp++ = '^'; *tp++ = '@' + *buf;
137             buf++;
138         }
139         else
140         {
141             (void) sprintf(tp, "\\0x%02x", *buf++);
142             tp += strlen(tp);
143         }
144     }
145     *tp++ = '\0';
146     return(vbuf);
147 }
148
149 /* env.c ends here */