mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / mysys / my_fstream.c
blob929a59db9cd2474274d4fa0700cb8a36d042c2d5
1 /*
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"
23 #include <errno.h>
24 #include <stdio.h>
26 #ifdef HAVE_FSEEKO
27 #undef ftell
28 #undef fseek
29 #define ftell(A) ftello(A)
30 #define fseek(A,B,C) fseeko((A),(B),(C))
31 #endif
34 Read a chunk of bytes from a FILE
36 SYNOPSIS
37 my_fread()
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
43 RETURN
44 (size_t) -1 Error
45 # Number of bytes read
48 size_t my_fread(FILE *stream, uchar *Buffer, size_t Count, myf MyFlags)
50 size_t readbytes;
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))
60 if (ferror(stream))
61 my_error(EE_READ, MYF(ME_BELL+ME_WAITTANG),
62 my_filename(fileno(stream)),errno);
63 else
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);
75 } /* my_fread */
79 Write a chunk of bytes to a stream
81 my_fwrite()
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
87 RETURN
88 (size_t) -1 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;
95 my_off_t seekptr;
96 #if !defined(NO_BACKGROUND) && defined(USE_MY_STREAM)
97 uint errors;
98 #endif
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)
104 errors=0;
105 #endif
106 seekptr= ftell(stream);
107 for (;;)
109 size_t written;
110 if ((written = (size_t) fwrite((char*) Buffer,sizeof(char),
111 Count, stream)) != Count)
113 DBUG_PRINT("error",("Write only %d bytes", (int) writtenbytes));
114 my_errno=errno;
115 if (written != (size_t) -1)
117 seekptr+=written;
118 Buffer+=written;
119 writtenbytes+=written;
120 Count-=written;
122 #ifdef EINTR
123 if (errno == EINTR)
125 VOID(my_fseek(stream,seekptr,MY_SEEK_SET,MYF(0)));
126 continue;
128 #endif
129 #if !defined(NO_BACKGROUND) && defined(USE_MY_STREAM)
130 #ifdef THREAD
131 if (my_thread_var->abort)
132 MyFlags&= ~ MY_WAIT_IF_FULL; /* End if aborted by user */
133 #endif
134 if ((errno == ENOSPC || errno == EDQUOT) &&
135 (MyFlags & MY_WAIT_IF_FULL))
137 wait_for_free_space("[stream]", errors);
138 errors++;
139 VOID(my_fseek(stream,seekptr,MY_SEEK_SET,MYF(0)));
140 continue;
142 #endif
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 */
151 break;
154 if (MyFlags & (MY_NABP | MY_FNABP))
155 writtenbytes= 0; /* Everything OK */
156 else
157 writtenbytes+= written;
158 break;
160 DBUG_RETURN(writtenbytes);
161 } /* my_fwrite */
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));
174 } /* my_seek */
177 /* Tell current position of file */
179 my_off_t my_ftell(FILE *stream, myf MyFlags __attribute__((unused)))
181 off_t pos;
182 DBUG_ENTER("my_ftell");
183 DBUG_PRINT("my",("stream: 0x%lx MyFlags: %d", (long) stream, MyFlags));
184 pos=ftell(stream);
185 DBUG_PRINT("exit",("ftell: %lu",(ulong) pos));
186 DBUG_RETURN((my_off_t) pos);
187 } /* my_ftell */