update from main archive 961208
[glibc.git] / libio / iofopncook.c
blob718205e073f7d20fadedd94b837e3a12e4d43b7b
1 /*
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)
8 any later version.
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. */
25 #include <libioP.h>
26 #include <stdio.h>
27 #include <stdlib.h>
30 /* Prototyped for local functions. */
31 static _IO_ssize_t _IO_cookie_read __P ((register _IO_FILE* fp, void* buf,
32 _IO_ssize_t size));
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,
36 int dir));
37 static int _IO_cookie_close __P ((_IO_FILE* fp));
40 static _IO_ssize_t
41 _IO_cookie_read (fp, buf, size)
42 register _IO_FILE* fp;
43 void* buf;
44 _IO_ssize_t size;
46 struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
48 if (cfile->io_functions.read == NULL)
49 return -1;
51 return cfile->io_functions.read (cfile->cookie, buf, size);
54 static _IO_ssize_t
55 _IO_cookie_write (fp, buf, size)
56 register _IO_FILE* fp;
57 const void* buf;
58 _IO_ssize_t size;
60 struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
62 if (cfile->io_functions.write == NULL)
63 return -1;
65 return cfile->io_functions.write (cfile->cookie, buf, size);
68 static _IO_fpos_t
69 _IO_cookie_seek (fp, offset, dir)
70 _IO_FILE *fp;
71 _IO_off_t offset;
72 int dir;
74 struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
75 _IO_fpos_t pos;
77 if (cfile->io_functions.seek == NULL)
78 return _IO_pos_BAD;
80 pos = _IO_pos_0;
81 _IO_pos_adjust (pos, offset);
83 if (cfile->io_functions.seek (cfile->cookie, pos, dir))
84 return _IO_pos_BAD;
86 return pos;
89 static int
90 _IO_cookie_close (fp)
91 _IO_FILE* fp;
93 struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
95 if (cfile->io_functions.close == NULL)
96 return 0;
98 return cfile->io_functions.close (cfile->cookie);
102 static struct _IO_jump_t _IO_cookie_jumps = {
103 JUMP_INIT_DUMMY,
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)
124 _IO_FILE *
125 fopencookie (cookie, mode, io_functions)
126 void *cookie;
127 const char *mode;
128 _IO_cookie_io_functions_t io_functions;
130 int read_write;
131 struct locked_FILE
133 struct _IO_cookie_file cfile;
134 #ifdef _IO_MTSAFE_IO
135 _IO_lock_t lock;
136 #endif
137 } *new_f;
139 switch (*mode++)
141 case 'r':
142 read_write = _IO_NO_WRITES;
143 break;
144 case 'w':
145 read_write = _IO_NO_READS;
146 break;
147 case 'a':
148 read_write = _IO_NO_READS|_IO_IS_APPENDING;
149 break;
150 default:
151 return NULL;
153 if (mode[0] == '+' || (mode[0] == 'b' && mode[1] == '+'))
154 read_write &= _IO_IS_APPENDING;
156 new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE));
157 if (new_f == NULL)
158 return NULL;
159 #ifdef _IO_MTSAFE_IO
160 new_f->cfile.file._lock = &new_f->lock;
161 #endif
163 _IO_init (&new_f->cfile.file, 0);
164 _IO_JUMPS (&new_f->cfile.file) = &_IO_cookie_jumps;
165 new_f->cfile.cookie = cookie;
166 new_f->cfile.io_functions = io_functions;
168 _IO_file_init(&new_f->cfile.file);
170 new_f->cfile.file._IO_file_flags =
171 _IO_mask_flags (&new_f->cfile.file, read_write,
172 _IO_NO_READS+_IO_NO_WRITES+_IO_IS_APPENDING);
174 return &new_f->cfile.file;