io.c
3.04 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <io0.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
int
stream_create (stream_t *pstream, int flags, void *owner)
{
stream_t stream;
if (pstream == NULL || owner == NULL)
return EINVAL;
stream = calloc (1, sizeof (*stream));
if (stream == NULL)
return ENOMEM;
stream->owner = owner;
stream->flags = flags;
*pstream = stream;
return 0;
}
void
stream_destroy (stream_t *pstream, void *owner)
{
if (pstream && *pstream)
{
stream_t stream = *pstream;
if (stream->_destroy)
stream->_destroy (owner);
if ((stream->flags & MU_STREAM_NO_CHECK) || stream->owner == owner)
free (stream);
*pstream = NULL;
}
}
int
stream_set_destroy (stream_t stream, void (*_destroy) (void *),
void *owner)
{
if (stream == NULL)
return EINVAL;
if (stream->owner != owner)
return EACCES;
stream->_destroy = _destroy;
return 0;
}
int
stream_set_fd (stream_t stream, int (*_get_fd) (stream_t, int *), void *owner)
{
if (stream == NULL)
return EINVAL;
if (owner == stream->owner)
{
stream->_get_fd = _get_fd;
return 0;
}
return EACCES;
}
int
stream_set_read (stream_t stream, int (*_read)
(stream_t, char *, size_t, off_t, size_t *),
void *owner)
{
if (stream == NULL)
return EINVAL;
if (owner == stream->owner)
{
stream->_read = _read;
return 0;
}
return EACCES;
}
int
stream_set_write (stream_t stream, int (*_write)
__P ((stream_t, const char *, size_t, off_t, size_t *)),
void *owner)
{
if (stream == NULL)
return EINVAL;
if (stream->owner == owner)
{
stream->_write = _write;
return 0;
}
return EACCES;
}
int
stream_read (stream_t is, char *buf, size_t count,
off_t offset, size_t *pnread)
{
if (is == NULL || is->_read == NULL)
return EINVAL;
return is->_read (is, buf, count, offset, pnread);
}
int
stream_write (stream_t os, const char *buf, size_t count,
off_t offset, size_t *pnwrite)
{
if (os == NULL || os->_write == NULL)
return EINVAL;
return os->_write (os, buf, count, offset, pnwrite);
}
int
stream_get_fd (stream_t stream, int *pfd)
{
if (stream == NULL || stream->_get_fd == NULL)
return EINVAL;
return stream->_get_fd (stream, pfd);
}