~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000 MySQL AB
2
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.
6
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.
11
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 */
15
16
/* USE_MY_STREAM isn't set because we can't thrust my_fclose! */
17
18
#include "mysys_priv.h"
19
#include "mysys_err.h"
20
#include <errno.h>
21
#include <stdio.h>
22
23
#ifdef HAVE_FSEEKO
24
#undef ftell
25
#undef fseek
26
#define ftell(A) ftello(A)
27
#define fseek(A,B,C) fseeko((A),(B),(C))
28
#endif
29
30
/*
31
  Read a chunk of bytes from a FILE
32
33
  SYNOPSIS
34
   my_fread()
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
39
40
  RETURN
41
    (size_t) -1 Error
42
    #		Number of bytes read
43
 */
44
45
size_t my_fread(FILE *stream, uchar *Buffer, size_t Count, myf MyFlags)
46
{
47
  size_t readbytes;
48
49
  if ((readbytes= fread(Buffer, sizeof(char), Count, stream)) != Count)
50
  {
51
    if (MyFlags & (MY_WME | MY_FAE | MY_FNABP))
52
    {
53
      if (ferror(stream))
54
	my_error(EE_READ, MYF(ME_BELL+ME_WAITTANG),
55
		 my_filename(fileno(stream)),errno);
56
      else
57
      if (MyFlags & (MY_NABP | MY_FNABP))
58
	my_error(EE_EOFERR, MYF(ME_BELL+ME_WAITTANG),
59
		 my_filename(fileno(stream)),errno);
60
    }
61
    my_errno=errno ? errno : -1;
62
    if (ferror(stream) || MyFlags & (MY_NABP | MY_FNABP))
51.3.19 by Jay Pipes
Phase 6 - Remove DBUG from mysys
63
      return((size_t) -1);			/* Return with error */
1 by brian
clean slate
64
  }
65
  if (MyFlags & (MY_NABP | MY_FNABP))
51.3.19 by Jay Pipes
Phase 6 - Remove DBUG from mysys
66
    return(0);				/* Read ok */
67
  return(readbytes);
1 by brian
clean slate
68
} /* my_fread */
69
70
71
/*
72
  Write a chunk of bytes to a stream
73
74
   my_fwrite()
75
   stream	File descriptor
76
   Buffer	Buffer to write from
77
   Count	Number of bytes to write
78
   MyFlags	Flags on what to do on error
79
80
  RETURN
81
    (size_t) -1 Error
82
    #		Number of bytes written
83
*/
84
85
size_t my_fwrite(FILE *stream, const uchar *Buffer, size_t Count, myf MyFlags)
86
{
87
  size_t writtenbytes =0;
88
  my_off_t seekptr;
89
#if !defined(NO_BACKGROUND) && defined(USE_MY_STREAM)
90
  uint errors;
91
#endif
92
93
#if !defined(NO_BACKGROUND) && defined(USE_MY_STREAM)
94
  errors=0;
95
#endif
96
  seekptr= ftell(stream);
97
  for (;;)
98
  {
99
    size_t written;
100
    if ((written = (size_t) fwrite((char*) Buffer,sizeof(char),
101
                                   Count, stream)) != Count)
102
    {
103
      my_errno=errno;
104
      if (written != (size_t) -1)
105
      {
106
	seekptr+=written;
107
	Buffer+=written;
108
	writtenbytes+=written;
109
	Count-=written;
110
      }
111
#ifdef EINTR
112
      if (errno == EINTR)
113
      {
114
	VOID(my_fseek(stream,seekptr,MY_SEEK_SET,MYF(0)));
115
	continue;
116
      }
117
#endif
118
#if !defined(NO_BACKGROUND) && defined(USE_MY_STREAM)
119
      if (my_thread_var->abort)
120
	MyFlags&= ~ MY_WAIT_IF_FULL;		/* End if aborted by user */
121
      if ((errno == ENOSPC || errno == EDQUOT) &&
122
          (MyFlags & MY_WAIT_IF_FULL))
123
      {
124
        if (!(errors++ % MY_WAIT_GIVE_USER_A_MESSAGE))
125
          my_error(EE_DISK_FULL,MYF(ME_BELL | ME_NOREFRESH),
126
                   "[stream]",my_errno,MY_WAIT_FOR_USER_TO_FIX_PANIC);
127
        VOID(sleep(MY_WAIT_FOR_USER_TO_FIX_PANIC));
128
        VOID(my_fseek(stream,seekptr,MY_SEEK_SET,MYF(0)));
129
        continue;
130
      }
131
#endif
132
      if (ferror(stream) || (MyFlags & (MY_NABP | MY_FNABP)))
133
      {
134
	if (MyFlags & (MY_WME | MY_FAE | MY_FNABP))
135
	{
136
	  my_error(EE_WRITE, MYF(ME_BELL+ME_WAITTANG),
137
		   my_filename(fileno(stream)),errno);
138
	}
139
	writtenbytes= (size_t) -1;        /* Return that we got error */
140
	break;
141
      }
142
    }
143
    if (MyFlags & (MY_NABP | MY_FNABP))
144
      writtenbytes= 0;				/* Everything OK */
145
    else
146
      writtenbytes+= written;
147
    break;
148
  }
51.3.19 by Jay Pipes
Phase 6 - Remove DBUG from mysys
149
  return(writtenbytes);
1 by brian
clean slate
150
} /* my_fwrite */
151
152
153
/* Seek to position in file */
154
155
my_off_t my_fseek(FILE *stream, my_off_t pos, int whence,
156
		  myf MyFlags __attribute__((unused)))
157
{
51.3.19 by Jay Pipes
Phase 6 - Remove DBUG from mysys
158
  return(fseek(stream, (off_t) pos, whence) ?
1 by brian
clean slate
159
	      MY_FILEPOS_ERROR : (my_off_t) ftell(stream));
160
} /* my_seek */
161
162
163
/* Tell current position of file */
164
165
my_off_t my_ftell(FILE *stream, myf MyFlags __attribute__((unused)))
166
{
167
  off_t pos;
168
  pos=ftell(stream);
51.3.19 by Jay Pipes
Phase 6 - Remove DBUG from mysys
169
  return((my_off_t) pos);
1 by brian
clean slate
170
} /* my_ftell */