]> Pileus Git - ~andy/fetchmail/blob - env.c
Jonathan T. Agnew's massive code cleanup.
[~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 #include "fetchmail.h"
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     struct passwd *pw;
32
33     if ((program_name = strrchr(argv[0], '/')) != NULL)
34         ++program_name;
35     else
36         program_name = argv[0];
37
38     if ((user = getenv("USER")) == (char *)NULL)
39         user = getenv("LOGNAME");
40
41     if ((user == (char *)NULL) || (home = getenv("HOME")) == (char *)NULL)
42     {
43         if ((pw = getpwuid(getuid())) != NULL)
44         {
45             user = pw->pw_name;
46             home = pw->pw_dir;
47         }
48         else
49         {
50             fprintf(stderr,
51                     "%s: can't find your name and home directory!\n",
52                     program_name);
53             exit(PS_UNDEFINED);
54         }
55     }
56
57 #define RCFILE_NAME     ".fetchmailrc"
58     rcfile = (char *) xmalloc(strlen(home)+strlen(RCFILE_NAME)+2);
59     /* avoid //.fetchmailrc */
60     if (strcmp(home, "/") != 0) {
61         strcpy(rcfile, home);
62     } else {
63         *rcfile = '\0';
64     }
65     strcat(rcfile, "/");
66     strcat(rcfile, RCFILE_NAME);
67 }
68
69 char *host_fqdn(void)
70 /* get the FQDN of the machine we're running */
71 {
72     char        tmpbuf[HOSTLEN+1];
73
74     if (gethostname(tmpbuf, sizeof(tmpbuf)))
75     {
76         fprintf(stderr, "%s: can't determine your host!",
77                 program_name);
78         exit(PS_DNS);
79     }
80 #ifdef HAVE_GETHOSTBYNAME
81     /* if we got a . in the hostname assume it is a FQDN */
82     if (strchr(tmpbuf, '.') == NULL)
83     {
84         struct hostent *hp;
85
86         /* if we got a basename (as we do in Linux) make a FQDN of it */
87         hp = gethostbyname(tmpbuf);
88         if (hp == (struct hostent *) NULL)
89         {
90             /* exit with error message */
91             fprintf(stderr,
92                     "gethostbyname failed for %s\n", tmpbuf);
93             exit(PS_DNS);
94         }
95         return(xstrdup(hp->h_name));
96     }
97     else
98 #endif /* HAVE_GETHOSTBYNAME */
99         return(xstrdup(tmpbuf));
100 }
101
102
103 const char *showproto(int proto)
104 /* protocol index to protocol name mapping */
105 {
106     switch (proto)
107     {
108     case P_AUTO: return("auto"); break;
109 #ifdef POP2_ENABLE
110     case P_POP2: return("POP2"); break;
111 #endif /* POP2_ENABLE */
112     case P_POP3: return("POP3"); break;
113     case P_IMAP: return("IMAP"); break;
114     case P_IMAP_K4: return("IMAP-K4"); break;
115 #ifdef GSSAPI
116     case P_IMAP_GSS: return("IMAP-GSS"); break;
117 #endif /* GSSAPI */
118     case P_APOP: return("APOP"); break;
119     case P_RPOP: return("RPOP"); break;
120     case P_ETRN: return("ETRN"); break;
121     default: return("unknown?!?"); break;
122     }
123 }
124
125 char *visbuf(const char *buf)
126 /* visibilize a given string */
127 {
128     static char vbuf[BUFSIZ];
129     char *tp = vbuf;
130
131     while (*buf)
132     {
133         if (*buf == '"')
134         {
135             *tp++ = '\\'; *tp++ = '"';
136             buf++;
137         }
138         else if (isprint(*buf) || *buf == ' ')
139             *tp++ = *buf++;
140         else if (*buf == '\n')
141         {
142             *tp++ = '\\'; *tp++ = 'n';
143             buf++;
144         }
145         else if (*buf == '\r')
146         {
147             *tp++ = '\\'; *tp++ = 'r';
148             buf++;
149         }
150         else if (*buf == '\b')
151         {
152             *tp++ = '\\'; *tp++ = 'b';
153             buf++;
154         }
155         else if (*buf < ' ')
156         {
157             *tp++ = '\\'; *tp++ = '^'; *tp++ = '@' + *buf;
158             buf++;
159         }
160         else
161         {
162             (void) sprintf(tp, "\\0x%02x", *buf++);
163             tp += strlen(tp);
164         }
165     }
166     *tp++ = '\0';
167     return(vbuf);
168 }
169
170 /* env.c ends here */