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