Update.
[glibc.git] / libio / iofopncook.c
blob4f28ec20a8a2f72a5b1b8204bff78b3a62ca066b
1 /* Copyright (C) 1993, 1995, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU IO Library.
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.
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.
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.
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. */
26 #include <libioP.h>
27 #include <stdio.h>
28 #include <stdlib.h>
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));
36 static _IO_fpos64_t _IO_cookie_seek __P ((_IO_FILE *fp, _IO_off64_t offset,
37 int dir));
38 static int _IO_cookie_close __P ((_IO_FILE* fp));
41 static _IO_ssize_t
42 _IO_cookie_read (fp, buf, size)
43 _IO_FILE *fp;
44 void *buf;
45 _IO_ssize_t size;
47 struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
49 if (cfile->io_functions.read == NULL)
50 return -1;
52 return cfile->io_functions.read (cfile->cookie, buf, size);
55 static _IO_ssize_t
56 _IO_cookie_write (fp, buf, size)
57 _IO_FILE *fp;
58 const void *buf;
59 _IO_ssize_t size;
61 struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
63 if (cfile->io_functions.write == NULL)
64 return -1;
66 return cfile->io_functions.write (cfile->cookie, buf, size);
69 static _IO_fpos64_t
70 _IO_cookie_seek (fp, offset, dir)
71 _IO_FILE *fp;
72 _IO_off64_t offset;
73 int dir;
75 struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
76 _IO_fpos64_t pos;
78 if (cfile->io_functions.seek == NULL)
79 return _IO_pos_BAD;
81 pos = _IO_pos_0;
82 _IO_pos_adjust (pos, offset);
84 if (cfile->io_functions.seek (cfile->cookie, pos, dir))
85 return _IO_pos_BAD;
87 return pos;
90 static int
91 _IO_cookie_close (fp)
92 _IO_FILE *fp;
94 struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
96 if (cfile->io_functions.close == NULL)
97 return 0;
99 return cfile->io_functions.close (cfile->cookie);
103 static struct _IO_jump_t _IO_cookie_jumps = {
104 JUMP_INIT_DUMMY,
105 JUMP_INIT(finish, _IO_file_finish),
106 JUMP_INIT(overflow, _IO_file_overflow),
107 JUMP_INIT(underflow, _IO_file_underflow),
108 JUMP_INIT(uflow, _IO_default_uflow),
109 JUMP_INIT(pbackfail, _IO_default_pbackfail),
110 JUMP_INIT(xsputn, _IO_file_xsputn),
111 JUMP_INIT(xsgetn, _IO_default_xsgetn),
112 JUMP_INIT(seekoff, _IO_file_seekoff),
113 JUMP_INIT(seekpos, _IO_default_seekpos),
114 JUMP_INIT(setbuf, _IO_file_setbuf),
115 JUMP_INIT(sync, _IO_file_sync),
116 JUMP_INIT(doallocate, _IO_file_doallocate),
117 JUMP_INIT(read, _IO_cookie_read),
118 JUMP_INIT(write, _IO_cookie_write),
119 JUMP_INIT(seek, _IO_cookie_seek),
120 JUMP_INIT(close, _IO_cookie_close),
121 JUMP_INIT(stat, _IO_default_stat),
122 JUMP_INIT(showmanyc, _IO_default_showmanyc),
123 JUMP_INIT(imbue, _IO_default_imbue),
127 _IO_FILE *
128 fopencookie (cookie, mode, io_functions)
129 void *cookie;
130 const char *mode;
131 _IO_cookie_io_functions_t io_functions;
133 int read_write;
134 struct locked_FILE
136 struct _IO_cookie_file cfile;
137 #ifdef _IO_MTSAFE_IO
138 _IO_lock_t lock;
139 #endif
140 } *new_f;
142 switch (*mode++)
144 case 'r':
145 read_write = _IO_NO_WRITES;
146 break;
147 case 'w':
148 read_write = _IO_NO_READS;
149 break;
150 case 'a':
151 read_write = _IO_NO_READS|_IO_IS_APPENDING;
152 break;
153 default:
154 return NULL;
156 if (mode[0] == '+' || (mode[0] == 'b' && mode[1] == '+'))
157 read_write &= _IO_IS_APPENDING;
159 new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE));
160 if (new_f == NULL)
161 return NULL;
162 #ifdef _IO_MTSAFE_IO
163 new_f->cfile.file._lock = &new_f->lock;
164 #endif
166 _IO_init (&new_f->cfile.file, 0);
167 _IO_JUMPS (&new_f->cfile.file) = &_IO_cookie_jumps;
168 new_f->cfile.cookie = cookie;
169 new_f->cfile.io_functions = io_functions;
171 _IO_file_init(&new_f->cfile.file);
173 new_f->cfile.file._IO_file_flags =
174 _IO_mask_flags (&new_f->cfile.file, read_write,
175 _IO_NO_READS+_IO_NO_WRITES+_IO_IS_APPENDING);
177 return &new_f->cfile.file;