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
#include "mysys_priv.h"
17
#include "my_static.h"
19
#include "mysys_err.h"
21
static void make_ftype(char * to,int flag);
28
FileName Path-name of file
29
Flags Read | write | append | trunc (like for open())
30
MyFlags Flags for handling errors
37
FILE *my_fopen(const char *filename, int flags, myf MyFlags)
42
if we are not creating, then we need to use my_access to make sure
43
the file exists since Windows doesn't handle files like "com1.sym"
47
make_ftype(type,flags);
48
fd = fopen(filename, type);
54
The test works if MY_NFILE < 128. The problem is that fileno() is char
55
on some OS (SUNOS). Actually the filename save isn't that important
56
so we can ignore if this doesn't work.
58
if ((uint) fileno(fd) >= my_file_limit)
60
thread_safe_increment(my_stream_opened,&THR_LOCK_open);
61
return(fd); /* safeguard */
63
pthread_mutex_lock(&THR_LOCK_open);
64
if ((my_file_info[fileno(fd)].name = (char*)
65
my_strdup(filename,MyFlags)))
68
my_file_total_opened++;
69
my_file_info[fileno(fd)].type = STREAM_BY_FOPEN;
70
pthread_mutex_unlock(&THR_LOCK_open);
73
pthread_mutex_unlock(&THR_LOCK_open);
74
(void) my_fclose(fd,MyFlags);
79
if (MyFlags & (MY_FFNF | MY_FAE | MY_WME))
80
my_error((flags & O_RDONLY) || (flags == O_RDONLY ) ? EE_FILENOTFOUND :
82
MYF(ME_BELL+ME_WAITTANG), filename, my_errno);
89
int my_fclose(FILE *fd, myf MyFlags)
93
pthread_mutex_lock(&THR_LOCK_open);
95
if ((err = fclose(fd)) < 0)
98
if (MyFlags & (MY_FAE | MY_WME))
99
my_error(EE_BADCLOSE, MYF(ME_BELL+ME_WAITTANG),
100
my_filename(file),errno);
104
if ((uint) file < my_file_limit && my_file_info[file].type != UNOPEN)
106
my_file_info[file].type = UNOPEN;
107
my_free(my_file_info[file].name, MYF(MY_ALLOW_ZERO_PTR));
109
pthread_mutex_unlock(&THR_LOCK_open);
116
Make a fopen() typestring from a open() type bitmap
120
to String for fopen() is stored here
121
flag Flag used by open()
124
This routine attempts to find the best possible match
125
between a numeric option and a string option that could be
126
fed to fopen. There is not a 1 to 1 mapping between the two.
129
On Unix, O_RDONLY is usually 0
133
w == O_WRONLY|O_TRUNC|O_CREAT
134
a == O_WRONLY|O_APPEND|O_CREAT
136
w+ == O_RDWR|O_TRUNC|O_CREAT
137
a+ == O_RDWR|O_APPEND|O_CREAT
140
static void make_ftype(register char * to, register int flag)
142
/* check some possible invalid combinations */
143
assert((flag & (O_TRUNC | O_APPEND)) != (O_TRUNC | O_APPEND));
144
assert((flag & (O_WRONLY | O_RDWR)) != (O_WRONLY | O_RDWR));
146
if ((flag & (O_RDONLY|O_WRONLY)) == O_WRONLY)
147
*to++= (flag & O_APPEND) ? 'a' : 'w';
148
else if (flag & O_RDWR)
150
/* Add '+' after theese */
151
if (flag & (O_TRUNC | O_CREAT))
153
else if (flag & O_APPEND)
162
#if FILE_BINARY /* If we have binary-files */
163
if (flag & FILE_BINARY)