File: osdep.c

package info (click to toggle)
secnet 0.6.8
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 1,956 kB
  • sloc: ansic: 15,234; python: 1,057; perl: 966; sh: 596; tcl: 484; java: 231; asm: 114; yacc: 89; php: 64; makefile: 48; awk: 40
file content (65 lines) | stat: -rw-r--r-- 1,693 bytes parent folder | download | duplicates (2)
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
58
59
60
61
62
63
64
65
/*
 * osdep.c
 * - portability routines
 */
/*
 * This file is part of secnet.
 * See README for full list of copyright holders.
 *
 * secnet is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * secnet is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * version 3 along with secnet; if not, see
 * https://www.gnu.org/licenses/gpl.html.
 */

#include "config.h"
#include "osdep.h"
#include "secnet.h"
#include "util.h"

#ifndef HAVE_FMEMOPEN
# ifdef HAVE_FUNOPEN

struct fmemopen_state {
    const char *bufp;
    size_t remain;
};

static int fmemopen_readfn(void *sst, char *out, int sz)
{
    struct fmemopen_state *st=sst;
    assert(sz>=0);
    int now=MIN((size_t)sz,st->remain);
    memcpy(out,st->bufp,now);
    st->remain-=now;
    return now;
}
static int fmemopen_close(void *sst) { free(sst); return 0; }

FILE *fmemopen(void *buf, size_t size, const char *mode)
{
    /* this is just a fake plastic imitation */
    assert(!strcmp(mode,"r"));
    struct fmemopen_state *st;
    NEW(st);
    st->bufp=buf;
    st->remain=size;
    FILE *f=funopen(st,fmemopen_readfn,0,0,fmemopen_close);
    if (!f) free(st);
    return f;
}

# else /* HAVE_FUNOPEN */
#  error no fmemopen, no funopen, cannot proceed
# endif

#endif /* HAVE_FMEMOPEN */