about summary refs log tree commit diff
path: root/src/stdio/fmemopen.c
blob: cc72a42056f7d48f2e891b4afb90182f54c2a2d6 (plain) (blame)
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
#if 0
#include "stdio_impl.h"

static ssize_t mread(FILE *f, unsigned char *buf, size_t len)
{
	size_t rem = f->memsize - f->mempos;
	if (len > rem) len = rem;
	memcpy(buf, f->membuf+f->mempos, len);
	f->mempos += len;
	return len;
}

static ssize_t mwrite(FILE *f, const unsigned char *buf, size_t len)
{
	size_t rem;
	if (f->memmode == 'a') f->mempos = f->memsize;
	rem = f->memlim - f->mempos;
	if (len > rem) len = rem;
	memcpy(f->membuf+f->mempos, buf, len);
	f->mempos += len;
	if (f->mempos >= f->memsize) {
		f->memsize = f->mempos;
	}
	return len;
}

FILE *fmemopen(void *buf, size_t size, const char *mode)
{
	FILE *f;
	int plus = !!strchr(mode, '+');
	
	if (!size || !strchr("rwa", *mode)) {
		errno = EINVAL;
		return 0;
	}

	if (!buf && size > SIZE_MAX-sizeof(FILE)-BUFSIZ-UNGET) {
		errno = ENOMEM;
		return 0;
	}

	f = calloc(sizeof(FILE) + UNGET + BUFSIZ + (buf?0:size), 1);
	if (!f) return 0;
	f->fd = -1;
	f->lbf = EOF;
	f->buf = (unsigned char *)(f+1) + UNGET;
	f->buf_size = BUFSIZ;
	if (!buf) buf = f->buf + BUFSIZ;
	
	if (!plus) f->flags = (*mode == 'r') ? F_NOWR : F_NORD;
	if (*mode == 'a') f->mempos = strchr(buf, 0)-buf;

	f->read = mread;
	f->write = mwrite;
	f->seek = mseek;
	f->flush = mflush;
	f->close = mclose;

	return f;
}
#endif