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"
18
#include "mysys_err.h"
25
static void make_ftype(char * to,int flag);
32
FileName Path-name of file
33
Flags Read | write | append | trunc (like for open())
34
MyFlags Flags for handling errors
41
FILE *my_fopen(const char *filename, int flags, myf MyFlags)
46
if we are not creating, then we need to use my_access to make sure
47
the file exists since Windows doesn't handle files like "com1.sym"
51
make_ftype(type,flags);
52
fd = fopen(filename, type);
58
The test works if MY_NFILE < 128. The problem is that fileno() is char
59
on some OS (SUNOS). Actually the filename save isn't that important
60
so we can ignore if this doesn't work.
62
if ((uint32_t) fileno(fd) >= my_file_limit)
64
thread_safe_increment(my_stream_opened,&THR_LOCK_open);
65
return(fd); /* safeguard */
67
pthread_mutex_lock(&THR_LOCK_open);
68
if ((my_file_info[fileno(fd)].name = (char*)
72
my_file_total_opened++;
73
my_file_info[fileno(fd)].type = STREAM_BY_FOPEN;
74
pthread_mutex_unlock(&THR_LOCK_open);
77
pthread_mutex_unlock(&THR_LOCK_open);
78
(void) my_fclose(fd,MyFlags);
83
if (MyFlags & (MY_FFNF | MY_FAE | MY_WME))
84
my_error((flags & O_RDONLY) || (flags == O_RDONLY ) ? EE_FILENOTFOUND :
86
MYF(ME_BELL+ME_WAITTANG), filename, my_errno);
93
int my_fclose(FILE *fd, myf MyFlags)
97
pthread_mutex_lock(&THR_LOCK_open);
99
if ((err = fclose(fd)) < 0)
102
if (MyFlags & (MY_FAE | MY_WME))
103
my_error(EE_BADCLOSE, MYF(ME_BELL+ME_WAITTANG),
104
my_filename(file),errno);
108
if ((uint32_t) file < my_file_limit && my_file_info[file].type != UNOPEN)
110
my_file_info[file].type = UNOPEN;
111
free(my_file_info[file].name);
113
pthread_mutex_unlock(&THR_LOCK_open);
120
Make a fopen() typestring from a open() type bitmap
124
to String for fopen() is stored here
125
flag Flag used by open()
128
This routine attempts to find the best possible match
129
between a numeric option and a string option that could be
130
fed to fopen. There is not a 1 to 1 mapping between the two.
133
On Unix, O_RDONLY is usually 0
137
w == O_WRONLY|O_TRUNC|O_CREAT
138
a == O_WRONLY|O_APPEND|O_CREAT
140
w+ == O_RDWR|O_TRUNC|O_CREAT
141
a+ == O_RDWR|O_APPEND|O_CREAT
144
static void make_ftype(register char * to, register int flag)
146
/* check some possible invalid combinations */
147
assert((flag & (O_TRUNC | O_APPEND)) != (O_TRUNC | O_APPEND));
148
assert((flag & (O_WRONLY | O_RDWR)) != (O_WRONLY | O_RDWR));
150
if ((flag & (O_RDONLY|O_WRONLY)) == O_WRONLY)
151
*to++= (flag & O_APPEND) ? 'a' : 'w';
152
else if (flag & O_RDWR)
154
/* Add '+' after theese */
155
if (flag & (O_TRUNC | O_CREAT))
157
else if (flag & O_APPEND)