s/qmail 4.2.29a
Next generation secure email transport
Loading...
Searching...
No Matches
control.c
Go to the documentation of this file.
1#include <unistd.h>
2#include "open.h"
3#include "getln.h"
4#include "stralloc.h"
5#include "buffer.h"
6#include "logmsg.h"
7#include "control.h"
8#include "alloc.h"
9#include "scan.h"
10#include "error.h"
11
12static char inbuf[2048];
13static stralloc line = {0};
14static stralloc me = {0};
15static int meok = 0;
16
20static void striptrailingwhitespace(stralloc *sa)
21{
22 while (sa->len > 0)
23 switch (sa->s[sa->len - 1]) {
24 case '\n': case ' ': case '\t':
25 --sa->len;
26 break;
27 default:
28 return;
29 }
30}
31
32int control_init(void)
33{
34 int r;
35
36 r = control_readline(&me,"control/me");
37 if (r == 1) meok = 1;
38 return r;
39}
40
41int control_rldef(stralloc *sa,char *fn,int flagme,char *def)
42{
43 int r;
44
46 if (r) return r;
47 if (flagme) if (meok) return stralloc_copy(sa,&me) ? 1 : -1;
48 if (def) return stralloc_copys(sa,def) ? 1 : -1;
49 return r;
50}
51
52int control_readline(stralloc *sa,char *fn)
53{
54 buffer b;
55 int fd;
56 int match;
57
58 fd = open_read(fn);
59 if (fd == -1) { if (errno == ENOENT) return 0; return -1; }
60
61 buffer_init(&b,read,fd,inbuf,sizeof(inbuf));
62
63 if (getln(&b,sa,&match,'\n') == -1) { close(fd); return -1; }
64
65 striptrailingwhitespace(sa);
66
67 close(fd);
68 return 1;
69}
70
71int control_readint(int *i,char *fn)
72{
73 unsigned long u;
74
75 switch (control_readline(&line,fn)) {
76 case 0: return 0;
77 case -1: return -1;
78 }
79 if (!stralloc_0(&line)) return -1;
80 if (!scan_ulong(line.s,&u)) return 0;
81 *i = u;
82
83 return 1;
84}
85
86int control_readfile(stralloc *sa,char *fn,int flagme)
87{
88 buffer b;
89 int fd;
90 int match;
91
92 if (!stralloc_copys(sa,"")) return -1;
93
94 fd = open_read(fn);
95 if (fd == -1) {
96 if (errno == ENOENT) {
97 if (flagme && meok) {
98 if (!stralloc_copy(sa,&me)) return -1;
99 if (!stralloc_0(sa)) return -1;
100 return 1;
101 }
102 return 0;
103 }
104 return -1;
105 }
106
107 buffer_init(&b,read,fd,inbuf,sizeof(inbuf));
108
109 for (;;) {
110 if (getln(&b,&line,&match,'\n') == -1) break;
111 if (!match && !line.len) { close(fd); return 1; }
112 striptrailingwhitespace(&line);
113 if (!stralloc_0(&line)) break;
114 if (line.s[0])
115 if (line.s[0] != '#')
116 if (!stralloc_cat(sa,&line)) break;
117 if (!match) { close(fd); return 1; }
118 }
119
120 close(fd);
121 return -1;
122}
char inbuf[256]
Definition: auto-gid.c:9
buffer b
Definition: auto-gid.c:10
int control_readint(int *i, char *fn)
Definition: control.c:71
int control_readline(stralloc *sa, char *fn)
Definition: control.c:52
int control_rldef(stralloc *sa, char *fn, int flagme, char *def)
Definition: control.c:41
int control_readfile(stralloc *sa, char *fn, int flagme)
Definition: control.c:86
int control_init(void)
Definition: control.c:32
int stralloc_copys(stralloc *, char const *)
stralloc sa
Definition: dnscname.c:11
int match
Definition: matchup.c:195
int fd
stralloc fn
Definition: qmail-qmaint.c:483