(pututline_r): Since we assign RESULT from lseek now, check that it's >= 0, not...
[glibc.git] / libio / iofopncook.c
blob989f6d6fe74fb9c39c53fa56850e1471d3b71afa
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 _IO_cookie_file *cfile;
133 switch (*mode++)
135 case 'r':
136 read_write = _IO_NO_WRITES;
137 break;
138 case 'w':
139 read_write = _IO_NO_READS;
140 break;
141 case 'a':
142 read_write = _IO_NO_READS|_IO_IS_APPENDING;
143 break;
144 default:
145 return NULL;
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));
151 if (cfile == NULL)
152 return NULL;
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);
165 return &cfile->file;