16 #include "mysys_priv.h"
17 #include "my_static.h"
19 #include "mysys_err.h"
21 #if defined(__FreeBSD__)
22 extern int getosreldate(
void);
25 static void make_ftype(
char *
to,
int flag);
41 FILE *my_fopen(
const char *filename,
int flags, myf MyFlags)
45 DBUG_ENTER(
"my_fopen");
46 DBUG_PRINT(
"my",(
"Name: '%s' flags: %d MyFlags: %d",
47 filename, flags, MyFlags));
49 make_ftype(type,flags);
52 fd= my_win_fopen(filename, type);
54 fd= fopen(filename, type);
64 int filedesc= my_fileno(fd);
65 if ((uint)filedesc >= my_file_limit)
67 thread_safe_increment(my_stream_opened,&THR_LOCK_open);
71 if ((my_file_info[filedesc].
name= (
char*)
72 my_strdup(filename,MyFlags)))
75 my_file_total_opened++;
76 my_file_info[filedesc].type= STREAM_BY_FOPEN;
78 DBUG_PRINT(
"exit",(
"stream: 0x%lx", (
long) fd));
82 (void) my_fclose(fd,MyFlags);
87 DBUG_PRINT(
"error",(
"Got error %d on open",my_errno));
88 if (MyFlags & (MY_FFNF | MY_FAE | MY_WME))
90 char errbuf[MYSYS_STRERROR_SIZE];
91 my_error((flags & O_RDONLY) || (flags == O_RDONLY ) ? EE_FILENOTFOUND :
93 MYF(ME_BELL+ME_WAITTANG), filename,
94 my_errno, my_strerror(errbuf,
sizeof(errbuf), my_errno));
96 DBUG_RETURN((FILE*) 0);
102 static FILE *my_win_freopen(
const char *path,
const char *
mode, FILE *stream)
104 int handle_fd, fd= _fileno(stream);
107 DBUG_ASSERT(path && stream);
112 if (!freopen(path, mode, stream))
118 if ((osfh= CreateFile(path, GENERIC_READ | GENERIC_WRITE,
119 FILE_SHARE_READ | FILE_SHARE_WRITE |
120 FILE_SHARE_DELETE, NULL,
121 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL,
122 NULL)) == INVALID_HANDLE_VALUE)
125 if ((handle_fd= _open_osfhandle((intptr_t)osfh,
126 _O_APPEND | _O_TEXT)) == -1)
132 if (_dup2(handle_fd, fd) < 0)
143 #elif defined(__FreeBSD__)
147 static int no_close(
void *cookie __attribute__((unused)))
164 static FILE *my_freebsd_freopen(
const char *path,
const char *mode, FILE *stream)
171 old_fd= fileno(stream);
174 stream->_close= no_close;
177 result= freopen(path, mode, stream);
206 FILE *my_freopen(
const char *path,
const char *mode, FILE *stream)
211 result= my_win_freopen(path, mode, stream);
212 #elif defined(__FreeBSD__)
218 if (getosreldate() > 900027)
219 result= freopen(path, mode, stream);
221 result= my_freebsd_freopen(path, mode, stream);
223 result= freopen(path, mode, stream);
231 int my_fclose(FILE *fd, myf MyFlags)
234 DBUG_ENTER(
"my_fclose");
235 DBUG_PRINT(
"my",(
"stream: 0x%lx MyFlags: %d", (
long) fd, MyFlags));
242 err= my_win_fclose(fd);
247 if (MyFlags & (MY_FAE | MY_WME))
249 char errbuf[MYSYS_STRERROR_SIZE];
250 my_error(EE_BADCLOSE, MYF(ME_BELL+ME_WAITTANG), my_filename(file),
251 my_errno, my_strerror(errbuf,
sizeof(errbuf), my_errno));
256 if ((uint) file < my_file_limit && my_file_info[file].type != UNOPEN)
258 my_file_info[
file].type = UNOPEN;
259 my_free(my_file_info[file].
name);
269 FILE *my_fdopen(File Filedes,
const char *
name,
int Flags, myf MyFlags)
273 DBUG_ENTER(
"my_fdopen");
274 DBUG_PRINT(
"my",(
"Fd: %d Flags: %d MyFlags: %d",
275 Filedes, Flags, MyFlags));
277 make_ftype(type,Flags);
279 fd= my_win_fdopen(Filedes, type);
281 fd= fdopen(Filedes, type);
286 if (MyFlags & (MY_FAE | MY_WME))
288 char errbuf[MYSYS_STRERROR_SIZE];
289 my_error(EE_CANT_OPEN_STREAM, MYF(ME_BELL+ME_WAITTANG),
290 my_errno, my_strerror(errbuf,
sizeof(errbuf), my_errno));
297 if ((uint) Filedes < (uint) my_file_limit)
299 if (my_file_info[Filedes].type != UNOPEN)
305 my_file_info[Filedes].name= my_strdup(name,MyFlags);
307 my_file_info[Filedes].type = STREAM_BY_FDOPEN;
312 DBUG_PRINT(
"exit",(
"stream: 0x%lx", (
long) fd));
342 static void make_ftype(
register char *
to,
register int flag)
345 DBUG_ASSERT((flag & (O_TRUNC | O_APPEND)) != (O_TRUNC | O_APPEND));
346 DBUG_ASSERT((flag & (O_WRONLY | O_RDWR)) != (O_WRONLY | O_RDWR));
348 if ((flag & (O_RDONLY|O_WRONLY)) == O_WRONLY)
349 *to++= (flag & O_APPEND) ?
'a' :
'w';
350 else if (flag & O_RDWR)
353 if (flag & (O_TRUNC | O_CREAT))
355 else if (flag & O_APPEND)
365 if (flag & FILE_BINARY)