~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_fopen.cc

  • Committer: Brian Aker
  • Date: 2009-05-28 00:15:58 UTC
  • mfrom: (1039.1.7 merge)
  • Revision ID: brian@gaz-20090528001558-xychs3xtpg8wz5i8
MErge Brian

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/mysys_priv.h"
17
 
#include "my_static.h"
18
 
#include "mysys/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 != NULL)
56
 
  {
57
 
    my_stream_opened++;
58
 
    return fd;                          /* safeguard */
59
 
  }
60
 
  else
61
 
    my_errno=errno;
62
 
  if (MyFlags & (MY_FFNF | MY_FAE | MY_WME))
63
 
    my_error((flags & O_RDONLY) || (flags == O_RDONLY ) ? EE_FILENOTFOUND :
64
 
             EE_CANTCREATEFILE,
65
 
             MYF(ME_BELL+ME_WAITTANG), filename, my_errno);
66
 
  return NULL;
67
 
} /* my_fopen */
68
 
 
69
 
 
70
 
        /* Close a stream */
71
 
 
72
 
int my_fclose(FILE *fd, myf MyFlags)
73
 
{
74
 
  int err,file;
75
 
 
76
 
  file= fileno(fd);
77
 
  if ((err = fclose(fd)) < 0)
78
 
  {
79
 
    my_errno=errno;
80
 
    if (MyFlags & (MY_FAE | MY_WME))
81
 
      my_error(EE_BADCLOSE, MYF(ME_BELL+ME_WAITTANG),
82
 
               my_filename(file),errno);
83
 
  }
84
 
  else
85
 
    my_stream_opened--;
86
 
 
87
 
  return(err);
88
 
} /* my_fclose */
89
 
 
90
 
 
91
 
 
92
 
/*
93
 
  Make a fopen() typestring from a open() type bitmap
94
 
 
95
 
  SYNOPSIS
96
 
    make_ftype()
97
 
    to          String for fopen() is stored here
98
 
    flag        Flag used by open()
99
 
 
100
 
  IMPLEMENTATION
101
 
    This routine attempts to find the best possible match
102
 
    between  a numeric option and a string option that could be
103
 
    fed to fopen.  There is not a 1 to 1 mapping between the two.
104
 
 
105
 
  NOTE
106
 
    On Unix, O_RDONLY is usually 0
107
 
 
108
 
  MAPPING
109
 
    r  == O_RDONLY
110
 
    w  == O_WRONLY|O_TRUNC|O_CREAT
111
 
    a  == O_WRONLY|O_APPEND|O_CREAT
112
 
    r+ == O_RDWR
113
 
    w+ == O_RDWR|O_TRUNC|O_CREAT
114
 
    a+ == O_RDWR|O_APPEND|O_CREAT
115
 
*/
116
 
 
117
 
static void make_ftype(register char * to, register int flag)
118
 
{
119
 
  /* check some possible invalid combinations */
120
 
  assert((flag & (O_TRUNC | O_APPEND)) != (O_TRUNC | O_APPEND));
121
 
  assert((flag & (O_WRONLY | O_RDWR)) != (O_WRONLY | O_RDWR));
122
 
 
123
 
  if ((flag & (O_RDONLY|O_WRONLY)) == O_WRONLY)
124
 
    *to++= (flag & O_APPEND) ? 'a' : 'w';
125
 
  else if (flag & O_RDWR)
126
 
  {
127
 
    /* Add '+' after theese */
128
 
    if (flag & (O_TRUNC | O_CREAT))
129
 
      *to++= 'w';
130
 
    else if (flag & O_APPEND)
131
 
      *to++= 'a';
132
 
    else
133
 
      *to++= 'r';
134
 
    *to++= '+';
135
 
  }
136
 
  else
137
 
    *to++= 'r';
138
 
 
139
 
  *to='\0';
140
 
} /* make_ftype */