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