2 Copyright (c) 2000, 2001, 2004, 2006, 2007 MySQL AB, 2009 Sun Microsystems, Inc.
3 Use is subject to license terms.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; version 2 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 /* USE_MY_STREAM isn't set because we can't thrust my_fclose! */
21 #include "mysys_priv.h"
22 #include "mysys_err.h"
29 #define ftell(A) ftello(A)
30 #define fseek(A,B,C) fseeko((A),(B),(C))
34 Read a chunk of bytes from a FILE
38 stream File descriptor
39 Buffer Buffer to read to
40 Count Number of bytes to read
41 MyFlags Flags on what to do on error
45 # Number of bytes read
48 size_t my_fread(FILE *stream
, uchar
*Buffer
, size_t Count
, myf MyFlags
)
51 DBUG_ENTER("my_fread");
52 DBUG_PRINT("my",("stream: 0x%lx Buffer: 0x%lx Count: %u MyFlags: %d",
53 (long) stream
, (long) Buffer
, (uint
) Count
, MyFlags
));
55 if ((readbytes
= fread(Buffer
, sizeof(char), Count
, stream
)) != Count
)
57 DBUG_PRINT("error",("Read only %d bytes", (int) readbytes
));
58 if (MyFlags
& (MY_WME
| MY_FAE
| MY_FNABP
))
61 my_error(EE_READ
, MYF(ME_BELL
+ME_WAITTANG
),
62 my_filename(fileno(stream
)),errno
);
64 if (MyFlags
& (MY_NABP
| MY_FNABP
))
65 my_error(EE_EOFERR
, MYF(ME_BELL
+ME_WAITTANG
),
66 my_filename(fileno(stream
)),errno
);
68 my_errno
=errno
? errno
: -1;
69 if (ferror(stream
) || MyFlags
& (MY_NABP
| MY_FNABP
))
70 DBUG_RETURN((size_t) -1); /* Return with error */
72 if (MyFlags
& (MY_NABP
| MY_FNABP
))
73 DBUG_RETURN(0); /* Read ok */
74 DBUG_RETURN(readbytes
);
79 Write a chunk of bytes to a stream
82 stream File descriptor
83 Buffer Buffer to write from
84 Count Number of bytes to write
85 MyFlags Flags on what to do on error
89 # Number of bytes written
92 size_t my_fwrite(FILE *stream
, const uchar
*Buffer
, size_t Count
, myf MyFlags
)
94 size_t writtenbytes
=0;
96 #if !defined(NO_BACKGROUND) && defined(USE_MY_STREAM)
99 DBUG_ENTER("my_fwrite");
100 DBUG_PRINT("my",("stream: 0x%lx Buffer: 0x%lx Count: %u MyFlags: %d",
101 (long) stream
, (long) Buffer
, (uint
) Count
, MyFlags
));
103 #if !defined(NO_BACKGROUND) && defined(USE_MY_STREAM)
106 seekptr
= ftell(stream
);
110 if ((written
= (size_t) fwrite((char*) Buffer
,sizeof(char),
111 Count
, stream
)) != Count
)
113 DBUG_PRINT("error",("Write only %d bytes", (int) writtenbytes
));
115 if (written
!= (size_t) -1)
119 writtenbytes
+=written
;
125 VOID(my_fseek(stream
,seekptr
,MY_SEEK_SET
,MYF(0)));
129 #if !defined(NO_BACKGROUND) && defined(USE_MY_STREAM)
131 if (my_thread_var
->abort
)
132 MyFlags
&= ~ MY_WAIT_IF_FULL
; /* End if aborted by user */
134 if ((errno
== ENOSPC
|| errno
== EDQUOT
) &&
135 (MyFlags
& MY_WAIT_IF_FULL
))
137 wait_for_free_space("[stream]", errors
);
139 VOID(my_fseek(stream
,seekptr
,MY_SEEK_SET
,MYF(0)));
143 if (ferror(stream
) || (MyFlags
& (MY_NABP
| MY_FNABP
)))
145 if (MyFlags
& (MY_WME
| MY_FAE
| MY_FNABP
))
147 my_error(EE_WRITE
, MYF(ME_BELL
+ME_WAITTANG
),
148 my_filename(fileno(stream
)),errno
);
150 writtenbytes
= (size_t) -1; /* Return that we got error */
154 if (MyFlags
& (MY_NABP
| MY_FNABP
))
155 writtenbytes
= 0; /* Everything OK */
157 writtenbytes
+= written
;
160 DBUG_RETURN(writtenbytes
);
164 /* Seek to position in file */
166 my_off_t
my_fseek(FILE *stream
, my_off_t pos
, int whence
,
167 myf MyFlags
__attribute__((unused
)))
169 DBUG_ENTER("my_fseek");
170 DBUG_PRINT("my",("stream: 0x%lx pos: %lu whence: %d MyFlags: %d",
171 (long) stream
, (long) pos
, whence
, MyFlags
));
172 DBUG_RETURN(fseek(stream
, (off_t
) pos
, whence
) ?
173 MY_FILEPOS_ERROR
: (my_off_t
) ftell(stream
));
177 /* Tell current position of file */
179 my_off_t
my_ftell(FILE *stream
, myf MyFlags
__attribute__((unused
)))
182 DBUG_ENTER("my_ftell");
183 DBUG_PRINT("my",("stream: 0x%lx MyFlags: %d", (long) stream
, MyFlags
));
185 DBUG_PRINT("exit",("ftell: %lu",(ulong
) pos
));
186 DBUG_RETURN((my_off_t
) pos
);