1 /* Copyright (C) 2000 MySQL AB
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16 /* USE_MY_STREAM isn't set because we can't thrust my_fclose! */
18 #include "mysys_priv.h"
19 #include "mysys_err.h"
26 #define ftell(A) ftello(A)
27 #define fseek(A,B,C) fseeko((A),(B),(C))
31 Read a chunk of bytes from a FILE
35 stream File descriptor
36 Buffer Buffer to read to
37 Count Number of bytes to read
38 MyFlags Flags on what to do on error
42 # Number of bytes read
45 size_t my_fread(FILE *stream
, uchar
*Buffer
, size_t Count
, myf MyFlags
)
48 DBUG_ENTER("my_fread");
49 DBUG_PRINT("my",("stream: 0x%lx Buffer: 0x%lx Count: %u MyFlags: %d",
50 (long) stream
, (long) Buffer
, (uint
) Count
, MyFlags
));
52 if ((readbytes
= fread(Buffer
, sizeof(char), Count
, stream
)) != Count
)
54 DBUG_PRINT("error",("Read only %d bytes", (int) readbytes
));
55 if (MyFlags
& (MY_WME
| MY_FAE
| MY_FNABP
))
58 my_error(EE_READ
, MYF(ME_BELL
+ME_WAITTANG
),
59 my_filename(fileno(stream
)),errno
);
61 if (MyFlags
& (MY_NABP
| MY_FNABP
))
62 my_error(EE_EOFERR
, MYF(ME_BELL
+ME_WAITTANG
),
63 my_filename(fileno(stream
)),errno
);
65 my_errno
=errno
? errno
: -1;
66 if (ferror(stream
) || MyFlags
& (MY_NABP
| MY_FNABP
))
67 DBUG_RETURN((size_t) -1); /* Return with error */
69 if (MyFlags
& (MY_NABP
| MY_FNABP
))
70 DBUG_RETURN(0); /* Read ok */
71 DBUG_RETURN(readbytes
);
76 Write a chunk of bytes to a stream
79 stream File descriptor
80 Buffer Buffer to write from
81 Count Number of bytes to write
82 MyFlags Flags on what to do on error
86 # Number of bytes written
89 size_t my_fwrite(FILE *stream
, const uchar
*Buffer
, size_t Count
, myf MyFlags
)
91 size_t writtenbytes
=0;
93 #if !defined(NO_BACKGROUND) && defined(USE_MY_STREAM)
96 DBUG_ENTER("my_fwrite");
97 DBUG_PRINT("my",("stream: 0x%lx Buffer: 0x%lx Count: %u MyFlags: %d",
98 (long) stream
, (long) Buffer
, (uint
) Count
, MyFlags
));
100 #if !defined(NO_BACKGROUND) && defined(USE_MY_STREAM)
103 seekptr
= ftell(stream
);
107 if ((written
= (size_t) fwrite((char*) Buffer
,sizeof(char),
108 Count
, stream
)) != Count
)
110 DBUG_PRINT("error",("Write only %d bytes", (int) writtenbytes
));
112 if (written
!= (size_t) -1)
116 writtenbytes
+=written
;
122 VOID(my_fseek(stream
,seekptr
,MY_SEEK_SET
,MYF(0)));
126 #if !defined(NO_BACKGROUND) && defined(USE_MY_STREAM)
128 if (my_thread_var
->abort
)
129 MyFlags
&= ~ MY_WAIT_IF_FULL
; /* End if aborted by user */
131 if ((errno
== ENOSPC
|| errno
== EDQUOT
) &&
132 (MyFlags
& MY_WAIT_IF_FULL
))
134 wait_for_free_space("[stream]", errors
);
136 VOID(my_fseek(stream
,seekptr
,MY_SEEK_SET
,MYF(0)));
140 if (ferror(stream
) || (MyFlags
& (MY_NABP
| MY_FNABP
)))
142 if (MyFlags
& (MY_WME
| MY_FAE
| MY_FNABP
))
144 my_error(EE_WRITE
, MYF(ME_BELL
+ME_WAITTANG
),
145 my_filename(fileno(stream
)),errno
);
147 writtenbytes
= (size_t) -1; /* Return that we got error */
151 if (MyFlags
& (MY_NABP
| MY_FNABP
))
152 writtenbytes
= 0; /* Everything OK */
154 writtenbytes
+= written
;
157 DBUG_RETURN(writtenbytes
);
161 /* Seek to position in file */
163 my_off_t
my_fseek(FILE *stream
, my_off_t pos
, int whence
,
164 myf MyFlags
__attribute__((unused
)))
166 DBUG_ENTER("my_fseek");
167 DBUG_PRINT("my",("stream: 0x%lx pos: %lu whence: %d MyFlags: %d",
168 (long) stream
, (long) pos
, whence
, MyFlags
));
169 DBUG_RETURN(fseek(stream
, (off_t
) pos
, whence
) ?
170 MY_FILEPOS_ERROR
: (my_off_t
) ftell(stream
));
174 /* Tell current position of file */
176 my_off_t
my_ftell(FILE *stream
, myf MyFlags
__attribute__((unused
)))
179 DBUG_ENTER("my_ftell");
180 DBUG_PRINT("my",("stream: 0x%lx MyFlags: %d", (long) stream
, MyFlags
));
182 DBUG_PRINT("exit",("ftell: %lu",(ulong
) pos
));
183 DBUG_RETURN((my_off_t
) pos
);