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