~drizzle-trunk/drizzle/development

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
1802.10.2 by Monty Taylor
Update all of the copyright headers to include the correct address.
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
1 by brian
clean slate
15
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
16
#include <config.h>
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
17
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
18
#include <drizzled/internal/my_sys.h>
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
19
20
#include <fcntl.h>
21
22
#ifdef HAVE_SYS_STAT_H
23
# include <sys/stat.h>
24
#endif
25
26
#include <algorithm>
27
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
28
#include <drizzled/internal/m_string.h>
1 by brian
clean slate
29
1067.4.10 by Nathan Williams
Converted all cmin/cmax usages in the mysys directory to std::min/max.
30
using namespace std;
31
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
32
namespace drizzled
33
{
34
namespace internal
35
{
36
2241.4.31 by Stewart Smith
strlength() can be private to mf_format.cc and not in my_sys header
37
static size_t strlength(const char *str);
38
1 by brian
clean slate
39
/*
40
  Formats a filename with possible replace of directory of extension
41
  Function can handle the case where 'to' == 'name'
42
  For a description of the flag values, consult my_sys.h
43
  The arguments should be in unix format.
44
*/
45
46
char * fn_format(char * to, const char *name, const char *dir,
482 by Brian Aker
Remove uint.
47
		    const char *extension, uint32_t flag)
1 by brian
clean slate
48
{
266.6.6 by Andy Lester
fixing some constants
49
  char dev[FN_REFLEN], buff[FN_REFLEN], *pos;
50
  const char *startpos = name;
1 by brian
clean slate
51
  const char *ext;
2194.3.1 by Olaf van der Spek
Remove register keyword
52
  size_t length;
1 by brian
clean slate
53
  size_t dev_length;
54
55
  /* Copy and skip directory */
266.6.6 by Andy Lester
fixing some constants
56
  name+=(length=dirname_part(dev, startpos, &dev_length));
1 by brian
clean slate
57
  if (length == 0 || (flag & MY_REPLACE_DIR))
58
  {
59
    /* Use given directory */
461 by Monty Taylor
Removed NullS. bu-bye.
60
    convert_dirname(dev,dir,NULL);		/* Fix to this OS */
1 by brian
clean slate
61
  }
62
  else if ((flag & MY_RELATIVE_PATH) && !test_if_hard_path(dev))
63
  {
64
    /* Put 'dir' before the given path */
629.5.2 by Toru Maesaka
Second pass of replacing MySQL's strmake() with libc calls
65
    strncpy(buff,dev,sizeof(buff)-1);
461 by Monty Taylor
Removed NullS. bu-bye.
66
    pos=convert_dirname(dev,dir,NULL);
629.5.2 by Toru Maesaka
Second pass of replacing MySQL's strmake() with libc calls
67
    strncpy(pos,buff,sizeof(buff)-1- (int) (pos-dev));
1 by brian
clean slate
68
  }
69
70
  if (flag & MY_UNPACK_FILENAME)
71
    (void) unpack_dirname(dev,dev);		/* Replace ~/.. with dir */
72
73
  if (!(flag & MY_APPEND_EXT) &&
461 by Monty Taylor
Removed NullS. bu-bye.
74
      (pos= (char*) strchr(name,FN_EXTCHAR)) != NULL)
1 by brian
clean slate
75
  {
76
    if ((flag & MY_REPLACE_EXT) == 0)		/* If we should keep old ext */
77
    {
78
      length=strlength(name);			/* Use old extension */
79
      ext = "";
80
    }
81
    else
82
    {
83
      length= (size_t) (pos-(char*) name);	/* Change extension */
84
      ext= extension;
85
    }
86
  }
87
  else
88
  {
89
    length=strlength(name);			/* No ext, use the now one */
90
    ext=extension;
91
  }
92
93
  if (strlen(dev)+length+strlen(ext) >= FN_REFLEN || length >= FN_LEN )
94
  {
95
    /* To long path, return original or NULL */
96
    size_t tmp_length;
97
    if (flag & MY_SAFE_PATH)
461 by Monty Taylor
Removed NullS. bu-bye.
98
      return NULL;
1067.4.10 by Nathan Williams
Converted all cmin/cmax usages in the mysys directory to std::min/max.
99
    tmp_length= min(strlength(startpos), (size_t)(FN_REFLEN-1));
629.5.2 by Toru Maesaka
Second pass of replacing MySQL's strmake() with libc calls
100
    strncpy(to,startpos,tmp_length);
101
    to[tmp_length]= '\0';
1 by brian
clean slate
102
  }
103
  else
104
  {
105
    if (to == startpos)
106
    {
629.3.4 by Kristian Nielsen
Take Mats'es changes from bmove()->memcpy(), and fix all of them to be
107
      memmove(buff, name, length); /* Save name for last copy */
1 by brian
clean slate
108
      name=buff;
109
    }
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
110
    char *tmp= strcpy(to, dev) + strlen(dev);
111
    pos= strncpy(tmp,name,length) + length;
112
    (void) strcpy(pos,ext);			/* Don't convert extension */
1 by brian
clean slate
113
  }
114
  /*
115
    If MY_RETURN_REAL_PATH and MY_RESOLVE_SYMLINK is given, only do
116
    realpath if the file is a symbolic link
117
  */
118
  if (flag & MY_RETURN_REAL_PATH)
1060.2.5 by Eric Lambert
-removed my_realpath from my_symlink and replaced it with calls to posix realpath
119
  {
120
    struct stat stat_buff;
121
    char rp_buff[PATH_MAX];
122
    if ((!flag & MY_RESOLVE_SYMLINKS) || 
1060.2.7 by Eric Lambert
-it appears that the result from symlink() in ha_archive can not be
123
       (!lstat(to,&stat_buff) && S_ISLNK(stat_buff.st_mode)))
1060.2.5 by Eric Lambert
-removed my_realpath from my_symlink and replaced it with calls to posix realpath
124
    {
125
      if (!realpath(to,rp_buff))
126
        my_load_path(rp_buff, to, NULL);
127
      rp_buff[FN_REFLEN-1]= '\0';
128
      strcpy(to,rp_buff);
129
    }
130
  }
1 by brian
clean slate
131
  else if (flag & MY_RESOLVE_SYMLINKS)
132
  {
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
133
    strcpy(buff,to);
1060.2.1 by Eric Lambert
-replace calls to my_readlink with readlink
134
    ssize_t sym_link_size= readlink(buff,to,FN_REFLEN-1);
135
    if (sym_link_size >= 0)
136
      to[sym_link_size]= '\0';
1 by brian
clean slate
137
  }
51.3.22 by Jay Pipes
Final round of removal of DBUG in mysys/, including Makefile
138
  return(to);
1 by brian
clean slate
139
} /* fn_format */
140
141
142
/*
143
  strlength(const string str)
144
  Return length of string with end-space:s not counted.
145
*/
146
2241.4.31 by Stewart Smith
strlength() can be private to mf_format.cc and not in my_sys header
147
static size_t strlength(const char *str)
1 by brian
clean slate
148
{
2194.3.1 by Olaf van der Spek
Remove register keyword
149
  const char* found= str;
150
  const char* pos= str;
1 by brian
clean slate
151
152
  while (*pos)
153
  {
154
    if (*pos != ' ')
155
    {
156
      while (*++pos && *pos != ' ') {};
157
      if (!*pos)
158
      {
159
	found=pos;			/* String ends here */
160
	break;
161
      }
162
    }
163
    found=pos;
164
    while (*++pos == ' ') {};
165
  }
51.3.22 by Jay Pipes
Final round of removal of DBUG in mysys/, including Makefile
166
  return((size_t) (found - str));
1 by brian
clean slate
167
} /* strlength */
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
168
169
} /* namespace internal */
170
} /* namespace drizzled */