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"
24
static void make_ftype(char * to,int flag);
31
FileName Path-name of file
32
Flags Read | write | append | trunc (like for open())
33
MyFlags Flags for handling errors
40
FILE *my_fopen(const char *filename, int flags, myf MyFlags)
45
if we are not creating, then we need to use my_access to make sure
46
the file exists since Windows doesn't handle files like "com1.sym"
50
make_ftype(type,flags);
51
fd = fopen(filename, type);
57
The test works if MY_NFILE < 128. The problem is that fileno() is char
58
on some OS (SUNOS). Actually the filename save isn't that important
59
so we can ignore if this doesn't work.
61
if ((uint) fileno(fd) >= my_file_limit)
63
thread_safe_increment(my_stream_opened,&THR_LOCK_open);
64
return(fd); /* safeguard */
66
pthread_mutex_lock(&THR_LOCK_open);
67
if ((my_file_info[fileno(fd)].name = (char*)
68
my_strdup(filename,MyFlags)))
71
my_file_total_opened++;
72
my_file_info[fileno(fd)].type = STREAM_BY_FOPEN;
73
pthread_mutex_unlock(&THR_LOCK_open);
76
pthread_mutex_unlock(&THR_LOCK_open);
77
(void) my_fclose(fd,MyFlags);
82
if (MyFlags & (MY_FFNF | MY_FAE | MY_WME))
83
my_error((flags & O_RDONLY) || (flags == O_RDONLY ) ? EE_FILENOTFOUND :
85
MYF(ME_BELL+ME_WAITTANG), filename, my_errno);
92
int my_fclose(FILE *fd, myf MyFlags)
96
pthread_mutex_lock(&THR_LOCK_open);
98
if ((err = fclose(fd)) < 0)
101
if (MyFlags & (MY_FAE | MY_WME))
102
my_error(EE_BADCLOSE, MYF(ME_BELL+ME_WAITTANG),
103
my_filename(file),errno);
107
if ((uint) file < my_file_limit && my_file_info[file].type != UNOPEN)
109
my_file_info[file].type = UNOPEN;
110
free(my_file_info[file].name);
112
pthread_mutex_unlock(&THR_LOCK_open);
119
Make a fopen() typestring from a open() type bitmap
123
to String for fopen() is stored here
124
flag Flag used by open()
127
This routine attempts to find the best possible match
128
between a numeric option and a string option that could be
129
fed to fopen. There is not a 1 to 1 mapping between the two.
132
On Unix, O_RDONLY is usually 0
136
w == O_WRONLY|O_TRUNC|O_CREAT
137
a == O_WRONLY|O_APPEND|O_CREAT
139
w+ == O_RDWR|O_TRUNC|O_CREAT
140
a+ == O_RDWR|O_APPEND|O_CREAT
143
static void make_ftype(register char * to, register int flag)
145
/* check some possible invalid combinations */
146
assert((flag & (O_TRUNC | O_APPEND)) != (O_TRUNC | O_APPEND));
147
assert((flag & (O_WRONLY | O_RDWR)) != (O_WRONLY | O_RDWR));
149
if ((flag & (O_RDONLY|O_WRONLY)) == O_WRONLY)
150
*to++= (flag & O_APPEND) ? 'a' : 'w';
151
else if (flag & O_RDWR)
153
/* Add '+' after theese */
154
if (flag & (O_TRUNC | O_CREAT))
156
else if (flag & O_APPEND)