1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
3
This library is free software; you can redistribute it and/or
4
modify it under the terms of the GNU Library General Public
5
License as published by the Free Software Foundation; either
6
version 2 of the License, or (at your option) any later version.
8
This library is distributed in the hope that it will be useful,
9
but WITHOUT ANY WARRANTY; without even the implied warranty of
10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
Library General Public License for more details.
13
You should have received a copy of the GNU Library General Public
14
License along with this library; if not, write to the Free
15
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18
/* USE_MY_STREAM isn't set because we can't thrust my_fclose! */
20
#include "mysys_priv.h"
21
#include "mysys_err.h"
28
#define ftell(A) ftello(A)
29
#define fseek(A,B,C) fseeko((A),(B),(C))
32
/* Read a chunk of bytes from a file */
33
/* Returns (uint) -1 if error as my_read() */
35
uint my_fread(FILE *stream, byte *Buffer, uint Count, myf MyFlags)
37
/* Buffer must be at least count bytes */
38
/* Max number of bytes returnd */
39
/* Flags on what to do on error */
42
DBUG_ENTER("my_fread");
43
DBUG_PRINT("my",("stream: %lx Buffer: %lx Count: %u MyFlags: %d",
44
stream, Buffer, Count, MyFlags));
46
if ((readbytes = (uint) fread(Buffer,sizeof(char),(size_t) Count,stream))
49
DBUG_PRINT("error",("Read only %d bytes",readbytes));
50
if (MyFlags & (MY_WME | MY_FAE | MY_FNABP))
53
my_error(EE_READ, MYF(ME_BELL+ME_WAITTANG),
54
my_filename(fileno(stream)),errno);
56
if (MyFlags & (MY_NABP | MY_FNABP))
57
my_error(EE_EOFERR, MYF(ME_BELL+ME_WAITTANG),
58
my_filename(fileno(stream)),errno);
60
my_errno=errno ? errno : -1;
61
if (ferror(stream) || MyFlags & (MY_NABP | MY_FNABP))
62
DBUG_RETURN((uint) -1); /* Return with error */
64
if (MyFlags & (MY_NABP | MY_FNABP))
65
DBUG_RETURN(0); /* Read ok */
66
DBUG_RETURN(readbytes);
71
** Write a chunk of bytes to a stream
72
** Returns (uint) -1 if error as my_write()
73
** Does retries if interrupted
76
uint my_fwrite(FILE *stream, const byte *Buffer, uint Count, myf MyFlags)
80
#if !defined(NO_BACKGROUND) && defined(USE_MY_STREAM)
83
DBUG_ENTER("my_fwrite");
84
DBUG_PRINT("my",("stream: %lx Buffer: %lx Count: %u MyFlags: %d",
85
stream, Buffer, Count, MyFlags));
87
#if !defined(NO_BACKGROUND) && defined(USE_MY_STREAM)
90
seekptr=ftell(stream);
94
if ((writen = (uint) fwrite((char*) Buffer,sizeof(char),
95
(size_t) Count, stream)) != Count)
97
DBUG_PRINT("error",("Write only %d bytes",writenbytes));
99
if (writen != (uint) -1)
109
VOID(my_fseek(stream,seekptr,MY_SEEK_SET,MYF(0)));
113
#if !defined(NO_BACKGROUND) && defined(USE_MY_STREAM)
115
if (my_thread_var->abort)
116
MyFlags&= ~ MY_WAIT_IF_FULL; /* End if aborted by user */
118
if (errno == ENOSPC && (MyFlags & MY_WAIT_IF_FULL))
120
if (!(errors++ % MY_WAIT_GIVE_USER_A_MESSAGE))
121
my_error(EE_DISK_FULL,MYF(ME_BELL | ME_NOREFRESH));
122
sleep(MY_WAIT_FOR_USER_TO_FIX_PANIC);
123
VOID(my_fseek(stream,seekptr,MY_SEEK_SET,MYF(0)));
127
if (ferror(stream) || (MyFlags & (MY_NABP | MY_FNABP)))
129
if (MyFlags & (MY_WME | MY_FAE | MY_FNABP))
131
my_error(EE_WRITE, MYF(ME_BELL+ME_WAITTANG),
132
my_filename(fileno(stream)),errno);
134
writenbytes=(uint) -1; /* Return that we got error */
138
if (MyFlags & (MY_NABP | MY_FNABP))
139
writenbytes=0; /* Everything OK */
144
DBUG_RETURN(writenbytes);
147
/* Seek to position in file */
150
my_off_t my_fseek(FILE *stream, my_off_t pos, int whence, myf MyFlags)
152
DBUG_ENTER("my_fseek");
153
DBUG_PRINT("my",("stream: %lx pos: %lu whence: %d MyFlags: %d",
154
stream, pos, whence, MyFlags));
155
DBUG_RETURN(fseek(stream, (off_t) pos, whence) ?
156
MY_FILEPOS_ERROR : (my_off_t) ftell(stream));
160
/* Tell current position of file */
163
my_off_t my_ftell(FILE *stream, myf MyFlags)
166
DBUG_ENTER("my_ftell");
167
DBUG_PRINT("my",("stream: %lx MyFlags: %d",stream, MyFlags));
169
DBUG_PRINT("exit",("ftell: %lu",(ulong) pos));
170
DBUG_RETURN((my_off_t) pos);