]>
Commit | Line | Data |
---|---|---|
b4e54243 | 1 | /* Copyright (C) 1993,95,97,99,2000 Free Software Foundation, Inc. |
40a55d20 UD |
2 | This file is part of the GNU IO Library. |
3 | ||
4 | This library is free software; you can redistribute it and/or | |
5 | modify it under the terms of the GNU General Public License as | |
6 | published by the Free Software Foundation; either version 2, or (at | |
7 | your option) any later version. | |
8 | ||
9 | This library is distributed in the hope that it will be useful, but | |
10 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public License | |
15 | along with this library; see the file COPYING. If not, write to | |
16 | the Free Software Foundation, 59 Temple Place - Suite 330, Boston, | |
17 | MA 02111-1307, USA. | |
18 | ||
19 | As a special exception, if you link this library with files | |
20 | compiled with a GNU compiler to produce an executable, this does | |
21 | not cause the resulting executable to be covered by the GNU General | |
22 | Public License. This exception does not however invalidate any | |
23 | other reasons why the executable file might be covered by the GNU | |
24 | General Public License. */ | |
96aa2d94 RM |
25 | |
26 | #include <libioP.h> | |
fa0bc87c | 27 | #include <stdio.h> |
96aa2d94 RM |
28 | #include <stdlib.h> |
29 | ||
fa0bc87c RM |
30 | |
31 | /* Prototyped for local functions. */ | |
32 | static _IO_ssize_t _IO_cookie_read __P ((register _IO_FILE* fp, void* buf, | |
33 | _IO_ssize_t size)); | |
34 | static _IO_ssize_t _IO_cookie_write __P ((register _IO_FILE* fp, | |
35 | const void* buf, _IO_ssize_t size)); | |
d64b6ad0 UD |
36 | static _IO_off64_t _IO_cookie_seek __P ((_IO_FILE *fp, _IO_off64_t offset, |
37 | int dir)); | |
fa0bc87c | 38 | static int _IO_cookie_close __P ((_IO_FILE* fp)); |
96aa2d94 RM |
39 | |
40 | ||
41 | static _IO_ssize_t | |
42 | _IO_cookie_read (fp, buf, size) | |
40a55d20 UD |
43 | _IO_FILE *fp; |
44 | void *buf; | |
96aa2d94 RM |
45 | _IO_ssize_t size; |
46 | { | |
47 | struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp; | |
48 | ||
542f5e01 | 49 | if (cfile->__io_functions.read == NULL) |
96aa2d94 RM |
50 | return -1; |
51 | ||
542f5e01 | 52 | return cfile->__io_functions.read (cfile->__cookie, buf, size); |
96aa2d94 RM |
53 | } |
54 | ||
55 | static _IO_ssize_t | |
56 | _IO_cookie_write (fp, buf, size) | |
40a55d20 UD |
57 | _IO_FILE *fp; |
58 | const void *buf; | |
96aa2d94 RM |
59 | _IO_ssize_t size; |
60 | { | |
61 | struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp; | |
62 | ||
542f5e01 | 63 | if (cfile->__io_functions.write == NULL) |
96aa2d94 RM |
64 | return -1; |
65 | ||
542f5e01 | 66 | return cfile->__io_functions.write (cfile->__cookie, buf, size); |
96aa2d94 RM |
67 | } |
68 | ||
d64b6ad0 | 69 | static _IO_off64_t |
96aa2d94 RM |
70 | _IO_cookie_seek (fp, offset, dir) |
71 | _IO_FILE *fp; | |
dfd2257a | 72 | _IO_off64_t offset; |
96aa2d94 RM |
73 | int dir; |
74 | { | |
75 | struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp; | |
96aa2d94 | 76 | |
b4e54243 | 77 | return ((cfile->__io_functions.seek == NULL |
9658516a UD |
78 | || (cfile->__io_functions.seek (cfile->__cookie, &offset, dir) |
79 | == (_IO_off64_t) -1)) | |
b4e54243 | 80 | ? _IO_pos_BAD : offset); |
96aa2d94 RM |
81 | } |
82 | ||
83 | static int | |
84 | _IO_cookie_close (fp) | |
40a55d20 | 85 | _IO_FILE *fp; |
96aa2d94 RM |
86 | { |
87 | struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp; | |
88 | ||
542f5e01 | 89 | if (cfile->__io_functions.close == NULL) |
96aa2d94 RM |
90 | return 0; |
91 | ||
542f5e01 | 92 | return cfile->__io_functions.close (cfile->__cookie); |
96aa2d94 RM |
93 | } |
94 | ||
95 | ||
96 | static struct _IO_jump_t _IO_cookie_jumps = { | |
97 | JUMP_INIT_DUMMY, | |
98 | JUMP_INIT(finish, _IO_file_finish), | |
99 | JUMP_INIT(overflow, _IO_file_overflow), | |
100 | JUMP_INIT(underflow, _IO_file_underflow), | |
101 | JUMP_INIT(uflow, _IO_default_uflow), | |
102 | JUMP_INIT(pbackfail, _IO_default_pbackfail), | |
103 | JUMP_INIT(xsputn, _IO_file_xsputn), | |
104 | JUMP_INIT(xsgetn, _IO_default_xsgetn), | |
105 | JUMP_INIT(seekoff, _IO_file_seekoff), | |
106 | JUMP_INIT(seekpos, _IO_default_seekpos), | |
107 | JUMP_INIT(setbuf, _IO_file_setbuf), | |
108 | JUMP_INIT(sync, _IO_file_sync), | |
109 | JUMP_INIT(doallocate, _IO_file_doallocate), | |
110 | JUMP_INIT(read, _IO_cookie_read), | |
111 | JUMP_INIT(write, _IO_cookie_write), | |
112 | JUMP_INIT(seek, _IO_cookie_seek), | |
113 | JUMP_INIT(close, _IO_cookie_close), | |
dfd2257a UD |
114 | JUMP_INIT(stat, _IO_default_stat), |
115 | JUMP_INIT(showmanyc, _IO_default_showmanyc), | |
116 | JUMP_INIT(imbue, _IO_default_imbue), | |
96aa2d94 RM |
117 | }; |
118 | ||
119 | ||
b4e54243 RM |
120 | void |
121 | _IO_cookie_init (struct _IO_cookie_file *cfile, int read_write, | |
122 | void *cookie, _IO_cookie_io_functions_t io_functions) | |
123 | { | |
124 | _IO_init (&cfile->__file, 0); | |
125 | _IO_JUMPS (&cfile->__file) = &_IO_cookie_jumps; | |
126 | ||
127 | cfile->__cookie = cookie; | |
128 | cfile->__io_functions = io_functions; | |
129 | ||
130 | _IO_file_init(&cfile->__file); | |
131 | ||
132 | cfile->__file._IO_file_flags = | |
133 | _IO_mask_flags (&cfile->__file, read_write, | |
134 | _IO_NO_READS+_IO_NO_WRITES+_IO_IS_APPENDING); | |
135 | ||
136 | /* We use a negative number different from -1 for _fileno to mark that | |
137 | this special stream is not associated with a real file, but still has | |
138 | to be treated as such. */ | |
139 | cfile->__file._fileno = -2; | |
140 | } | |
141 | ||
142 | ||
96aa2d94 RM |
143 | _IO_FILE * |
144 | fopencookie (cookie, mode, io_functions) | |
145 | void *cookie; | |
146 | const char *mode; | |
147 | _IO_cookie_io_functions_t io_functions; | |
148 | { | |
149 | int read_write; | |
edf5b2d7 UD |
150 | struct locked_FILE |
151 | { | |
152 | struct _IO_cookie_file cfile; | |
499e7464 | 153 | #ifdef _IO_MTSAFE_IO |
edf5b2d7 | 154 | _IO_lock_t lock; |
499e7464 | 155 | #endif |
edf5b2d7 | 156 | } *new_f; |
96aa2d94 RM |
157 | |
158 | switch (*mode++) | |
159 | { | |
160 | case 'r': | |
161 | read_write = _IO_NO_WRITES; | |
162 | break; | |
163 | case 'w': | |
164 | read_write = _IO_NO_READS; | |
165 | break; | |
166 | case 'a': | |
167 | read_write = _IO_NO_READS|_IO_IS_APPENDING; | |
168 | break; | |
169 | default: | |
170 | return NULL; | |
171 | } | |
172 | if (mode[0] == '+' || (mode[0] == 'b' && mode[1] == '+')) | |
173 | read_write &= _IO_IS_APPENDING; | |
174 | ||
edf5b2d7 UD |
175 | new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE)); |
176 | if (new_f == NULL) | |
96aa2d94 | 177 | return NULL; |
499e7464 | 178 | #ifdef _IO_MTSAFE_IO |
542f5e01 | 179 | new_f->cfile.__file._lock = &new_f->lock; |
499e7464 | 180 | #endif |
96aa2d94 | 181 | |
b4e54243 | 182 | _IO_cookie_init (&new_f->cfile, read_write, cookie, io_functions); |
110215a9 | 183 | |
542f5e01 | 184 | return &new_f->cfile.__file; |
96aa2d94 | 185 | } |