~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_fopen.cc

  • Committer: Brian Aker
  • Date: 2009-03-05 07:54:39 UTC
  • mto: This revision was merged to the branch mainline in revision 911.
  • Revision ID: brian@tangent.org-20090305075439-ubkvoauwj5er551l
Remove some dead bits of string (and fix the semi_join test).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
16
#include "mysys_priv.h"
 
17
#include "my_static.h"
 
18
#include "mysys_err.h"
 
19
 
 
20
#include <stdio.h>
 
21
#include <stdlib.h>
 
22
#include <errno.h>
 
23
#include <string.h>
 
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];
 
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
 
49
  */
 
50
  {
 
51
    make_ftype(type,flags);
 
52
    fd = fopen(filename, type);
 
53
  }
 
54
 
 
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
    */
 
62
    if ((uint32_t) fileno(fd) >= my_file_limit)
 
63
    {
 
64
      thread_safe_increment(my_stream_opened,&THR_LOCK_open);
 
65
      return(fd);                               /* safeguard */
 
66
    }
 
67
    pthread_mutex_lock(&THR_LOCK_open);
 
68
    if ((my_file_info[fileno(fd)].name = (char*)
 
69
         strdup(filename)))
 
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);
 
75
      return(fd);
 
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);
 
87
  return((FILE*) 0);
 
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--;
 
108
  if ((uint32_t) file < my_file_limit && my_file_info[file].type != UNOPEN)
 
109
  {
 
110
    my_file_info[file].type = UNOPEN;
 
111
    free(my_file_info[file].name);
 
112
  }
 
113
  pthread_mutex_unlock(&THR_LOCK_open);
 
114
  return(err);
 
115
} /* my_fclose */
 
116
 
 
117
 
 
118
 
 
119
/*
 
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
 
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
 
 
132
  NOTE
 
133
    On Unix, O_RDONLY is usually 0
 
134
 
 
135
  MAPPING
 
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
 
141
    a+ == O_RDWR|O_APPEND|O_CREAT
 
142
*/
 
143
 
 
144
static void make_ftype(register char * to, register int flag)
 
145
{
 
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));
 
149
 
 
150
  if ((flag & (O_RDONLY|O_WRONLY)) == O_WRONLY)
 
151
    *to++= (flag & O_APPEND) ? 'a' : 'w';
 
152
  else if (flag & O_RDWR)
 
153
  {
 
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
 
160
      *to++= 'r';
 
161
    *to++= '+';
 
162
  }
 
163
  else
 
164
    *to++= 'r';
 
165
 
 
166
  *to='\0';
 
167
} /* make_ftype */