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 |
||
612.2.6
by Monty Taylor
OpenSolaris builds. |
16 |
#include "mysys_priv.h" |
17 |
#include "my_static.h" |
|
18 |
#include "mysys_err.h" |
|
19 |
||
595
by Brian Aker
Fix, partial, for Sun Studio. |
20 |
#include <stdio.h> |
21 |
#include <stdlib.h> |
|
1
by brian
clean slate |
22 |
#include <errno.h> |
656.1.26
by Monty Taylor
Finally removed all of the my_malloc stuff. |
23 |
#include <string.h> |
1
by brian
clean slate |
24 |
|
25 |
static void make_ftype(char * to,int flag); |
|
26 |
||
27 |
/*
|
|
28 |
Open a file as stream
|
|
29 |
||
30 |
SYNOPSIS
|
|
31 |
my_fopen()
|
|
32 |
FileName Path-name of file
|
|
33 |
Flags Read | write | append | trunc (like for open())
|
|
34 |
MyFlags Flags for handling errors
|
|
35 |
||
36 |
RETURN
|
|
37 |
0 Error
|
|
38 |
# File handler
|
|
39 |
*/
|
|
40 |
||
41 |
FILE *my_fopen(const char *filename, int flags, myf MyFlags) |
|
42 |
{
|
|
43 |
FILE *fd; |
|
44 |
char type[5]; |
|
660.1.3
by Eric Herman
removed trailing whitespace with simple script: |
45 |
/*
|
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"
|
|
48 |
very well
|
|
1
by brian
clean slate |
49 |
*/
|
50 |
{
|
|
51 |
make_ftype(type,flags); |
|
52 |
fd = fopen(filename, type); |
|
53 |
}
|
|
660.1.3
by Eric Herman
removed trailing whitespace with simple script: |
54 |
|
1
by brian
clean slate |
55 |
if (fd != 0) |
56 |
{
|
|
57 |
/*
|
|
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.
|
|
61 |
*/
|
|
895
by Brian Aker
Completion (?) of uint conversion. |
62 |
if ((uint32_t) fileno(fd) >= my_file_limit) |
1
by brian
clean slate |
63 |
{
|
64 |
thread_safe_increment(my_stream_opened,&THR_LOCK_open); |
|
51.3.19
by Jay Pipes
Phase 6 - Remove DBUG from mysys |
65 |
return(fd); /* safeguard */ |
1
by brian
clean slate |
66 |
}
|
67 |
pthread_mutex_lock(&THR_LOCK_open); |
|
68 |
if ((my_file_info[fileno(fd)].name = (char*) |
|
656.1.26
by Monty Taylor
Finally removed all of the my_malloc stuff. |
69 |
strdup(filename))) |
1
by brian
clean slate |
70 |
{
|
71 |
my_stream_opened++; |
|
72 |
my_file_total_opened++; |
|
73 |
my_file_info[fileno(fd)].type = STREAM_BY_FOPEN; |
|
74 |
pthread_mutex_unlock(&THR_LOCK_open); |
|
51.3.19
by Jay Pipes
Phase 6 - Remove DBUG from mysys |
75 |
return(fd); |
1
by brian
clean slate |
76 |
}
|
77 |
pthread_mutex_unlock(&THR_LOCK_open); |
|
78 |
(void) my_fclose(fd,MyFlags); |
|
79 |
my_errno=ENOMEM; |
|
80 |
}
|
|
81 |
else
|
|
82 |
my_errno=errno; |
|
83 |
if (MyFlags & (MY_FFNF | MY_FAE | MY_WME)) |
|
84 |
my_error((flags & O_RDONLY) || (flags == O_RDONLY ) ? EE_FILENOTFOUND : |
|
85 |
EE_CANTCREATEFILE, |
|
86 |
MYF(ME_BELL+ME_WAITTANG), filename, my_errno); |
|
51.3.19
by Jay Pipes
Phase 6 - Remove DBUG from mysys |
87 |
return((FILE*) 0); |
1
by brian
clean slate |
88 |
} /* my_fopen */ |
89 |
||
90 |
||
91 |
/* Close a stream */
|
|
92 |
||
93 |
int my_fclose(FILE *fd, myf MyFlags) |
|
94 |
{
|
|
95 |
int err,file; |
|
96 |
||
97 |
pthread_mutex_lock(&THR_LOCK_open); |
|
98 |
file=fileno(fd); |
|
99 |
if ((err = fclose(fd)) < 0) |
|
100 |
{
|
|
101 |
my_errno=errno; |
|
102 |
if (MyFlags & (MY_FAE | MY_WME)) |
|
103 |
my_error(EE_BADCLOSE, MYF(ME_BELL+ME_WAITTANG), |
|
104 |
my_filename(file),errno); |
|
105 |
}
|
|
106 |
else
|
|
107 |
my_stream_opened--; |
|
895
by Brian Aker
Completion (?) of uint conversion. |
108 |
if ((uint32_t) file < my_file_limit && my_file_info[file].type != UNOPEN) |
1
by brian
clean slate |
109 |
{
|
110 |
my_file_info[file].type = UNOPEN; |
|
477
by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that. |
111 |
free(my_file_info[file].name); |
1
by brian
clean slate |
112 |
}
|
113 |
pthread_mutex_unlock(&THR_LOCK_open); |
|
51.3.19
by Jay Pipes
Phase 6 - Remove DBUG from mysys |
114 |
return(err); |
1
by brian
clean slate |
115 |
} /* my_fclose */ |
116 |
||
117 |
||
118 |
||
660.1.3
by Eric Herman
removed trailing whitespace with simple script: |
119 |
/*
|
1
by brian
clean slate |
120 |
Make a fopen() typestring from a open() type bitmap
|
121 |
||
122 |
SYNOPSIS
|
|
123 |
make_ftype()
|
|
124 |
to String for fopen() is stored here
|
|
125 |
flag Flag used by open()
|
|
126 |
||
127 |
IMPLEMENTATION
|
|
660.1.3
by Eric Herman
removed trailing whitespace with simple script: |
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.
|
|
131 |
||
1
by brian
clean slate |
132 |
NOTE
|
133 |
On Unix, O_RDONLY is usually 0
|
|
134 |
||
135 |
MAPPING
|
|
660.1.3
by Eric Herman
removed trailing whitespace with simple script: |
136 |
r == O_RDONLY
|
137 |
w == O_WRONLY|O_TRUNC|O_CREAT
|
|
138 |
a == O_WRONLY|O_APPEND|O_CREAT
|
|
139 |
r+ == O_RDWR
|
|
140 |
w+ == O_RDWR|O_TRUNC|O_CREAT
|
|
1
by brian
clean slate |
141 |
a+ == O_RDWR|O_APPEND|O_CREAT
|
142 |
*/
|
|
143 |
||
144 |
static void make_ftype(register char * to, register int flag) |
|
145 |
{
|
|
660.1.3
by Eric Herman
removed trailing whitespace with simple script: |
146 |
/* check some possible invalid combinations */
|
51.3.19
by Jay Pipes
Phase 6 - Remove DBUG from mysys |
147 |
assert((flag & (O_TRUNC | O_APPEND)) != (O_TRUNC | O_APPEND)); |
148 |
assert((flag & (O_WRONLY | O_RDWR)) != (O_WRONLY | O_RDWR)); |
|
1
by brian
clean slate |
149 |
|
660.1.3
by Eric Herman
removed trailing whitespace with simple script: |
150 |
if ((flag & (O_RDONLY|O_WRONLY)) == O_WRONLY) |
151 |
*to++= (flag & O_APPEND) ? 'a' : 'w'; |
|
152 |
else if (flag & O_RDWR) |
|
1
by brian
clean slate |
153 |
{
|
660.1.3
by Eric Herman
removed trailing whitespace with simple script: |
154 |
/* Add '+' after theese */
|
155 |
if (flag & (O_TRUNC | O_CREAT)) |
|
156 |
*to++= 'w'; |
|
157 |
else if (flag & O_APPEND) |
|
158 |
*to++= 'a'; |
|
159 |
else
|
|
1
by brian
clean slate |
160 |
*to++= 'r'; |
660.1.3
by Eric Herman
removed trailing whitespace with simple script: |
161 |
*to++= '+'; |
162 |
}
|
|
163 |
else
|
|
1
by brian
clean slate |
164 |
*to++= 'r'; |
165 |
||
166 |
*to='\0'; |
|
167 |
} /* make_ftype */ |