2 Copyright (C) 1993, 1995 Free Software Foundation
4 This file is part of the GNU IO Library. This library is free
5 software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option)
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this library; see the file COPYING. If not, write to the Free
17 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 As a special exception, if you link this library with files
20 compiled with a GNU compiler to produce an executable, this does not cause
21 the resulting executable to be covered by the GNU General Public License.
22 This exception does not however invalidate any other reasons why
23 the executable file might be covered by the GNU General Public License. */
30 /* Prototyped for local functions. */
31 static _IO_ssize_t _IO_cookie_read
__P ((register _IO_FILE
* fp
, void* buf
,
33 static _IO_ssize_t _IO_cookie_write
__P ((register _IO_FILE
* fp
,
34 const void* buf
, _IO_ssize_t size
));
35 static _IO_fpos_t _IO_cookie_seek
__P ((_IO_FILE
*fp
, _IO_off_t offset
,
37 static int _IO_cookie_close
__P ((_IO_FILE
* fp
));
41 _IO_cookie_read (fp
, buf
, size
)
42 register _IO_FILE
* fp
;
46 struct _IO_cookie_file
*cfile
= (struct _IO_cookie_file
*) fp
;
48 if (cfile
->io_functions
.read
== NULL
)
51 return cfile
->io_functions
.read (cfile
->cookie
, buf
, size
);
55 _IO_cookie_write (fp
, buf
, size
)
56 register _IO_FILE
* fp
;
60 struct _IO_cookie_file
*cfile
= (struct _IO_cookie_file
*) fp
;
62 if (cfile
->io_functions
.write
== NULL
)
65 return cfile
->io_functions
.write (cfile
->cookie
, buf
, size
);
69 _IO_cookie_seek (fp
, offset
, dir
)
74 struct _IO_cookie_file
*cfile
= (struct _IO_cookie_file
*) fp
;
77 if (cfile
->io_functions
.seek
== NULL
)
81 _IO_pos_adjust (pos
, offset
);
83 if (cfile
->io_functions
.seek (cfile
->cookie
, pos
, dir
))
93 struct _IO_cookie_file
*cfile
= (struct _IO_cookie_file
*) fp
;
95 if (cfile
->io_functions
.close
== NULL
)
98 return cfile
->io_functions
.close (cfile
->cookie
);
102 static struct _IO_jump_t _IO_cookie_jumps
= {
104 JUMP_INIT(finish
, _IO_file_finish
),
105 JUMP_INIT(overflow
, _IO_file_overflow
),
106 JUMP_INIT(underflow
, _IO_file_underflow
),
107 JUMP_INIT(uflow
, _IO_default_uflow
),
108 JUMP_INIT(pbackfail
, _IO_default_pbackfail
),
109 JUMP_INIT(xsputn
, _IO_file_xsputn
),
110 JUMP_INIT(xsgetn
, _IO_default_xsgetn
),
111 JUMP_INIT(seekoff
, _IO_file_seekoff
),
112 JUMP_INIT(seekpos
, _IO_default_seekpos
),
113 JUMP_INIT(setbuf
, _IO_file_setbuf
),
114 JUMP_INIT(sync
, _IO_file_sync
),
115 JUMP_INIT(doallocate
, _IO_file_doallocate
),
116 JUMP_INIT(read
, _IO_cookie_read
),
117 JUMP_INIT(write
, _IO_cookie_write
),
118 JUMP_INIT(seek
, _IO_cookie_seek
),
119 JUMP_INIT(close
, _IO_cookie_close
),
120 JUMP_INIT(stat
, _IO_default_stat
)
125 fopencookie (cookie
, mode
, io_functions
)
128 _IO_cookie_io_functions_t io_functions
;
131 struct _IO_cookie_file
*cfile
;
136 read_write
= _IO_NO_WRITES
;
139 read_write
= _IO_NO_READS
;
142 read_write
= _IO_NO_READS
|_IO_IS_APPENDING
;
147 if (mode
[0] == '+' || (mode
[0] == 'b' && mode
[1] == '+'))
148 read_write
&= _IO_IS_APPENDING
;
150 cfile
= (struct _IO_cookie_file
*) malloc (sizeof (struct _IO_cookie_file
));
154 _IO_init (&cfile
->file
, 0);
155 _IO_JUMPS (&cfile
->file
) = &_IO_cookie_jumps
;
156 cfile
->cookie
= cookie
;
157 cfile
->io_functions
= io_functions
;
159 _IO_file_init(&cfile
->file
);
161 cfile
->file
._IO_file_flags
=
162 _IO_mask_flags (&cfile
->file
, read_write
,
163 _IO_NO_READS
+_IO_NO_WRITES
+_IO_IS_APPENDING
);