Updated from /src/gettext-0.10.4/intl/hash-string.h
[glibc/history.git] / libio / iofopncook.c
blob0b57ecd344d89c0e57af58e004d910cb0a8ce97c
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 <stdlib.h>
28 struct _IO_cookie_file {
29 struct _IO_FILE file;
30 const void *vtable;
31 void *cookie;
32 _IO_cookie_io_functions_t io_functions;
36 static _IO_ssize_t
37 _IO_cookie_read (fp, buf, size)
38 register _IO_FILE* fp;
39 void* buf;
40 _IO_ssize_t size;
42 struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
44 if (cfile->io_functions.read == NULL)
45 return -1;
47 return cfile->io_functions.read (cfile->cookie, buf, size);
50 static _IO_ssize_t
51 _IO_cookie_write (fp, buf, size)
52 register _IO_FILE* fp;
53 const void* buf;
54 _IO_ssize_t size;
56 struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
58 if (cfile->io_functions.write == NULL)
59 return -1;
61 return cfile->io_functions.write (cfile->cookie, buf, size);
64 static _IO_fpos_t
65 _IO_cookie_seek (fp, offset, dir)
66 _IO_FILE *fp;
67 _IO_off_t offset;
68 int dir;
70 struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
71 _IO_fpos_t pos;
73 if (cfile->io_functions.seek == NULL)
74 return _IO_pos_BAD;
76 pos = _IO_pos_0;
77 _IO_pos_adjust (pos, offset);
79 if (cfile->io_functions.seek (cfile->cookie, pos, dir))
80 return _IO_pos_BAD;
82 return pos;
85 static int
86 _IO_cookie_close (fp)
87 _IO_FILE* fp;
89 struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
91 if (cfile->io_functions.close == NULL)
92 return 0;
94 return cfile->io_functions.close (cfile->cookie);
98 static struct _IO_jump_t _IO_cookie_jumps = {
99 JUMP_INIT_DUMMY,
100 JUMP_INIT(finish, _IO_file_finish),
101 JUMP_INIT(overflow, _IO_file_overflow),
102 JUMP_INIT(underflow, _IO_file_underflow),
103 JUMP_INIT(uflow, _IO_default_uflow),
104 JUMP_INIT(pbackfail, _IO_default_pbackfail),
105 JUMP_INIT(xsputn, _IO_file_xsputn),
106 JUMP_INIT(xsgetn, _IO_default_xsgetn),
107 JUMP_INIT(seekoff, _IO_file_seekoff),
108 JUMP_INIT(seekpos, _IO_default_seekpos),
109 JUMP_INIT(setbuf, _IO_file_setbuf),
110 JUMP_INIT(sync, _IO_file_sync),
111 JUMP_INIT(doallocate, _IO_file_doallocate),
112 JUMP_INIT(read, _IO_cookie_read),
113 JUMP_INIT(write, _IO_cookie_write),
114 JUMP_INIT(seek, _IO_cookie_seek),
115 JUMP_INIT(close, _IO_cookie_close),
116 JUMP_INIT(stat, _IO_default_stat)
120 _IO_FILE *
121 fopencookie (cookie, mode, io_functions)
122 void *cookie;
123 const char *mode;
124 _IO_cookie_io_functions_t io_functions;
126 int read_write;
127 struct _IO_cookie_file *cfile;
129 switch (*mode++)
131 case 'r':
132 read_write = _IO_NO_WRITES;
133 break;
134 case 'w':
135 read_write = _IO_NO_READS;
136 break;
137 case 'a':
138 read_write = _IO_NO_READS|_IO_IS_APPENDING;
139 break;
140 default:
141 return NULL;
143 if (mode[0] == '+' || (mode[0] == 'b' && mode[1] == '+'))
144 read_write &= _IO_IS_APPENDING;
146 cfile = (struct _IO_cookie_file *) malloc (sizeof (struct _IO_cookie_file));
147 if (cfile == NULL)
148 return NULL;
150 _IO_init (&cfile->file, 0);
151 _IO_JUMPS (&cfile->file) = &_IO_cookie_jumps;
152 cfile->cookie = cookie;
153 cfile->io_functions = io_functions;
155 _IO_file_init(&cfile->file);
157 cfile->file._IO_file_flags =
158 _IO_mask_flags (&cfile->file, read_write,
159 _IO_NO_READS+_IO_NO_WRITES+_IO_IS_APPENDING);
161 return &cfile->file;