chiark / gitweb /
make-secnet-sites: Fix error handling if caller is in wrong group
[secnet.git] / osdep.c
1 /*
2  * osdep.c
3  * - portability routines
4  */
5 /*
6  * This file is part of secnet.
7  * See README for full list of copyright holders.
8  *
9  * secnet is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  * 
14  * secnet is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  * 
19  * You should have received a copy of the GNU General Public License
20  * version 3 along with secnet; if not, see
21  * https://www.gnu.org/licenses/gpl.html.
22  */
23
24 #include "config.h"
25 #include "osdep.h"
26 #include "secnet.h"
27 #include "util.h"
28
29 #ifndef HAVE_FMEMOPEN
30 # ifdef HAVE_FUNOPEN
31
32 struct fmemopen_state {
33     const char *bufp;
34     size_t remain;
35 };
36
37 static int fmemopen_readfn(void *sst, char *out, int sz)
38 {
39     struct fmemopen_state *st=sst;
40     assert(sz>=0);
41     int now=MIN((size_t)sz,st->remain);
42     memcpy(out,st->bufp,now);
43     st->remain-=now;
44     return now;
45 }
46 static int fmemopen_close(void *sst) { free(sst); return 0; }
47
48 FILE *fmemopen(void *buf, size_t size, const char *mode)
49 {
50     /* this is just a fake plastic imitation */
51     assert(!strcmp(mode,"r"));
52     struct fmemopen_state *st;
53     NEW(st);
54     st->bufp=buf;
55     st->remain=size;
56     FILE *f=funopen(st,fmemopen_readfn,0,0,fmemopen_close);
57     if (!f) free(st);
58     return f;
59 }
60
61 # else /* HAVE_FUNOPEN */
62 #  error no fmemopen, no funopen, cannot proceed
63 # endif
64
65 #endif /* HAVE_FMEMOPEN */