forked from bruceg/ezmlm-idx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
altpath.c
57 lines (52 loc) · 1.28 KB
/
altpath.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "stralloc.h"
#include "config.h"
#include "die.h"
#include "env.h"
#include "error.h"
#include "open.h"
#include "slurp.h"
#include "altpath.h"
#include "idx.h"
#include "auto_etc.h"
static stralloc path = {0};
const char *altpath(stralloc *s,const char *fn)
{
if (!stralloc_copy(s,&ezmlmrc)) die_nomem();
if (!stralloc_append(s,'/')) die_nomem();
if (!stralloc_cats(s,fn)) die_nomem();
if (!stralloc_0(s)) die_nomem();
return s->s;
}
const char *altdefaultpath(stralloc *s,const char *fn)
{
if (!stralloc_copys(s,auto_etc())) die_nomem();
if (!stralloc_cats(s,TXT_DEFAULT)) die_nomem();
if (!stralloc_append(s,'/')) die_nomem();
if (!stralloc_cats(s,fn)) die_nomem();
if (!stralloc_0(s)) die_nomem();
return s->s;
}
int alt_open_read(const char *fn)
{
int fd;
if ((fd = open_read(fn)) == -1
&& errno == error_noent) {
if (ezmlmrc.len > 0)
fd = open_read(altpath(&path,fn));
if (fd == -1
&& errno == error_noent)
fd = open_read(altdefaultpath(&path,fn));
}
return fd;
}
int alt_slurp(const char *fn,struct stralloc *sa,int bufsize)
{
int r;
if ((r = slurp(fn,sa,bufsize)) == 0) {
if (ezmlmrc.len > 0)
r = slurp(altpath(&path,fn),sa,bufsize);
if (r == 0)
r = slurp(altdefaultpath(&path,fn),sa,bufsize);
}
return r;
}